GeezerVison is showing github.com — unwind patch for bcc · SeeFlowerX/unwindbcc@f654863
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/cc/libbpf.c +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1450,6 +1450,15 @@ void * bpf_open_perf_buffer_opts(perf_reader_raw_cb raw_cb, | 1450 | 1450 | attr.sample_type = PERF_SAMPLE_RAW; | 1451 | 1451 | attr.sample_period = 1; | 1452 | 1452 | attr.wakeup_events = opts->wakeup_events; | | 1453 | + | | 1454 | +if (opts->unwind_call_stack == 1) { | | 1455 | + attr.sample_type |= PERF_SAMPLE_STACK_USER | PERF_SAMPLE_REGS_USER; | | 1456 | + attr.sample_stack_user = 16384; // MAX=65528 | | 1457 | + attr.sample_regs_user = ((1ULL << 33) - 1); | | 1458 | + attr.size = sizeof(struct perf_event_attr); | | 1459 | + reader->is_unwind_call_stack = true; | | 1460 | + } | | 1461 | + | 1453 | 1462 | pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC); | 1454 | 1463 | if (pfd < 0) { | 1455 | 1464 | fprintf(stderr, "perf_event_open: %s\n", strerror(errno)); | |
Collapse file Copy file name to clipboardExpand all lines: src/cc/libbpf.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ struct bcc_perf_buffer_opts { | 39 | 39 | int pid; | 40 | 40 | int cpu; | 41 | 41 | int wakeup_events; | | 42 | + int unwind_call_stack; | 42 | 43 | }; | 43 | 44 |
| 44 | 45 | int bcc_create_map(enum bpf_map_type map_type, const char *name, | |
Collapse file Copy file name to clipboardExpand all lines: src/cc/perf_reader.c +121-14Lines changed: 121 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | #include <sys/mman.h> | 26 | 26 | #include <sys/types.h> | 27 | 27 | #include <unistd.h> | | 28 | +#include <errno.h> | | 29 | +#include <arpa/inet.h> | | 30 | +#include <sys/socket.h> | | 31 | +#include <linux/un.h> | 28 | 32 | #include <linux/types.h> | 29 | 33 | #include <linux/perf_event.h> | 30 | 34 |
| @@ -37,20 +41,6 @@ enum { | 37 | 41 | RB_USED_IN_READ = 2, // used in read | 38 | 42 | }; | 39 | 43 |
| 40 | | -struct perf_reader { | 41 | | - perf_reader_raw_cb raw_cb; | 42 | | - perf_reader_lost_cb lost_cb; | 43 | | - void *cb_cookie; // to be returned in the cb | 44 | | - void *buf; // for keeping segmented data | 45 | | - size_t buf_size; | 46 | | - void *base; | 47 | | - int rb_use_state; | 48 | | - pid_t rb_read_tid; | 49 | | - int page_size; | 50 | | - int page_cnt; | 51 | | - int fd; | 52 | | -}; | 53 | | - | 54 | 44 | struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb, | 55 | 45 | perf_reader_lost_cb lost_cb, | 56 | 46 | void *cb_cookie, int page_cnt) { | @@ -63,6 +53,7 @@ struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb, | 63 | 53 | reader->fd = -1; | 64 | 54 | reader->page_size = getpagesize(); | 65 | 55 | reader->page_cnt = page_cnt; | | 56 | + reader->is_unwind_call_stack = false; | 66 | 57 | return reader; | 67 | 58 | } | 68 | 59 |
| @@ -114,10 +105,87 @@ struct perf_sample_trace_kprobe { | 114 | 105 | uint64_t ip; | 115 | 106 | }; | 116 | 107 |
| | 108 | +bool ReadFully(int fd, void* data, size_t byte_count) { | | 109 | + uint8_t* p = (uint8_t*)data; | | 110 | + size_t remaining = byte_count; | | 111 | + while (remaining > 0) { | | 112 | + ssize_t n = read(fd, p, remaining); | | 113 | + if (n <= 0) return false; | | 114 | + p += n; | | 115 | + remaining -= n; | | 116 | + } | | 117 | + return true; | | 118 | +} | | 119 | + | | 120 | +bool WriteFully(int fd, void* data, size_t byte_count) { | | 121 | + uint8_t* p = (uint8_t*)data; | | 122 | + size_t remaining = byte_count; | | 123 | + while (remaining > 0) { | | 124 | + ssize_t n = write(fd, p, remaining); | | 125 | + if (n == -1) return false; | | 126 | + p += n; | | 127 | + remaining -= n; | | 128 | + } | | 129 | + return true; | | 130 | +} | | 131 | + | | 132 | +static void print_frame_info(int pid, uint8_t *ptr, int write_size) { | | 133 | + fprintf(stderr, "[%s] %d %p %d\n", __FUNCTION__, pid, ptr, write_size); | | 134 | + | | 135 | + int fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); | | 136 | + if (fd == -1) { | | 137 | + fprintf(stderr, "cannot socket()!\n"); | | 138 | + } | | 139 | + const char *socket_path = "/dev/socket/mysock"; | | 140 | + struct sockaddr_un addr = {.sun_family = AF_UNIX}; | | 141 | + strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)); | | 142 | + | | 143 | + int ret = connect(fd, (struct sockaddr *)(&addr), sizeof(addr)); | | 144 | + if (ret != 0) { | | 145 | + fprintf(stderr, "connect() to %s failed: %s\n", socket_path, strerror(errno)); | | 146 | + } else { | | 147 | + WriteFully(fd, &pid, 4); | | 148 | + // write_size += 4; | | 149 | + WriteFully(fd, &write_size, 4); | | 150 | + | | 151 | + if (!WriteFully(fd, ptr, write_size)) { | | 152 | + fprintf(stderr, "prepare to write %d bytes to socket\n", write_size); | | 153 | + close(fd); | | 154 | + return; | | 155 | + } | | 156 | + int frameinfo_size; | | 157 | + if (ReadFully(fd, &frameinfo_size, 4)) { | | 158 | + unsigned char frame_info[frameinfo_size + 1]; | | 159 | + if (ReadFully(fd, frame_info, frameinfo_size)) { | | 160 | + frame_info[frameinfo_size] = '\0'; | | 161 | + printf("===================================>Frame:\n"); | | 162 | + printf("%s\n", frame_info); | | 163 | + } else { | | 164 | + fprintf(stderr, "Read frame_info from socket error.\n"); | | 165 | + } | | 166 | + } else { | | 167 | + fprintf(stderr, "Read frameinfo_size from socket error.\n"); | | 168 | + } | | 169 | + // fprintf(stderr, "Read frame_info from socket placeholder.\n"); | | 170 | + close(fd); | | 171 | + } | | 172 | +} | | 173 | + | 117 | 174 | static void parse_sw(struct perf_reader *reader, void *data, int size) { | 118 | 175 | uint8_t *ptr = data; | 119 | 176 | struct perf_event_header *header = (void *)data; | 120 | 177 |
| | 178 | + // struct { | | 179 | + // struct perf_event_header header; | | 180 | + // u32 size; /* if PERF_SAMPLE_RAW */ | | 181 | + // char data[size]; /* if PERF_SAMPLE_RAW */ | | 182 | + // u64 abi; /* if PERF_SAMPLE_REGS_USER */ | | 183 | + // u64 regs[weight(mask)]; /* if PERF_SAMPLE_REGS_USER */ | | 184 | + // u64 size; /* if PERF_SAMPLE_STACK_USER */ | | 185 | + // char data[size]; /* if PERF_SAMPLE_STACK_USER */ | | 186 | + // u64 dyn_size; /* if PERF_SAMPLE_STACK_USER && size != 0 */ | | 187 | + // }; | | 188 | + | 121 | 189 | struct { | 122 | 190 | uint32_t size; | 123 | 191 | char data[0]; | @@ -136,6 +204,45 @@ static void parse_sw(struct perf_reader *reader, void *data, int size) { | 136 | 204 | return; | 137 | 205 | } | 138 | 206 |
| | 207 | + if (reader->is_unwind_call_stack) { | | 208 | + // 这里要和bpf代码里面传递的数据结构一致 | | 209 | + int pid = *(int *)raw->data; | | 210 | + // 这里的 size 是 perf_submit 传递的那个大小 | | 211 | + // 这里的 data 是整个传递的数据 也就是 PERF_SAMPLE_RAW 部分 | | 212 | + // 到此处 ptr 也就是 PERF_SAMPLE_RAW 结尾 | | 213 | + // 也就是说 write_size 是 PERF_SAMPLE_REGS_USER 和 PERF_SAMPLE_STACK_USER 的整个大小 | | 214 | + int write_size = ((uint8_t *)data + size) - ptr; | | 215 | + print_frame_info(pid, ptr, write_size); | | 216 | + } | | 217 | + | | 218 | + // enum perf_sample_regs_abi { | | 219 | + // PERF_SAMPLE_REGS_ABI_NONE = 0, | | 220 | + // PERF_SAMPLE_REGS_ABI_32 = 1, | | 221 | + // PERF_SAMPLE_REGS_ABI_64 = 2, | | 222 | + // }; | | 223 | + | | 224 | + // struct { | | 225 | + // uint64_t abi; | | 226 | + // uint64_t regs[33]; | | 227 | + // } *user_regs = NULL; | | 228 | + // user_regs = (void *)ptr; | | 229 | + // ptr += 8 + 8 * 33; | | 230 | + // fprintf(stderr, "[%s] pid=%d abi=%lu\n", __FUNCTION__, *(int *)raw->data, user_regs->abi); | | 231 | + | | 232 | + // struct { | | 233 | + // uint64_t size; | | 234 | + // // 这个是 bcc 里面预设的固定值 sample_stack_user | | 235 | + // char data[16384]; | | 236 | + // uint64_t dyn_size; | | 237 | + // } *user_stack = NULL; | | 238 | + // user_stack = (void *)ptr; | | 239 | + // ptr += 8 + user_stack->size + 8; | | 240 | + // fprintf(stderr, "[%s] size=%lu dyn_size=%lu\n", __FUNCTION__, user_stack->size, user_stack->dyn_size); | | 241 | + | | 242 | + if (reader->is_unwind_call_stack) { | | 243 | + // hack ptr | | 244 | + ptr += 16672; | | 245 | + } | 139 | 246 | // sanity check | 140 | 247 | if (ptr != (uint8_t *)data + size) { | 141 | 248 | fprintf(stderr, "%s: extra data at end of sample\n", __FUNCTION__); | |
Collapse file Copy file name to clipboardExpand all lines: src/cc/perf_reader.h +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | extern "C" { | 24 | 24 | #endif | 25 | 25 |
| 26 | | -struct perf_reader; | | 26 | +struct perf_reader { | | 27 | + perf_reader_raw_cb raw_cb; | | 28 | + perf_reader_lost_cb lost_cb; | | 29 | + void *cb_cookie; // to be returned in the cb | | 30 | + void *buf; // for keeping segmented data | | 31 | + size_t buf_size; | | 32 | + void *base; | | 33 | + int rb_use_state; | | 34 | + pid_t rb_read_tid; | | 35 | + int page_size; | | 36 | + int page_cnt; | | 37 | + int fd; | | 38 | + bool is_unwind_call_stack; | | 39 | +}; | 27 | 40 |
| 28 | 41 | struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb, | 29 | 42 | perf_reader_lost_cb lost_cb, | |
Collapse file Copy file name to clipboardExpand all lines: src/python/bcc/libbcc.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,6 +141,7 @@ class bcc_perf_buffer_opts(ct.Structure): | 141 | 141 | ('pid', ct.c_int), | 142 | 142 | ('cpu', ct.c_int), | 143 | 143 | ('wakeup_events', ct.c_int), | | 144 | + ('unwind_call_stack', ct.c_int), | 144 | 145 | ] | 145 | 146 |
| 146 | 147 | lib.bpf_open_perf_buffer_opts.restype = ct.c_void_p | |
Collapse file Copy file name to clipboardExpand all lines: src/python/bcc/table.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -960,7 +960,7 @@ def event(self, data): | 960 | 960 | self._event_class = _get_event_class(self) | 961 | 961 | return ct.cast(data, ct.POINTER(self._event_class)).contents | 962 | 962 |
| 963 | | - def open_perf_buffer(self, callback, page_cnt=8, lost_cb=None, wakeup_events=1): | | 963 | + def open_perf_buffer(self, callback, page_cnt=8, lost_cb=None, wakeup_events=1, unwind_call_stack=0): | 964 | 964 | """open_perf_buffers(callback) | 965 | 965 |
| 966 | 966 | Opens a set of per-cpu ring buffer to receive custom perf event | @@ -974,9 +974,9 @@ def open_perf_buffer(self, callback, page_cnt=8, lost_cb=None, wakeup_events=1): | 974 | 974 | raise Exception("Perf buffer page_cnt must be a power of two") | 975 | 975 |
| 976 | 976 | for i in get_online_cpus(): | 977 | | - self._open_perf_buffer(i, callback, page_cnt, lost_cb, wakeup_events) | | 977 | + self._open_perf_buffer(i, callback, page_cnt, lost_cb, wakeup_events, unwind_call_stack) | 978 | 978 |
| 979 | | - def _open_perf_buffer(self, cpu, callback, page_cnt, lost_cb, wakeup_events): | | 979 | + def _open_perf_buffer(self, cpu, callback, page_cnt, lost_cb, wakeup_events, unwind_call_stack): | 980 | 980 | def raw_cb_(_, data, size): | 981 | 981 | try: | 982 | 982 | callback(cpu, data, size) | @@ -999,6 +999,7 @@ def lost_cb_(_, lost): | 999 | 999 | opts.pid = -1 | 1000 | 1000 | opts.cpu = cpu | 1001 | 1001 | opts.wakeup_events = wakeup_events | | 1002 | + opts.unwind_call_stack = unwind_call_stack | 1002 | 1003 | reader = lib.bpf_open_perf_buffer_opts(fn, lost_fn, None, page_cnt, ct.byref(opts)) | 1003 | 1004 | if not reader: | 1004 | 1005 | raise Exception("Could not open perf buffer") | |
|