Skip to content

Commit a9c0b33

Browse files
Shradha Guptaliuw
authored andcommitted
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 <[email protected]> Reviewed-by: Naman Jain <[email protected]> Reviewed-by: Dexuan Cui <[email protected]> Link: https://lore.kernel.org/r/1744715978-8185-1-git-send-email-shradhagupta@linux.microsoft.com Signed-off-by: Wei Liu <[email protected]> Message-ID: <1744715978-8185-1-git-send-email-shradhagupta@linux.microsoft.com>
1 parent 82f2b0b commit a9c0b33

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

tools/hv/hv_kvp_daemon.c

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ enum {
8484
};
8585

8686
static int in_hand_shake;
87+
static int debug;
8788

8889
static char *os_name = "";
8990
static char *os_major = "";
@@ -184,6 +185,20 @@ static void kvp_update_file(int pool)
184185
kvp_release_lock(pool);
185186
}
186187

188+
static void kvp_dump_initial_pools(int pool)
189+
{
190+
int i;
191+
192+
syslog(LOG_DEBUG, "===Start dumping the contents of pool %d ===\n",
193+
pool);
194+
195+
for (i = 0; i < kvp_file_info[pool].num_records; i++)
196+
syslog(LOG_DEBUG, "pool: %d, %d/%d key=%s val=%s\n",
197+
pool, i + 1, kvp_file_info[pool].num_records,
198+
kvp_file_info[pool].records[i].key,
199+
kvp_file_info[pool].records[i].value);
200+
}
201+
187202
static void kvp_update_mem_state(int pool)
188203
{
189204
FILE *filep;
@@ -271,6 +286,8 @@ static int kvp_file_init(void)
271286
return 1;
272287
kvp_file_info[i].num_records = 0;
273288
kvp_update_mem_state(i);
289+
if (debug)
290+
kvp_dump_initial_pools(i);
274291
}
275292

276293
return 0;
@@ -298,6 +315,9 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
298315
* Found a match; just move the remaining
299316
* entries up.
300317
*/
318+
if (debug)
319+
syslog(LOG_DEBUG, "%s: deleting the KVP: pool=%d key=%s val=%s",
320+
__func__, pool, record[i].key, record[i].value);
301321
if (i == (num_records - 1)) {
302322
kvp_file_info[pool].num_records--;
303323
kvp_update_file(pool);
@@ -316,20 +336,36 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
316336
kvp_update_file(pool);
317337
return 0;
318338
}
339+
340+
if (debug)
341+
syslog(LOG_DEBUG, "%s: could not delete KVP: pool=%d key=%s. Record not found",
342+
__func__, pool, key);
343+
319344
return 1;
320345
}
321346

322347
static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
323348
const __u8 *value, int value_size)
324349
{
325-
int i;
326-
int num_records;
327350
struct kvp_record *record;
351+
int num_records;
328352
int num_blocks;
353+
int i;
354+
355+
if (debug)
356+
syslog(LOG_DEBUG, "%s: got a KVP: pool=%d key=%s val=%s",
357+
__func__, pool, key, value);
329358

330359
if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
331-
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
360+
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
361+
syslog(LOG_ERR, "%s: Too long key or value: key=%s, val=%s",
362+
__func__, key, value);
363+
364+
if (debug)
365+
syslog(LOG_DEBUG, "%s: Too long key or value: pool=%d, key=%s, val=%s",
366+
__func__, pool, key, value);
332367
return 1;
368+
}
333369

334370
/*
335371
* First update the in-memory state.
@@ -349,6 +385,9 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
349385
*/
350386
memcpy(record[i].value, value, value_size);
351387
kvp_update_file(pool);
388+
if (debug)
389+
syslog(LOG_DEBUG, "%s: updated: pool=%d key=%s val=%s",
390+
__func__, pool, key, value);
352391
return 0;
353392
}
354393

@@ -360,15 +399,22 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
360399
record = realloc(record, sizeof(struct kvp_record) *
361400
ENTRIES_PER_BLOCK * (num_blocks + 1));
362401

363-
if (record == NULL)
402+
if (!record) {
403+
syslog(LOG_ERR, "%s: Memory alloc failure", __func__);
364404
return 1;
405+
}
365406
kvp_file_info[pool].num_blocks++;
366407

367408
}
368409
memcpy(record[i].value, value, value_size);
369410
memcpy(record[i].key, key, key_size);
370411
kvp_file_info[pool].records = record;
371412
kvp_file_info[pool].num_records++;
413+
414+
if (debug)
415+
syslog(LOG_DEBUG, "%s: added: pool=%d key=%s val=%s",
416+
__func__, pool, key, value);
417+
372418
kvp_update_file(pool);
373419
return 0;
374420
}
@@ -1722,6 +1768,7 @@ void print_usage(char *argv[])
17221768
fprintf(stderr, "Usage: %s [options]\n"
17231769
"Options are:\n"
17241770
" -n, --no-daemon stay in foreground, don't daemonize\n"
1771+
" -d, --debug Enable debug logs(syslog debug by default)\n"
17251772
" -h, --help print this help\n", argv[0]);
17261773
}
17271774

@@ -1743,10 +1790,11 @@ int main(int argc, char *argv[])
17431790
static struct option long_options[] = {
17441791
{"help", no_argument, 0, 'h' },
17451792
{"no-daemon", no_argument, 0, 'n' },
1793+
{"debug", no_argument, 0, 'd' },
17461794
{0, 0, 0, 0 }
17471795
};
17481796

1749-
while ((opt = getopt_long(argc, argv, "hn", long_options,
1797+
while ((opt = getopt_long(argc, argv, "hnd", long_options,
17501798
&long_index)) != -1) {
17511799
switch (opt) {
17521800
case 'n':
@@ -1755,6 +1803,9 @@ int main(int argc, char *argv[])
17551803
case 'h':
17561804
print_usage(argv);
17571805
exit(0);
1806+
case 'd':
1807+
debug = 1;
1808+
break;
17581809
default:
17591810
print_usage(argv);
17601811
exit(EXIT_FAILURE);
@@ -1777,6 +1828,9 @@ int main(int argc, char *argv[])
17771828
*/
17781829
kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
17791830

1831+
if (debug)
1832+
syslog(LOG_INFO, "Logging debug info in syslog(debug)");
1833+
17801834
if (kvp_file_init()) {
17811835
syslog(LOG_ERR, "Failed to initialize the pools");
17821836
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)