From 8b09996a30ff97dc57034b1985b4e7b91dc81a66 Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Sat, 10 Jul 2021 18:25:47 +0800 Subject: [PATCH] libbpf-tools: gethostlatency allow specify libc path This commit adds a new option to gethostlatency which allows user to specify which libc to use for tracing. This is useful when application is not linked against default libc. Signed-off-by: Hengqi Chen --- libbpf-tools/gethostlatency.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libbpf-tools/gethostlatency.c b/libbpf-tools/gethostlatency.c index a5529bcd..4b57d0e2 100644 --- a/libbpf-tools/gethostlatency.c +++ b/libbpf-tools/gethostlatency.c @@ -26,6 +26,7 @@ static volatile sig_atomic_t exiting = 0; static pid_t target_pid = 0; +static const char *libc_path = NULL; const char *argp_program_version = "gethostlatency 0.1"; const char *argp_program_bug_address = @@ -33,7 +34,7 @@ const char *argp_program_bug_address = const char argp_program_doc[] = "Show latency for getaddrinfo/gethostbyname[2] calls.\n" "\n" -"USAGE: gethostlatency [-h] [-p PID]\n" +"USAGE: gethostlatency [-h] [-p PID] [-l LIBC]\n" "\n" "EXAMPLES:\n" " gethostlatency # time getaddrinfo/gethostbyname[2] calls\n" @@ -41,6 +42,7 @@ const char argp_program_doc[] = static const struct argp_option opts[] = { { "pid", 'p', "PID", 0, "Process ID to trace" }, + { "libc", 'l', "LIBC", 0, "Specify which libc.so to use" }, { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, {}, }; @@ -59,6 +61,13 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) } target_pid = pid; break; + case 'l': + libc_path = strdup(arg); + if (access(libc_path, F_OK)) { + warn("Invalid libc: %s\n", arg); + argp_usage(state); + } + break; case 'h': argp_state_help(state, stderr, ARGP_HELP_STD_HELP); break; @@ -99,6 +108,11 @@ static int get_libc_path(char *path) char *filename; float version; + if (libc_path) { + memcpy(path, libc_path, strlen(libc_path)); + return 0; + } + f = fopen("/proc/self/maps", "r"); if (!f) return -errno; -- 2.34.1