print_trace

This commit is contained in:
zy
2023-11-22 21:14:15 -05:00
parent 9cd12a063a
commit 0ae425d99e
4 changed files with 145 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ CFLAGS = -Wall
PROG = helloworld
HPTEST = hptest
USTACK = userstack
SPID = stack-pid
UDIR = $(PWD)/source/uapi
MDIR := $(PWD)/source
@@ -18,7 +19,7 @@ OUTPUT_DIR = $(PWD)/build
# obj-m := kernel/$(KMOD).o
# $(KMOD)-objs := kernel/monitor_kernel.o
all: $(PROG) $(HPTEST) $(USTACK) module
all: $(PROG) $(HPTEST) $(USTACK) $(SPID) module
$(PROG): $(TDIR)/helloworld.c
mkdir -p $(OUTPUT_DIR)
@@ -30,7 +31,11 @@ $(HPTEST): $(TDIR)/hptest.c
$(USTACK): $(TDIR)/userstack.c
mkdir -p $(OUTPUT_DIR)
$(CC) $(CFLAGS) -o $(OUTPUT_DIR)/$(USTACK) $(TDIR)/userstack.c
$(CC) -g $(CFLAGS) -o $(OUTPUT_DIR)/$(USTACK) $(TDIR)/userstack.c -lunwind -lunwind-x86_64
$(SPID): $(TDIR)/stack-pid.c
mkdir -p $(OUTPUT_DIR)
$(CC) $(CFLAGS) -o $(OUTPUT_DIR)/$(SPID) $(TDIR)/stack-pid.c -lunwind -lunwind-x86_64 -lunwind-ptrace
module:
make -C $(KDIR) M=$(MDIR) modules

52
testcase/backtrace.c Normal file
View File

@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <execinfo.h>
#include <elfutils/libdwfl.h>
#define MAX_STACK_FRAMES 64
static void *stack_traces[MAX_STACK_FRAMES];
void print_trace() {
int stack_depth = backtrace(stack_traces, MAX_STACK_FRAMES);
Dwfl_Callbacks callbacks = {
.find_elf = dwfl_linux_proc_find_elf,
.find_debuginfo = dwfl_standard_find_debuginfo,
.debuginfo_path = NULL
};
Dwfl *dwfl = dwfl_begin(&callbacks);
dwfl_linux_proc_report(dwfl, getpid());
dwfl_report_end(dwfl, NULL, NULL);
for (int i = 0; i < stack_depth; ++i) {
const char *modname;
const char *symname;
GElf_Off offset;
Dwfl_Module *mod = dwfl_addrmodule(dwfl, (uintptr_t)stack_traces[i]);
Dwfl_Line *line = dwfl_module_getsrc(mod, (uintptr_t)stack_traces[i]);
const char *src;
int lineno, colno;
if (line) {
src = dwfl_lineinfo(mod, line, &lineno, &colno, &modname, &symname);
printf("%s (%d) %s\n", src, lineno, symname);
}
}
dwfl_end(dwfl);
}
void foo() {
print_trace();
}
void bar() {
foo();
}
int main(int argc, char **argv) {
bar();
return 0;
}

54
testcase/stack-pid.c Normal file
View File

@@ -0,0 +1,54 @@
#include <sys/ptrace.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <libunwind-ptrace.h>
void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
int main(int argc, char **argv)
{
if (argc != 2)
die("USAGE: unwind-pid <pid>\n");
unw_addr_space_t as = unw_create_addr_space(&_UPT_accessors, 0);
pid_t pid = atoi(argv[1]);
if (ptrace(PTRACE_ATTACH, pid, 0, 0) != 0)
die("ERROR: cannot attach to %d\n", pid);
void *context = _UPT_create(pid);
unw_cursor_t cursor;
if (unw_init_remote(&cursor, as, context) != 0)
die("ERROR: cannot initialize cursor for remote unwinding\n");
do {
unw_word_t offset, pc;
char sym[4096];
if (unw_get_reg(&cursor, UNW_REG_IP, &pc))
die("ERROR: cannot read program counter\n");
printf("0x%lx: ", pc);
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0)
printf("(%s+0x%lx)\n", sym, offset);
else
printf("-- no symbol name found\n");
} while (unw_step(&cursor) > 0);
_UPT_destroy(context);
(void) ptrace(PTRACE_DETACH, pid, 0, 0);
return 0;
}

View File

@@ -1,15 +1,46 @@
#include <stdio.h>
#include <libunwind.h>
#include <unistd.h>
void customFunction1(int n);
void customFunction2(int n);
void customFunction3(int n);
// Call this function to get a backtrace.
void backtrace() {
unw_cursor_t cursor;
unw_context_t context;
// Initialize cursor to current frame for local unwinding.
unw_getcontext(&context);
unw_init_local(&cursor, &context);
// Unwind frames one by one, going up the frame stack.
while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
if (pc == 0) {
break;
}
printf("0x%lx:", pc);
char sym[256];
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
printf(" (%s+0x%lx)\n", sym, offset);
} else {
printf(" -- error: unable to obtain symbol name for this frame\n");
}
}
}
void customFunction1(int n) {
if(n <= 0) {
printf("End of recursion\n");
// output backtrace
printf("pid: %d\n", getpid());
backtrace();
while (1) {
// sleep(1);
sleep(1);
} // never return, keep stack
return;
} else {