Skip to content

Commit 9fe16b6

Browse files
committed
#892: Resolve tracepoint id in asprof
1 parent 6248a87 commit 9fe16b6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/launcher/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,34 @@ static void run_jattach(int pid, String& cmd) {
361361
}
362362
}
363363

364+
// Resolve a kernel tracepoint name (e.g. syscalls:sys_enter_openat) to a numerical id
365+
static int get_tracepoint_id(const char* name) {
366+
char buf[256];
367+
if ((size_t)snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%s/id", name) >= sizeof(buf)) {
368+
return 0;
369+
}
370+
371+
char* p = strchr(buf, ':');
372+
if (p == NULL || strchr(p + 1, ':') != NULL) {
373+
return 0;
374+
}
375+
*p = '/';
376+
377+
int fd = open(buf, O_RDONLY);
378+
if (fd == -1) {
379+
return 0;
380+
}
381+
382+
ssize_t r = read(fd, buf, sizeof(buf) - 1);
383+
close(fd);
384+
385+
if (r > 0) {
386+
buf[r] = 0;
387+
return atoi(buf);
388+
}
389+
return 0;
390+
}
391+
364392

365393
int main(int argc, const char** argv) {
366394
Args args(argc, argv);
@@ -394,9 +422,13 @@ int main(int argc, const char** argv) {
394422

395423
} else if (arg == "-e" || arg == "--event") {
396424
const char* event = args.next();
425+
int tracepoint_id;
397426
if (strchr(event, ',') != NULL && event[strlen(event) - 1] == '/') {
398427
// PMU event, e.g.: cpu/umask=0x1,event=0xd3/
399428
params << ",event=" << String(event).replace(',', ":");
429+
} else if (strchr(event, ':') != NULL && (tracepoint_id = get_tracepoint_id(event)) > 0) {
430+
// Try to resolve tracepoint id before dropping root privileges
431+
params << ",event=trace:" << tracepoint_id;
400432
} else {
401433
params << ",event=" << event;
402434
}

0 commit comments

Comments
 (0)