|
| 1 | +/* |
| 2 | +htop - Action.c |
| 3 | +(C) 2015 Hisham H. Muhammad |
| 4 | +Released under the GNU GPLv2+, see the COPYING file |
| 5 | +in the source distribution for its full text. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "BacktraceScreen.h" |
| 9 | + |
| 10 | +#if (defined(HTOP_LINUX) && defined(HAVE_LIBUNWIND_PTRACE)) |
| 11 | + |
| 12 | +#include <sys/wait.h> |
| 13 | + |
| 14 | +#include "CRT.h" |
| 15 | +#include "Object.h" |
| 16 | +#include "Panel.h" |
| 17 | +#include "Process.h" |
| 18 | +#include "RichString.h" |
| 19 | +#include "XUtils.h" |
| 20 | +#include "errno.h" |
| 21 | + |
| 22 | +#ifdef HAVE_LIBIBERTY |
| 23 | +#include <libiberty/demangle.h> |
| 24 | +#endif |
| 25 | + |
| 26 | +#include <libunwind-ptrace.h> |
| 27 | +#include <sys/ptrace.h> |
| 28 | + |
| 29 | +#define MAX_FRAME 256 |
| 30 | + |
| 31 | +static const char* const BacktraceScreenFunctions[] = {"Done ", NULL}; |
| 32 | + |
| 33 | +static const char* const BacktraceScreenKeys[] = {"Esc"}; |
| 34 | + |
| 35 | +static const int BacktraceScreenEvents[] = {27}; |
| 36 | + |
| 37 | +static void Frame_display(const Object* super, RichString* out) { |
| 38 | + const Frame* const frame = (const Frame*)super; |
| 39 | + if (frame->isError) { |
| 40 | + RichString_appendAscii(out, CRT_colors[DEFAULT_COLOR], frame->error); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + char bufferNumberOfFrame[16] = {'\0'}; |
| 45 | + int len = snprintf(bufferNumberOfFrame, sizeof(bufferNumberOfFrame), "#%-3d ", frame->index); |
| 46 | + RichString_appendnAscii(out, CRT_colors[DYNAMIC_GREEN], bufferNumberOfFrame, len); |
| 47 | + |
| 48 | + char bufferAddress[32] = {'\0'}; |
| 49 | + len = snprintf(bufferAddress, sizeof(bufferAddress), "0x%016zx ", frame->address); |
| 50 | + RichString_appendnAscii(out, CRT_colors[DYNAMIC_BLUE], bufferAddress, len); |
| 51 | + |
| 52 | + RichString_appendAscii(out, CRT_colors[DEFAULT_COLOR], frame->functionName); |
| 53 | + if (frame->isSignalFrame) { |
| 54 | + RichString_appendAscii(out, CRT_colors[DYNAMIC_RED], " signal frame"); |
| 55 | + } |
| 56 | + |
| 57 | + char bufferFrameOffset[16] = {'\0'}; |
| 58 | + len = snprintf(bufferFrameOffset, sizeof(bufferFrameOffset), "+%zu", frame->offset); |
| 59 | + RichString_appendAscii(out, CRT_colors[DYNAMIC_YELLOW], bufferFrameOffset); |
| 60 | +} |
| 61 | + |
| 62 | +static void BacktracePanel_getFrames(BacktracePanel* this) { |
| 63 | + Panel* super = (Panel*) this; |
| 64 | + |
| 65 | + unw_addr_space_t addrSpace = unw_create_addr_space(&_UPT_accessors, 0); |
| 66 | + if (!addrSpace) { |
| 67 | + xAsprintf(&this->error, "Unable to init libunwind."); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const pid_t pid = Process_getPid(this->process); |
| 72 | + |
| 73 | + if (pid == 0) { |
| 74 | + xAsprintf(&this->error, "Unable to get the pid"); |
| 75 | + goto addr_space_error; |
| 76 | + } |
| 77 | + |
| 78 | + if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) { |
| 79 | + xAsprintf(&this->error, "ptrace: %s", strerror(errno)); |
| 80 | + goto addr_space_error; |
| 81 | + } |
| 82 | + wait(NULL); |
| 83 | + |
| 84 | + struct UPT_info* context = _UPT_create(pid); |
| 85 | + if (!context) { |
| 86 | + xAsprintf(&this->error, "Unable to init backtrace panel."); |
| 87 | + goto ptrace_error; |
| 88 | + } |
| 89 | + |
| 90 | + unw_cursor_t cursor; |
| 91 | + int ret = unw_init_remote(&cursor, addrSpace, context); |
| 92 | + if (ret < 0) { |
| 93 | + xAsprintf(&this->error, "libunwind cursor: ret=%d", ret); |
| 94 | + goto context_error; |
| 95 | + } |
| 96 | + |
| 97 | + int index = 0; |
| 98 | + do { |
| 99 | + char procName[256] = "?"; |
| 100 | + unw_word_t offset; |
| 101 | + unw_word_t pc; |
| 102 | + |
| 103 | + if (unw_get_proc_name(&cursor, procName, sizeof(procName), &offset) == 0) { |
| 104 | + ret = unw_get_reg(&cursor, UNW_REG_IP, &pc); |
| 105 | + if (ret < 0) { |
| 106 | + xAsprintf(&this->error, "unable to get register rip : %d", ret); |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + Frame* frame = Frame_new(); |
| 111 | + frame->index = index; |
| 112 | + frame->address = pc; |
| 113 | + frame->offset = offset; |
| 114 | + frame->isSignalFrame = unw_is_signal_frame(&cursor); |
| 115 | +#if HAVE_LIBIBERTY |
| 116 | + char* demangledName = cplus_demangle(procName, |
| 117 | + DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE | DMGL_RET_POSTFIX); |
| 118 | + if (demangledName == NULL) { |
| 119 | + xAsprintf(&frame->functionName, "%s", procName); |
| 120 | + } else { |
| 121 | + xAsprintf(&frame->functionName, "%s", demangledName); |
| 122 | + free(demangledName); |
| 123 | + } |
| 124 | +#else |
| 125 | + xAsprintf(&frame->functionName, "%s", procName); |
| 126 | +#endif |
| 127 | + Panel_add(super, (Object*)frame); |
| 128 | + } |
| 129 | + index++; |
| 130 | + } while (unw_step(&cursor) > 0 && index < MAX_FRAME); |
| 131 | + |
| 132 | +context_error: |
| 133 | + _UPT_destroy(context); |
| 134 | + |
| 135 | +ptrace_error: |
| 136 | + ptrace(PTRACE_DETACH, pid, 0, 0); |
| 137 | + |
| 138 | +addr_space_error: |
| 139 | + unw_destroy_addr_space(addrSpace); |
| 140 | +} |
| 141 | + |
| 142 | +BacktracePanel* BacktracePanel_new(const Process* process) { |
| 143 | + BacktracePanel* this = CallocThis(BacktracePanel); |
| 144 | + this->process = process; |
| 145 | + |
| 146 | + Panel* super = (Panel*) this; |
| 147 | + Panel_init(super, 1, 1, 1, 1, Class(Frame), true, FunctionBar_new(BacktraceScreenFunctions, BacktraceScreenKeys, BacktraceScreenEvents)); |
| 148 | + BacktracePanel_getFrames(this); |
| 149 | + if (this->error) { |
| 150 | + Panel_prune(super); |
| 151 | + |
| 152 | + Frame* errorFrame = Frame_new(); |
| 153 | + errorFrame->error = xStrdup(this->error); |
| 154 | + errorFrame->isError = true; |
| 155 | + Panel_add(super, (Object*)errorFrame); |
| 156 | + } |
| 157 | + |
| 158 | + char* header = NULL; |
| 159 | + xAsprintf(&header, "Backtrace of '%s' (%d)", process->procComm, Process_getPid(process)); |
| 160 | + Panel_setHeader(super, header); |
| 161 | + free(header); |
| 162 | + return this; |
| 163 | +} |
| 164 | + |
| 165 | +Frame* Frame_new(void) { |
| 166 | + Frame* this = CallocThis(Frame); |
| 167 | + return this; |
| 168 | +} |
| 169 | + |
| 170 | +static int Frame_compare(const void* object1, const void* object2) { |
| 171 | + const Frame* frame1 = (const Frame*)object1; |
| 172 | + const Frame* frame2 = (const Frame*)object2; |
| 173 | + return String_eq(frame1->functionName, frame2->functionName); |
| 174 | +} |
| 175 | + |
| 176 | +static void Frame_delete(Object* object) { |
| 177 | + Frame* this = (Frame*)object; |
| 178 | + if (this->functionName) { |
| 179 | + free(this->functionName); |
| 180 | + } |
| 181 | + |
| 182 | + if (this->isError && this->error) { |
| 183 | + free(this->error); |
| 184 | + } |
| 185 | + |
| 186 | + free(this); |
| 187 | +} |
| 188 | + |
| 189 | +void BacktracePanel_delete(Object* object) { |
| 190 | + BacktracePanel* this = (BacktracePanel*)object; |
| 191 | + if (this->error) { |
| 192 | + free(this->error); |
| 193 | + } |
| 194 | + Panel_delete(object); |
| 195 | +} |
| 196 | + |
| 197 | +const PanelClass BacktracePanel_class = { |
| 198 | + .super = { |
| 199 | + .extends = Class(Panel), |
| 200 | + .delete = BacktracePanel_delete, |
| 201 | + }, |
| 202 | +}; |
| 203 | + |
| 204 | +const ObjectClass Frame_class = { |
| 205 | + .extends = Class(Object), |
| 206 | + .compare = Frame_compare, |
| 207 | + .delete = Frame_delete, |
| 208 | + .display = Frame_display, |
| 209 | +}; |
| 210 | + |
| 211 | +#endif /* HTOP_LINUX && HAVE_LIBUNWIND_PTRACE */ |
0 commit comments