|
| 1 | +#include "BacktraceScreen.h" |
| 2 | + |
| 3 | +#include <ctype.h> |
| 4 | +#include <execinfo.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | +#include <sys/ptrace.h> |
| 8 | +#include <sys/types.h> |
| 9 | +#include <sys/wait.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +#include <libunwind-ptrace.h> |
| 13 | + |
| 14 | +#include "FunctionBar.h" |
| 15 | +#include "InfoScreen.h" |
| 16 | +#include "Object.h" |
| 17 | +#include "Panel.h" |
| 18 | +#include "Process.h" |
| 19 | +#include "Vector.h" |
| 20 | +#include "XUtils.h" |
| 21 | + |
| 22 | +static const char* const BackTraceScreenFunctions[] = {"Refresh", "Done", NULL}; |
| 23 | +static const char* const BackTraceScreenKeys[] = {"F3", "Esc"}; |
| 24 | +static const int BackTraceScreenEvents[] = {KEY_F(3), 27}; |
| 25 | + |
| 26 | +static void BacktraceScreen_getMaxWidth(size_t* maxFunctionWidth, size_t* maxPathWidth, Backtrace* backtraces, size_t maxBacktraces) { |
| 27 | + *maxFunctionWidth = 0; |
| 28 | + *maxPathWidth = 0; |
| 29 | + |
| 30 | + for (size_t i = 0; i < maxBacktraces; i++) { |
| 31 | + Backtrace* backtrace = &backtraces[i]; |
| 32 | + for (size_t j = 0; j < backtrace->nbFrames; j++) { |
| 33 | + Frame* frame = &backtrace->frames[j]; |
| 34 | + size_t lenFunction = strlen(frame->function); |
| 35 | + |
| 36 | + if (lenFunction > *maxFunctionWidth) |
| 37 | + *maxFunctionWidth = lenFunction; |
| 38 | + |
| 39 | + size_t lenPath = strlen(frame->path); |
| 40 | + if (lenPath > *maxPathWidth) |
| 41 | + *maxPathWidth = lenPath; |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +static void BacktraceScreen_freeFrame(Frame* frame) { |
| 47 | + free(frame->function); |
| 48 | + free(frame->path); |
| 49 | +} |
| 50 | + |
| 51 | +static void BacktraceScreen_freeBacktraces(Backtrace* backtraces) { |
| 52 | + if (!backtraces) |
| 53 | + return; |
| 54 | + |
| 55 | + Backtrace* backtraceArray = backtraces; |
| 56 | + for (size_t backtraceIndex = 0; backtraceIndex < MAX_BACKTRACE; backtraceIndex++) { |
| 57 | + Backtrace* currentBacktrace = &backtraces[backtraceIndex]; |
| 58 | + for (size_t frameIndex = 0; frameIndex < currentBacktrace->nbFrames; frameIndex++) { |
| 59 | + Frame* currentFrame = ¤tBacktrace->frames[frameIndex]; |
| 60 | + BacktraceScreen_freeFrame(currentFrame); |
| 61 | + } |
| 62 | + } |
| 63 | + free(backtraceArray); |
| 64 | +} |
| 65 | + |
| 66 | +BacktraceScreen* BacktraceScreen_new(const Process* process) { |
| 67 | + BacktraceScreen* this = xCalloc(1, sizeof(BacktraceScreen)); |
| 68 | + Object_setClass(this, Class(BacktraceScreen)); |
| 69 | + FunctionBar* functionBar = FunctionBar_new(BackTraceScreenFunctions, BackTraceScreenKeys, BackTraceScreenEvents); |
| 70 | + CRT_disableDelay(); |
| 71 | + return (BacktraceScreen*) InfoScreen_init(&this->super, process, functionBar, LINES - 2, "NB ADDRESS NAME PATH "); |
| 72 | +} |
| 73 | + |
| 74 | +void BacktraceScreen_delete(Object* cast) { |
| 75 | + BacktraceScreen* this = (BacktraceScreen*) cast; |
| 76 | + BacktraceScreen_freeBacktraces(this->backtraces); |
| 77 | + CRT_enableDelay(); |
| 78 | + free(InfoScreen_done((InfoScreen*)this)); |
| 79 | +} |
| 80 | + |
| 81 | +static void BacktraceScreen_draw(InfoScreen* this) { |
| 82 | + if (Process_isThread(this->process)) |
| 83 | + InfoScreen_drawTitled(this, "Backtrace of thread %d - %s", this->process->pid, Process_getCommand(this->process)); |
| 84 | + else |
| 85 | + InfoScreen_drawTitled(this, "Backtrace of process %d - %s", this->process->pid, Process_getCommand(this->process)); |
| 86 | +} |
| 87 | + |
| 88 | +static void BacktraceScreen_print(BacktraceScreen* backtraceScreen) { |
| 89 | + const Process* process = backtraceScreen->super.process; |
| 90 | + bool isThread = Process_isThread(process); |
| 91 | + |
| 92 | + size_t maxFunctionWidth = 0; |
| 93 | + size_t maxPathWidth = 0; |
| 94 | + BacktraceScreen_getMaxWidth(&maxFunctionWidth, &maxPathWidth, backtraceScreen->backtraces, backtraceScreen->nbBacktraces); |
| 95 | + |
| 96 | + char* header = NULL; |
| 97 | + xAsprintf(&header, " NB ADDRESS NAME %*s PATH", (int)(maxFunctionWidth - sizeof("NAME")), " "); |
| 98 | + Panel_setHeader(backtraceScreen->super.display, header); |
| 99 | + |
| 100 | + free(header); |
| 101 | + |
| 102 | + for (size_t i = 0; i < backtraceScreen->nbBacktraces; i++) { |
| 103 | + Backtrace* backtrace = &backtraceScreen->backtraces[i]; |
| 104 | + char* tgidStr = NULL; |
| 105 | + if (!isThread) { |
| 106 | + xAsprintf(&tgidStr, "TID: %d", backtrace->tgid); |
| 107 | + InfoScreen_addLine(&backtraceScreen->super, tgidStr); |
| 108 | + } |
| 109 | + |
| 110 | + if (isThread && backtrace->tgid != process->tgid) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + for (size_t j = 0; j < backtrace->nbFrames; j++) { |
| 115 | + Frame* frame = &backtrace->frames[j]; |
| 116 | + |
| 117 | + char* frameStr = NULL; |
| 118 | + xAsprintf(&frameStr, " #%zu 0x%016zx %-*s %s", frame->nb, frame->address, (int)maxFunctionWidth, frame->function, frame->path); |
| 119 | + InfoScreen_addLine(&backtraceScreen->super, frameStr); |
| 120 | + free(frameStr); |
| 121 | + } |
| 122 | + free(tgidStr); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +static void BacktraceScreen_getBacktraceFromProcess(const Process *process, Backtrace *backtraces, size_t maxBacktraces) { |
| 127 | + pid_t pid = Process_isThread(process) ? process->tgid : process->pid; |
| 128 | + |
| 129 | + unw_addr_space_t addrSpace = unw_create_addr_space(&_UPT_accessors, 0); |
| 130 | + ptrace(PTRACE_ATTACH, pid, 0, 0); |
| 131 | + void *context = _UPT_create(pid); |
| 132 | + unw_cursor_t cursor; |
| 133 | + unw_init_remote(&cursor, addrSpace, context); |
| 134 | + |
| 135 | + do { |
| 136 | + unw_word_t offset = 0; |
| 137 | + |
| 138 | + char symbolName[4096]; |
| 139 | + if (unw_get_proc_name(&cursor, symbolName, sizeof(symbolName), &offset) == 0) { |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + } while (unw_step(&cursor)); |
| 144 | + |
| 145 | + |
| 146 | + _UPT_destroy(context); |
| 147 | + unw_destroy_addr_space(addrSpace); |
| 148 | + ptrace(PTRACE_DETACH, pid); |
| 149 | +} |
| 150 | + |
| 151 | +static void BacktraceScreen_scan(InfoScreen* super) { |
| 152 | + BacktraceScreen* this = (BacktraceScreen*)super; |
| 153 | + this->backtraces = xCalloc(MAX_BACKTRACE, sizeof(Backtrace)); |
| 154 | + |
| 155 | + BacktraceScreen_print(this); |
| 156 | +} |
| 157 | + |
| 158 | +static bool BacktraceScreen_onKey(InfoScreen* super, int character) { |
| 159 | + BacktraceScreen* this = (BacktraceScreen*)super; |
| 160 | + switch (character) { |
| 161 | + case 'r': |
| 162 | + case KEY_F(3): |
| 163 | + BacktraceScreen_freeBacktraces(this->backtraces); |
| 164 | + Panel_prune(super->display); |
| 165 | + Vector_prune(super->lines); |
| 166 | + BacktraceScreen_scan(super); |
| 167 | + return true; |
| 168 | + } |
| 169 | + return false; |
| 170 | +} |
| 171 | + |
| 172 | +const InfoScreenClass BacktraceScreen_class = { |
| 173 | + .super = { |
| 174 | + .extends = Class(Object), |
| 175 | + .delete = BacktraceScreen_delete, |
| 176 | + }, |
| 177 | + .scan = BacktraceScreen_scan, |
| 178 | + .draw = BacktraceScreen_draw, |
| 179 | + .onKey = BacktraceScreen_onKey, |
| 180 | +}; |
0 commit comments