Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: hv: Enable debug logs for hv_kvp_daemon

Allow the KVP daemon to log the KVP updates triggered in the VM
with a new debug flag(-d).
When the daemon is started with this flag, it logs updates and debug
information in syslog with loglevel LOG_DEBUG. This information comes
in handy for debugging issues where the key-value pairs for certain
pools show mismatch/incorrect values.
The distro-vendors can further consume these changes and modify the
respective service files to redirect the logs to specific files as
needed.

Signed-off-by: Shradha Gupta <shradhagupta@linux.microsoft.com>
Reviewed-by: Naman Jain <namjain@linux.microsoft.com>
Reviewed-by: Dexuan Cui <decui@microsoft.com>
Link: https://lore.kernel.org/r/1744715978-8185-1-git-send-email-shradhagupta@linux.microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
Message-ID: <1744715978-8185-1-git-send-email-shradhagupta@linux.microsoft.com>

authored by

Shradha Gupta and committed by
Wei Liu
a9c0b33e 82f2b0b9

+59 -5
+59 -5
tools/hv/hv_kvp_daemon.c
··· 84 84 }; 85 85 86 86 static int in_hand_shake; 87 + static int debug; 87 88 88 89 static char *os_name = ""; 89 90 static char *os_major = ""; ··· 185 184 kvp_release_lock(pool); 186 185 } 187 186 187 + static void kvp_dump_initial_pools(int pool) 188 + { 189 + int i; 190 + 191 + syslog(LOG_DEBUG, "===Start dumping the contents of pool %d ===\n", 192 + pool); 193 + 194 + for (i = 0; i < kvp_file_info[pool].num_records; i++) 195 + syslog(LOG_DEBUG, "pool: %d, %d/%d key=%s val=%s\n", 196 + pool, i + 1, kvp_file_info[pool].num_records, 197 + kvp_file_info[pool].records[i].key, 198 + kvp_file_info[pool].records[i].value); 199 + } 200 + 188 201 static void kvp_update_mem_state(int pool) 189 202 { 190 203 FILE *filep; ··· 286 271 return 1; 287 272 kvp_file_info[i].num_records = 0; 288 273 kvp_update_mem_state(i); 274 + if (debug) 275 + kvp_dump_initial_pools(i); 289 276 } 290 277 291 278 return 0; ··· 315 298 * Found a match; just move the remaining 316 299 * entries up. 317 300 */ 301 + if (debug) 302 + syslog(LOG_DEBUG, "%s: deleting the KVP: pool=%d key=%s val=%s", 303 + __func__, pool, record[i].key, record[i].value); 318 304 if (i == (num_records - 1)) { 319 305 kvp_file_info[pool].num_records--; 320 306 kvp_update_file(pool); ··· 336 316 kvp_update_file(pool); 337 317 return 0; 338 318 } 319 + 320 + if (debug) 321 + syslog(LOG_DEBUG, "%s: could not delete KVP: pool=%d key=%s. Record not found", 322 + __func__, pool, key); 323 + 339 324 return 1; 340 325 } 341 326 342 327 static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size, 343 328 const __u8 *value, int value_size) 344 329 { 345 - int i; 346 - int num_records; 347 330 struct kvp_record *record; 331 + int num_records; 348 332 int num_blocks; 333 + int i; 334 + 335 + if (debug) 336 + syslog(LOG_DEBUG, "%s: got a KVP: pool=%d key=%s val=%s", 337 + __func__, pool, key, value); 349 338 350 339 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) || 351 - (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) 340 + (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) { 341 + syslog(LOG_ERR, "%s: Too long key or value: key=%s, val=%s", 342 + __func__, key, value); 343 + 344 + if (debug) 345 + syslog(LOG_DEBUG, "%s: Too long key or value: pool=%d, key=%s, val=%s", 346 + __func__, pool, key, value); 352 347 return 1; 348 + } 353 349 354 350 /* 355 351 * First update the in-memory state. ··· 385 349 */ 386 350 memcpy(record[i].value, value, value_size); 387 351 kvp_update_file(pool); 352 + if (debug) 353 + syslog(LOG_DEBUG, "%s: updated: pool=%d key=%s val=%s", 354 + __func__, pool, key, value); 388 355 return 0; 389 356 } 390 357 ··· 399 360 record = realloc(record, sizeof(struct kvp_record) * 400 361 ENTRIES_PER_BLOCK * (num_blocks + 1)); 401 362 402 - if (record == NULL) 363 + if (!record) { 364 + syslog(LOG_ERR, "%s: Memory alloc failure", __func__); 403 365 return 1; 366 + } 404 367 kvp_file_info[pool].num_blocks++; 405 368 406 369 } ··· 410 369 memcpy(record[i].key, key, key_size); 411 370 kvp_file_info[pool].records = record; 412 371 kvp_file_info[pool].num_records++; 372 + 373 + if (debug) 374 + syslog(LOG_DEBUG, "%s: added: pool=%d key=%s val=%s", 375 + __func__, pool, key, value); 376 + 413 377 kvp_update_file(pool); 414 378 return 0; 415 379 } ··· 1768 1722 fprintf(stderr, "Usage: %s [options]\n" 1769 1723 "Options are:\n" 1770 1724 " -n, --no-daemon stay in foreground, don't daemonize\n" 1725 + " -d, --debug Enable debug logs(syslog debug by default)\n" 1771 1726 " -h, --help print this help\n", argv[0]); 1772 1727 } 1773 1728 ··· 1790 1743 static struct option long_options[] = { 1791 1744 {"help", no_argument, 0, 'h' }, 1792 1745 {"no-daemon", no_argument, 0, 'n' }, 1746 + {"debug", no_argument, 0, 'd' }, 1793 1747 {0, 0, 0, 0 } 1794 1748 }; 1795 1749 1796 - while ((opt = getopt_long(argc, argv, "hn", long_options, 1750 + while ((opt = getopt_long(argc, argv, "hnd", long_options, 1797 1751 &long_index)) != -1) { 1798 1752 switch (opt) { 1799 1753 case 'n': ··· 1803 1755 case 'h': 1804 1756 print_usage(argv); 1805 1757 exit(0); 1758 + case 'd': 1759 + debug = 1; 1760 + break; 1806 1761 default: 1807 1762 print_usage(argv); 1808 1763 exit(EXIT_FAILURE); ··· 1827 1776 * unpredictable amount of time to finish. 1828 1777 */ 1829 1778 kvp_get_domain_name(full_domain_name, sizeof(full_domain_name)); 1779 + 1780 + if (debug) 1781 + syslog(LOG_INFO, "Logging debug info in syslog(debug)"); 1830 1782 1831 1783 if (kvp_file_init()) { 1832 1784 syslog(LOG_ERR, "Failed to initialize the pools");