From 8d51f032998b0aea3320bb6845cf81f223e34241 Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Mon, 27 Aug 2012 18:37:53 -0400 Subject: [PATCH] kprobe support Signed-off-by: Julien Desfossez --- src/common.c | 9 +++++ src/cursesdisplay.c | 34 ++++++++++++------ src/lttngtop.c | 87 ++++++++++++++++++++++++++++++++++++++------- src/lttngtoptypes.h | 9 +++++ 4 files changed, 117 insertions(+), 22 deletions(-) diff --git a/src/common.c b/src/common.c index 3a2fa0c..e55ff74 100644 --- a/src/common.c +++ b/src/common.c @@ -430,6 +430,7 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end) struct processtop *tmp, *tmp2, *new; struct cputime *tmpcpu, *newcpu; struct files *tmpfile, *newfile; + struct kprobes *tmpprobe, *newprobe; dst = g_new0(struct lttngtop, 1); dst->start = start; @@ -438,6 +439,7 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end) dst->process_table = g_ptr_array_new(); dst->files_table = g_ptr_array_new(); dst->cpu_table = g_ptr_array_new(); + dst->kprobes_table = g_ptr_array_new(); dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal); g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table, dst->process_hash_table); @@ -520,6 +522,13 @@ struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end) */ g_ptr_array_add(dst->cpu_table, newcpu); } + for (i = 0; i < lttngtop.kprobes_table->len; i++) { + tmpprobe = g_ptr_array_index(lttngtop.kprobes_table, i); + newprobe = g_new0(struct kprobes, 1); + memcpy(newprobe, tmpprobe, sizeof(struct kprobes)); + tmpprobe->count = 0; + g_ptr_array_add(dst->kprobes_table, newprobe); + } /* FIXME : better algo */ /* create the threads index if required */ for (i = 0; i < dst->process_table->len; i++) { diff --git a/src/cursesdisplay.c b/src/cursesdisplay.c index 1fcc798..6988029 100644 --- a/src/cursesdisplay.c +++ b/src/cursesdisplay.c @@ -70,6 +70,7 @@ pthread_t keyboard_thread; struct header_view cputopview[6]; struct header_view iostreamtopview[3]; struct header_view fileview[3]; +struct header_view kprobeview[2]; void reset_ncurses() { @@ -523,25 +524,34 @@ gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2) void update_kprobes_display() { int i, column; + struct kprobes *probe; + int header_offset = 2; + int current_line = 0; - set_window_title(center, "Kprobes Top"); - /* + set_window_title(center, "Kprobes Top "); wattron(center, A_BOLD); column = 1; - for (i = 0; i < 6; i++) { - if (toggle_virt < 0 && (i == 3 || i == 4)) { - continue; - } - if (cputopview[i].sort) { + for (i = 0; i < 2; i++) { + if (kprobeview[i].sort) { wattron(center, A_UNDERLINE); pref_current_sort = i; } - mvwprintw(center, 1, column, cputopview[i].title); + mvwprintw(center, 1, column, "%s", kprobeview[i].title); wattroff(center, A_UNDERLINE); - column += 10; + column += 30; } wattroff(center, A_BOLD); - */ + + for (i = 0; i < data->kprobes_table->len; i++) { + column = 1; + probe = g_ptr_array_index(data->kprobes_table, i); + mvwprintw(center, current_line + header_offset, column, + "%s", probe->probe_name + 6); + column += 30; + mvwprintw(center, current_line + header_offset, column, + "%d", probe->count); + current_line++; + } } void update_cputop_display() @@ -1647,6 +1657,10 @@ void init_view_headers() fileview[1].title = strdup("READ"); fileview[1].sort = 1; fileview[2].title = strdup("WRITE"); + + kprobeview[0].title = strdup("NAME"); + kprobeview[1].title = strdup("HIT"); + kprobeview[1].sort = 1; } void init_ncurses() diff --git a/src/lttngtop.c b/src/lttngtop.c index 2442884..2fe70df 100644 --- a/src/lttngtop.c +++ b/src/lttngtop.c @@ -255,6 +255,22 @@ error: return BT_CB_ERROR_STOP; } +enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data) +{ + int i; + struct kprobes *kprobe; + + /* for kprobes */ + for (i = 0; i < lttngtop.kprobes_table->len; i++) { + kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); + if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) { + kprobe->count++; + } + } + + return BT_CB_OK; +} + /* * hook on each event to check the timestamp and refresh the display if * necessary @@ -483,6 +499,7 @@ void init_lttngtop() lttngtop.process_table = g_ptr_array_new(); lttngtop.files_table = g_ptr_array_new(); lttngtop.cpu_table = g_ptr_array_new(); + lttngtop.kprobes_table = g_ptr_array_new(); } void usage(FILE *fp) @@ -567,7 +584,9 @@ void iter_trace(struct bt_context *bt_ctx) { struct bt_ctf_iter *iter; struct bt_iter_pos begin_pos; + struct kprobes *kprobe; const struct bt_ctf_event *event; + int i; int ret = 0; begin_pos.type = BT_SEEK_BEGIN; @@ -622,6 +641,16 @@ void iter_trace(struct bt_context *bt_ctx) "lttng_statedump_file_descriptor"), NULL, 0, handle_statedump_file_descriptor, NULL, NULL, NULL); + + /* for kprobes */ + for (i = 0; i < lttngtop.kprobes_table->len; i++) { + kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); + bt_ctf_iter_add_callback(iter, + g_quark_from_static_string( + kprobe->probe_name), + NULL, 0, handle_kprobes, + NULL, NULL, NULL); + } } while ((event = bt_ctf_iter_read_event(iter)) != NULL) { @@ -985,6 +1014,49 @@ end: return ret; } +int enable_kprobes(struct lttng_handle *handle, char *channel_name) +{ + struct lttng_event ev; + struct kprobes *kprobe; + int ret = 0; + int i; + + /* + kprobe = g_new0(struct kprobes, 1); + kprobe->probe_addr = 0; + kprobe->probe_offset = 0; + asprintf(&kprobe->probe_name, "probe_sys_open"); + asprintf(&kprobe->symbol_name, "sys_open"); + g_ptr_array_add(lttngtop.kprobes_table, kprobe); + + kprobe = g_new0(struct kprobes, 1); + kprobe->probe_addr = 0; + kprobe->probe_offset = 0; + asprintf(&kprobe->probe_name, "probe_sys_close"); + asprintf(&kprobe->symbol_name, "sys_close"); + g_ptr_array_add(lttngtop.kprobes_table, kprobe); + */ + + for (i = 0; i < lttngtop.kprobes_table->len; i++) { + kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); + + memset(&ev, '\0', sizeof(struct lttng_event)); + ev.type = LTTNG_EVENT_PROBE; + sprintf(ev.attr.probe.symbol_name, "%s", kprobe->symbol_name); + sprintf(ev.name, "%s", kprobe->probe_name); + ev.attr.probe.addr = kprobe->probe_addr; + ev.attr.probe.offset = kprobe->probe_offset; + if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) { + fprintf(stderr,"error enabling kprobes : %s\n", + helper_lttcomm_get_readable_code(ret)); + goto end; + } + } + +end: + return ret; +} + int setup_live_tracing() { struct lttng_domain dom; @@ -1078,19 +1150,10 @@ int setup_live_tracing() goto error_session; } - /* - memset(&ev, '\0', sizeof(struct lttng_event)); - ev.type = LTTNG_EVENT_PROBE; - sprintf(ev.attr.probe.symbol_name, "sys_open"); - sprintf(ev.name, "probe_sys_open"); - ev.attr.probe.addr = 0; - ev.attr.probe.offset = 0; - if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) { - fprintf(stderr,"error enabling kprobes : %s\n", - helper_lttcomm_get_readable_code(ret)); + ret = enable_kprobes(handle, channel_name); + if (ret < 0) { goto error_session; } - */ kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID; lttng_add_context(handle, &kctxpid, NULL, NULL); @@ -1147,11 +1210,11 @@ int main(int argc, char **argv) signal(SIGTERM, handle_textdump_sigterm); signal(SIGINT, handle_textdump_sigterm); } + init_lttngtop(); ret = setup_live_tracing(); if (ret < 0) { goto end; } - init_lttngtop(); if (!opt_textdump) { pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); diff --git a/src/lttngtoptypes.h b/src/lttngtoptypes.h index c2b99c6..43b3381 100644 --- a/src/lttngtoptypes.h +++ b/src/lttngtoptypes.h @@ -25,6 +25,7 @@ struct lttngtop { GPtrArray *process_table; /* struct processtop */ GPtrArray *files_table; /* struct files */ GPtrArray *cpu_table; /* struct cputime */ + GPtrArray *kprobes_table; /* struct kprobes */ unsigned long start; unsigned long end; unsigned int nbproc; @@ -181,4 +182,12 @@ struct header_view { int reverse; }; +struct kprobes { + char *probe_name; + char *symbol_name; + int probe_addr; + int probe_offset; + int count; +}; + #endif /* LTTNGTOPTYPES_H */ -- 2.34.1