Skip to content

Commit c62cc6a

Browse files
committed
tools: hv: Enable debug logs for hv_kvp_daemon
jira LE-3207 feature tools_hv commit-author Shradha Gupta <[email protected]> commit a9c0b33 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> (cherry picked from commit a9c0b33) Signed-off-by: Jonathan Maple <[email protected]> Signed-off-by: Jonathan Maple <[email protected]>
1 parent 0c88564 commit c62cc6a

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
@@ -83,6 +83,7 @@ enum {
8383
};
8484

8585
static int in_hand_shake;
86+
static int debug;
8687

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

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+
186201
static void kvp_update_mem_state(int pool)
187202
{
188203
FILE *filep;
@@ -270,6 +285,8 @@ static int kvp_file_init(void)
270285
return 1;
271286
kvp_file_info[i].num_records = 0;
272287
kvp_update_mem_state(i);
288+
if (debug)
289+
kvp_dump_initial_pools(i);
273290
}
274291

275292
return 0;
@@ -297,6 +314,9 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
297314
* Found a match; just move the remaining
298315
* entries up.
299316
*/
317+
if (debug)
318+
syslog(LOG_DEBUG, "%s: deleting the KVP: pool=%d key=%s val=%s",
319+
__func__, pool, record[i].key, record[i].value);
300320
if (i == (num_records - 1)) {
301321
kvp_file_info[pool].num_records--;
302322
kvp_update_file(pool);
@@ -315,20 +335,36 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
315335
kvp_update_file(pool);
316336
return 0;
317337
}
338+
339+
if (debug)
340+
syslog(LOG_DEBUG, "%s: could not delete KVP: pool=%d key=%s. Record not found",
341+
__func__, pool, key);
342+
318343
return 1;
319344
}
320345

321346
static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
322347
const __u8 *value, int value_size)
323348
{
324-
int i;
325-
int num_records;
326349
struct kvp_record *record;
350+
int num_records;
327351
int num_blocks;
352+
int i;
353+
354+
if (debug)
355+
syslog(LOG_DEBUG, "%s: got a KVP: pool=%d key=%s val=%s",
356+
__func__, pool, key, value);
328357

329358
if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
330-
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
359+
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
360+
syslog(LOG_ERR, "%s: Too long key or value: key=%s, val=%s",
361+
__func__, key, value);
362+
363+
if (debug)
364+
syslog(LOG_DEBUG, "%s: Too long key or value: pool=%d, key=%s, val=%s",
365+
__func__, pool, key, value);
331366
return 1;
367+
}
332368

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

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

362-
if (record == NULL)
401+
if (!record) {
402+
syslog(LOG_ERR, "%s: Memory alloc failure", __func__);
363403
return 1;
404+
}
364405
kvp_file_info[pool].num_blocks++;
365406

366407
}
367408
memcpy(record[i].value, value, value_size);
368409
memcpy(record[i].key, key, key_size);
369410
kvp_file_info[pool].records = record;
370411
kvp_file_info[pool].num_records++;
412+
413+
if (debug)
414+
syslog(LOG_DEBUG, "%s: added: pool=%d key=%s val=%s",
415+
__func__, pool, key, value);
416+
371417
kvp_update_file(pool);
372418
return 0;
373419
}
@@ -1661,6 +1707,7 @@ void print_usage(char *argv[])
16611707
fprintf(stderr, "Usage: %s [options]\n"
16621708
"Options are:\n"
16631709
" -n, --no-daemon stay in foreground, don't daemonize\n"
1710+
" -d, --debug Enable debug logs(syslog debug by default)\n"
16641711
" -h, --help print this help\n", argv[0]);
16651712
}
16661713

@@ -1682,10 +1729,11 @@ int main(int argc, char *argv[])
16821729
static struct option long_options[] = {
16831730
{"help", no_argument, 0, 'h' },
16841731
{"no-daemon", no_argument, 0, 'n' },
1732+
{"debug", no_argument, 0, 'd' },
16851733
{0, 0, 0, 0 }
16861734
};
16871735

1688-
while ((opt = getopt_long(argc, argv, "hn", long_options,
1736+
while ((opt = getopt_long(argc, argv, "hnd", long_options,
16891737
&long_index)) != -1) {
16901738
switch (opt) {
16911739
case 'n':
@@ -1694,6 +1742,9 @@ int main(int argc, char *argv[])
16941742
case 'h':
16951743
print_usage(argv);
16961744
exit(0);
1745+
case 'd':
1746+
debug = 1;
1747+
break;
16971748
default:
16981749
print_usage(argv);
16991750
exit(EXIT_FAILURE);
@@ -1716,6 +1767,9 @@ int main(int argc, char *argv[])
17161767
*/
17171768
kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
17181769

1770+
if (debug)
1771+
syslog(LOG_INFO, "Logging debug info in syslog(debug)");
1772+
17191773
if (kvp_file_init()) {
17201774
syslog(LOG_ERR, "Failed to initialize the pools");
17211775
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)