Skip to content

Commit 3590a63

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]>
1 parent 49618a6 commit 3590a63

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
@@ -77,6 +77,7 @@ enum {
7777
};
7878

7979
static int in_hand_shake;
80+
static int debug;
8081

8182
static char *os_name = "";
8283
static char *os_major = "";
@@ -172,6 +173,20 @@ static void kvp_update_file(int pool)
172173
kvp_release_lock(pool);
173174
}
174175

176+
static void kvp_dump_initial_pools(int pool)
177+
{
178+
int i;
179+
180+
syslog(LOG_DEBUG, "===Start dumping the contents of pool %d ===\n",
181+
pool);
182+
183+
for (i = 0; i < kvp_file_info[pool].num_records; i++)
184+
syslog(LOG_DEBUG, "pool: %d, %d/%d key=%s val=%s\n",
185+
pool, i + 1, kvp_file_info[pool].num_records,
186+
kvp_file_info[pool].records[i].key,
187+
kvp_file_info[pool].records[i].value);
188+
}
189+
175190
static void kvp_update_mem_state(int pool)
176191
{
177192
FILE *filep;
@@ -259,6 +274,8 @@ static int kvp_file_init(void)
259274
return 1;
260275
kvp_file_info[i].num_records = 0;
261276
kvp_update_mem_state(i);
277+
if (debug)
278+
kvp_dump_initial_pools(i);
262279
}
263280

264281
return 0;
@@ -286,6 +303,9 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
286303
* Found a match; just move the remaining
287304
* entries up.
288305
*/
306+
if (debug)
307+
syslog(LOG_DEBUG, "%s: deleting the KVP: pool=%d key=%s val=%s",
308+
__func__, pool, record[i].key, record[i].value);
289309
if (i == (num_records - 1)) {
290310
kvp_file_info[pool].num_records--;
291311
kvp_update_file(pool);
@@ -304,20 +324,36 @@ static int kvp_key_delete(int pool, const __u8 *key, int key_size)
304324
kvp_update_file(pool);
305325
return 0;
306326
}
327+
328+
if (debug)
329+
syslog(LOG_DEBUG, "%s: could not delete KVP: pool=%d key=%s. Record not found",
330+
__func__, pool, key);
331+
307332
return 1;
308333
}
309334

310335
static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
311336
const __u8 *value, int value_size)
312337
{
313-
int i;
314-
int num_records;
315338
struct kvp_record *record;
339+
int num_records;
316340
int num_blocks;
341+
int i;
342+
343+
if (debug)
344+
syslog(LOG_DEBUG, "%s: got a KVP: pool=%d key=%s val=%s",
345+
__func__, pool, key, value);
317346

318347
if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
319-
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
348+
(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
349+
syslog(LOG_ERR, "%s: Too long key or value: key=%s, val=%s",
350+
__func__, key, value);
351+
352+
if (debug)
353+
syslog(LOG_DEBUG, "%s: Too long key or value: pool=%d, key=%s, val=%s",
354+
__func__, pool, key, value);
320355
return 1;
356+
}
321357

322358
/*
323359
* First update the in-memory state.
@@ -337,6 +373,9 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
337373
*/
338374
memcpy(record[i].value, value, value_size);
339375
kvp_update_file(pool);
376+
if (debug)
377+
syslog(LOG_DEBUG, "%s: updated: pool=%d key=%s val=%s",
378+
__func__, pool, key, value);
340379
return 0;
341380
}
342381

@@ -348,15 +387,22 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
348387
record = realloc(record, sizeof(struct kvp_record) *
349388
ENTRIES_PER_BLOCK * (num_blocks + 1));
350389

351-
if (record == NULL)
390+
if (!record) {
391+
syslog(LOG_ERR, "%s: Memory alloc failure", __func__);
352392
return 1;
393+
}
353394
kvp_file_info[pool].num_blocks++;
354395

355396
}
356397
memcpy(record[i].value, value, value_size);
357398
memcpy(record[i].key, key, key_size);
358399
kvp_file_info[pool].records = record;
359400
kvp_file_info[pool].num_records++;
401+
402+
if (debug)
403+
syslog(LOG_DEBUG, "%s: added: pool=%d key=%s val=%s",
404+
__func__, pool, key, value);
405+
360406
kvp_update_file(pool);
361407
return 0;
362408
}
@@ -1355,6 +1401,7 @@ void print_usage(char *argv[])
13551401
fprintf(stderr, "Usage: %s [options]\n"
13561402
"Options are:\n"
13571403
" -n, --no-daemon stay in foreground, don't daemonize\n"
1404+
" -d, --debug Enable debug logs(syslog debug by default)\n"
13581405
" -h, --help print this help\n", argv[0]);
13591406
}
13601407

@@ -1376,10 +1423,11 @@ int main(int argc, char *argv[])
13761423
static struct option long_options[] = {
13771424
{"help", no_argument, 0, 'h' },
13781425
{"no-daemon", no_argument, 0, 'n' },
1426+
{"debug", no_argument, 0, 'd' },
13791427
{0, 0, 0, 0 }
13801428
};
13811429

1382-
while ((opt = getopt_long(argc, argv, "hn", long_options,
1430+
while ((opt = getopt_long(argc, argv, "hnd", long_options,
13831431
&long_index)) != -1) {
13841432
switch (opt) {
13851433
case 'n':
@@ -1388,6 +1436,9 @@ int main(int argc, char *argv[])
13881436
case 'h':
13891437
print_usage(argv);
13901438
exit(0);
1439+
case 'd':
1440+
debug = 1;
1441+
break;
13911442
default:
13921443
print_usage(argv);
13931444
exit(EXIT_FAILURE);
@@ -1410,6 +1461,9 @@ int main(int argc, char *argv[])
14101461
*/
14111462
kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
14121463

1464+
if (debug)
1465+
syslog(LOG_INFO, "Logging debug info in syslog(debug)");
1466+
14131467
if (kvp_file_init()) {
14141468
syslog(LOG_ERR, "Failed to initialize the pools");
14151469
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)