GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — unwind patch for bcc · SeeFlowerX/unwindbcc@f654863

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • cc
      • libbpf.c
      • libbpf.h
      • perf_reader.c
      • perf_reader.h
    • python/bcc
      • libbcc.py
      • table.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/cc/libbpf.c‎

Copy file name to clipboardExpand all lines: src/cc/libbpf.c
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,15 @@ void * bpf_open_perf_buffer_opts(perf_reader_raw_cb raw_cb,
14501450
attr.sample_type = PERF_SAMPLE_RAW;
14511451
attr.sample_period = 1;
14521452
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+
14531462
pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC);
14541463
if (pfd < 0) {
14551464
fprintf(stderr, "perf_event_open: %s\n", strerror(errno));
Collapse file

‎src/cc/libbpf.h‎

Copy file name to clipboardExpand all lines: src/cc/libbpf.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct bcc_perf_buffer_opts {
3939
int pid;
4040
int cpu;
4141
int wakeup_events;
42+
int unwind_call_stack;
4243
};
4344

4445
int bcc_create_map(enum bpf_map_type map_type, const char *name,
Collapse file

‎src/cc/perf_reader.c‎

Copy file name to clipboardExpand all lines: src/cc/perf_reader.c
+121-14Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <sys/mman.h>
2626
#include <sys/types.h>
2727
#include <unistd.h>
28+
#include <errno.h>
29+
#include <arpa/inet.h>
30+
#include <sys/socket.h>
31+
#include <linux/un.h>
2832
#include <linux/types.h>
2933
#include <linux/perf_event.h>
3034

@@ -37,20 +41,6 @@ enum {
3741
RB_USED_IN_READ = 2, // used in read
3842
};
3943

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-
5444
struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb,
5545
perf_reader_lost_cb lost_cb,
5646
void *cb_cookie, int page_cnt) {
@@ -63,6 +53,7 @@ struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb,
6353
reader->fd = -1;
6454
reader->page_size = getpagesize();
6555
reader->page_cnt = page_cnt;
56+
reader->is_unwind_call_stack = false;
6657
return reader;
6758
}
6859

@@ -114,10 +105,87 @@ struct perf_sample_trace_kprobe {
114105
uint64_t ip;
115106
};
116107

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+
117174
static void parse_sw(struct perf_reader *reader, void *data, int size) {
118175
uint8_t *ptr = data;
119176
struct perf_event_header *header = (void *)data;
120177

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+
121189
struct {
122190
uint32_t size;
123191
char data[0];
@@ -136,6 +204,45 @@ static void parse_sw(struct perf_reader *reader, void *data, int size) {
136204
return;
137205
}
138206

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+
}
139246
// sanity check
140247
if (ptr != (uint8_t *)data + size) {
141248
fprintf(stderr, "%s: extra data at end of sample\n", __FUNCTION__);
Collapse file

‎src/cc/perf_reader.h‎

Copy file name to clipboardExpand all lines: src/cc/perf_reader.h
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@
2323
extern "C" {
2424
#endif
2525

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+
};
2740

2841
struct perf_reader * perf_reader_new(perf_reader_raw_cb raw_cb,
2942
perf_reader_lost_cb lost_cb,
Collapse file

‎src/python/bcc/libbcc.py‎

Copy file name to clipboardExpand all lines: src/python/bcc/libbcc.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class bcc_perf_buffer_opts(ct.Structure):
141141
('pid', ct.c_int),
142142
('cpu', ct.c_int),
143143
('wakeup_events', ct.c_int),
144+
('unwind_call_stack', ct.c_int),
144145
]
145146

146147
lib.bpf_open_perf_buffer_opts.restype = ct.c_void_p
Collapse file

‎src/python/bcc/table.py‎

Copy file name to clipboardExpand all lines: src/python/bcc/table.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def event(self, data):
960960
self._event_class = _get_event_class(self)
961961
return ct.cast(data, ct.POINTER(self._event_class)).contents
962962

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):
964964
"""open_perf_buffers(callback)
965965
966966
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):
974974
raise Exception("Perf buffer page_cnt must be a power of two")
975975

976976
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)
978978

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):
980980
def raw_cb_(_, data, size):
981981
try:
982982
callback(cpu, data, size)
@@ -999,6 +999,7 @@ def lost_cb_(_, lost):
999999
opts.pid = -1
10001000
opts.cpu = cpu
10011001
opts.wakeup_events = wakeup_events
1002+
opts.unwind_call_stack = unwind_call_stack
10021003
reader = lib.bpf_open_perf_buffer_opts(fn, lost_fn, None, page_cnt, ct.byref(opts))
10031004
if not reader:
10041005
raise Exception("Could not open perf buffer")

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.