newt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. /*
  5. * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
  6. * the build if it isn't defined. Use the equivalent one that glibc
  7. * has on features.h.
  8. */
  9. #include <features.h>
  10. #ifndef HAVE_LONG_LONG
  11. #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
  12. #endif
  13. #include <slang.h>
  14. #include <signal.h>
  15. #include <stdlib.h>
  16. #include <elf.h>
  17. #include <newt.h>
  18. #include <sys/ttydefaults.h>
  19. #include "cache.h"
  20. #include "hist.h"
  21. #include "pstack.h"
  22. #include "session.h"
  23. #include "sort.h"
  24. #include "symbol.h"
  25. #include "ui/browser.h"
  26. #include "ui/helpline.h"
  27. #if SLANG_VERSION < 20104
  28. #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
  29. #define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
  30. #define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
  31. (char *)fg, (char *)bg)
  32. #else
  33. #define slsmg_printf SLsmg_printf
  34. #define slsmg_write_nstring SLsmg_write_nstring
  35. #define sltt_set_color SLtt_set_color
  36. #endif
  37. newtComponent newt_form__new(void);
  38. static int ui_entry__read(const char *title, char *bf, size_t size, int width)
  39. {
  40. struct newtExitStruct es;
  41. newtComponent form, entry;
  42. const char *result;
  43. int err = -1;
  44. newtCenteredWindow(width, 1, title);
  45. form = newtForm(NULL, NULL, 0);
  46. if (form == NULL)
  47. return -1;
  48. entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
  49. if (entry == NULL)
  50. goto out_free_form;
  51. newtFormAddComponent(form, entry);
  52. newtFormAddHotKey(form, NEWT_KEY_ENTER);
  53. newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
  54. newtFormAddHotKey(form, NEWT_KEY_LEFT);
  55. newtFormAddHotKey(form, CTRL('c'));
  56. newtFormRun(form, &es);
  57. if (result != NULL) {
  58. strncpy(bf, result, size);
  59. err = 0;
  60. }
  61. out_free_form:
  62. newtPopWindow();
  63. newtFormDestroy(form);
  64. return 0;
  65. }
  66. static char browser__last_msg[1024];
  67. int browser__show_help(const char *format, va_list ap)
  68. {
  69. int ret;
  70. static int backlog;
  71. ret = vsnprintf(browser__last_msg + backlog,
  72. sizeof(browser__last_msg) - backlog, format, ap);
  73. backlog += ret;
  74. if (browser__last_msg[backlog - 1] == '\n') {
  75. ui_helpline__puts(browser__last_msg);
  76. newtRefresh();
  77. backlog = 0;
  78. }
  79. return ret;
  80. }
  81. static void newt_form__set_exit_keys(newtComponent self)
  82. {
  83. newtFormAddHotKey(self, NEWT_KEY_LEFT);
  84. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  85. newtFormAddHotKey(self, 'Q');
  86. newtFormAddHotKey(self, 'q');
  87. newtFormAddHotKey(self, CTRL('c'));
  88. }
  89. newtComponent newt_form__new(void)
  90. {
  91. newtComponent self = newtForm(NULL, NULL, 0);
  92. if (self)
  93. newt_form__set_exit_keys(self);
  94. return self;
  95. }
  96. static int popup_menu(int argc, char * const argv[])
  97. {
  98. struct newtExitStruct es;
  99. int i, rc = -1, max_len = 5;
  100. newtComponent listbox, form = newt_form__new();
  101. if (form == NULL)
  102. return -1;
  103. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  104. if (listbox == NULL)
  105. goto out_destroy_form;
  106. newtFormAddComponent(form, listbox);
  107. for (i = 0; i < argc; ++i) {
  108. int len = strlen(argv[i]);
  109. if (len > max_len)
  110. max_len = len;
  111. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  112. goto out_destroy_form;
  113. }
  114. newtCenteredWindow(max_len, argc, NULL);
  115. newtFormRun(form, &es);
  116. rc = newtListboxGetCurrent(listbox) - NULL;
  117. if (es.reason == NEWT_EXIT_HOTKEY)
  118. rc = -1;
  119. newtPopWindow();
  120. out_destroy_form:
  121. newtFormDestroy(form);
  122. return rc;
  123. }
  124. static int ui__help_window(const char *text)
  125. {
  126. struct newtExitStruct es;
  127. newtComponent tb, form = newt_form__new();
  128. int rc = -1;
  129. int max_len = 0, nr_lines = 0;
  130. const char *t;
  131. if (form == NULL)
  132. return -1;
  133. t = text;
  134. while (1) {
  135. const char *sep = strchr(t, '\n');
  136. int len;
  137. if (sep == NULL)
  138. sep = strchr(t, '\0');
  139. len = sep - t;
  140. if (max_len < len)
  141. max_len = len;
  142. ++nr_lines;
  143. if (*sep == '\0')
  144. break;
  145. t = sep + 1;
  146. }
  147. tb = newtTextbox(0, 0, max_len, nr_lines, 0);
  148. if (tb == NULL)
  149. goto out_destroy_form;
  150. newtTextboxSetText(tb, text);
  151. newtFormAddComponent(form, tb);
  152. newtCenteredWindow(max_len, nr_lines, NULL);
  153. newtFormRun(form, &es);
  154. newtPopWindow();
  155. rc = 0;
  156. out_destroy_form:
  157. newtFormDestroy(form);
  158. return rc;
  159. }
  160. static bool dialog_yesno(const char *msg)
  161. {
  162. /* newtWinChoice should really be accepting const char pointers... */
  163. char yes[] = "Yes", no[] = "No";
  164. return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
  165. }
  166. static void ui__error_window(const char *fmt, ...)
  167. {
  168. va_list ap;
  169. va_start(ap, fmt);
  170. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  171. va_end(ap);
  172. }
  173. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  174. {
  175. struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
  176. bool current_entry = ui_browser__is_current_entry(self, row);
  177. int width = self->width;
  178. if (ol->offset != -1) {
  179. struct hist_entry *he = self->priv;
  180. struct symbol *sym = he->ms.sym;
  181. int len = he->ms.sym->end - he->ms.sym->start;
  182. unsigned int hits = 0;
  183. double percent = 0.0;
  184. int color;
  185. struct sym_priv *priv = symbol__priv(sym);
  186. struct sym_ext *sym_ext = priv->ext;
  187. struct sym_hist *h = priv->hist;
  188. s64 offset = ol->offset;
  189. struct objdump_line *next = objdump__get_next_ip_line(self->entries, ol);
  190. while (offset < (s64)len &&
  191. (next == NULL || offset < next->offset)) {
  192. if (sym_ext) {
  193. percent += sym_ext[offset].percent;
  194. } else
  195. hits += h->ip[offset];
  196. ++offset;
  197. }
  198. if (sym_ext == NULL && h->sum)
  199. percent = 100.0 * hits / h->sum;
  200. color = ui_browser__percent_color(percent, current_entry);
  201. SLsmg_set_color(color);
  202. slsmg_printf(" %7.2f ", percent);
  203. if (!current_entry)
  204. SLsmg_set_color(HE_COLORSET_CODE);
  205. } else {
  206. int color = ui_browser__percent_color(0, current_entry);
  207. SLsmg_set_color(color);
  208. slsmg_write_nstring(" ", 9);
  209. }
  210. SLsmg_write_char(':');
  211. slsmg_write_nstring(" ", 8);
  212. if (!*ol->line)
  213. slsmg_write_nstring(" ", width - 18);
  214. else
  215. slsmg_write_nstring(ol->line, width - 18);
  216. }
  217. static char *callchain_list__sym_name(struct callchain_list *self,
  218. char *bf, size_t bfsize)
  219. {
  220. if (self->ms.sym)
  221. return self->ms.sym->name;
  222. snprintf(bf, bfsize, "%#Lx", self->ip);
  223. return bf;
  224. }
  225. int hist_entry__tui_annotate(struct hist_entry *self)
  226. {
  227. struct newtExitStruct es;
  228. struct objdump_line *pos, *n;
  229. LIST_HEAD(head);
  230. struct ui_browser browser = {
  231. .entries = &head,
  232. .refresh = ui_browser__list_head_refresh,
  233. .seek = ui_browser__list_head_seek,
  234. .write = annotate_browser__write,
  235. .priv = self,
  236. };
  237. int ret;
  238. if (self->ms.sym == NULL)
  239. return -1;
  240. if (self->ms.map->dso->annotate_warned)
  241. return -1;
  242. if (hist_entry__annotate(self, &head) < 0) {
  243. ui__error_window(browser__last_msg);
  244. return -1;
  245. }
  246. ui_helpline__push("Press <- or ESC to exit");
  247. list_for_each_entry(pos, &head, node) {
  248. size_t line_len = strlen(pos->line);
  249. if (browser.width < line_len)
  250. browser.width = line_len;
  251. ++browser.nr_entries;
  252. }
  253. browser.width += 18; /* Percentage */
  254. ui_browser__show(&browser, self->ms.sym->name);
  255. newtFormAddHotKey(browser.form, ' ');
  256. ret = ui_browser__run(&browser, &es);
  257. newtFormDestroy(browser.form);
  258. newtPopWindow();
  259. list_for_each_entry_safe(pos, n, &head, node) {
  260. list_del(&pos->node);
  261. objdump_line__free(pos);
  262. }
  263. ui_helpline__pop();
  264. return ret;
  265. }
  266. /* -------------------------------------------------------------------- */
  267. struct map_browser {
  268. struct ui_browser b;
  269. struct map *map;
  270. u16 namelen;
  271. u8 addrlen;
  272. };
  273. static void map_browser__write(struct ui_browser *self, void *nd, int row)
  274. {
  275. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  276. struct map_browser *mb = container_of(self, struct map_browser, b);
  277. bool current_entry = ui_browser__is_current_entry(self, row);
  278. int color = ui_browser__percent_color(0, current_entry);
  279. SLsmg_set_color(color);
  280. slsmg_printf("%*llx %*llx %c ",
  281. mb->addrlen, sym->start, mb->addrlen, sym->end,
  282. sym->binding == STB_GLOBAL ? 'g' :
  283. sym->binding == STB_LOCAL ? 'l' : 'w');
  284. slsmg_write_nstring(sym->name, mb->namelen);
  285. }
  286. /* FIXME uber-kludgy, see comment on cmd_report... */
  287. static u32 *symbol__browser_index(struct symbol *self)
  288. {
  289. return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
  290. }
  291. static int map_browser__search(struct map_browser *self)
  292. {
  293. char target[512];
  294. struct symbol *sym;
  295. int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
  296. if (err)
  297. return err;
  298. if (target[0] == '0' && tolower(target[1]) == 'x') {
  299. u64 addr = strtoull(target, NULL, 16);
  300. sym = map__find_symbol(self->map, addr, NULL);
  301. } else
  302. sym = map__find_symbol_by_name(self->map, target, NULL);
  303. if (sym != NULL) {
  304. u32 *idx = symbol__browser_index(sym);
  305. self->b.top = &sym->rb_node;
  306. self->b.index = self->b.top_idx = *idx;
  307. } else
  308. ui_helpline__fpush("%s not found!", target);
  309. return 0;
  310. }
  311. static int map_browser__run(struct map_browser *self, struct newtExitStruct *es)
  312. {
  313. if (ui_browser__show(&self->b, self->map->dso->long_name) < 0)
  314. return -1;
  315. ui_helpline__fpush("Press <- or ESC to exit, %s / to search",
  316. verbose ? "" : "restart with -v to use");
  317. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  318. newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
  319. if (verbose)
  320. newtFormAddHotKey(self->b.form, '/');
  321. while (1) {
  322. ui_browser__run(&self->b, es);
  323. if (es->reason != NEWT_EXIT_HOTKEY)
  324. break;
  325. if (verbose && es->u.key == '/')
  326. map_browser__search(self);
  327. else
  328. break;
  329. }
  330. newtFormDestroy(self->b.form);
  331. newtPopWindow();
  332. ui_helpline__pop();
  333. return 0;
  334. }
  335. static int map__browse(struct map *self)
  336. {
  337. struct map_browser mb = {
  338. .b = {
  339. .entries = &self->dso->symbols[self->type],
  340. .refresh = ui_browser__rb_tree_refresh,
  341. .seek = ui_browser__rb_tree_seek,
  342. .write = map_browser__write,
  343. },
  344. .map = self,
  345. };
  346. struct newtExitStruct es;
  347. struct rb_node *nd;
  348. char tmp[BITS_PER_LONG / 4];
  349. u64 maxaddr = 0;
  350. for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
  351. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  352. if (mb.namelen < pos->namelen)
  353. mb.namelen = pos->namelen;
  354. if (maxaddr < pos->end)
  355. maxaddr = pos->end;
  356. if (verbose) {
  357. u32 *idx = symbol__browser_index(pos);
  358. *idx = mb.b.nr_entries;
  359. }
  360. ++mb.b.nr_entries;
  361. }
  362. mb.addrlen = snprintf(tmp, sizeof(tmp), "%llx", maxaddr);
  363. mb.b.width += mb.addrlen * 2 + 4 + mb.namelen;
  364. return map_browser__run(&mb, &es);
  365. }
  366. /* -------------------------------------------------------------------- */
  367. struct hist_browser {
  368. struct ui_browser b;
  369. struct hists *hists;
  370. struct hist_entry *he_selection;
  371. struct map_symbol *selection;
  372. };
  373. static void hist_browser__reset(struct hist_browser *self);
  374. static int hist_browser__run(struct hist_browser *self, const char *title,
  375. struct newtExitStruct *es);
  376. static unsigned int hist_browser__refresh(struct ui_browser *self);
  377. static void ui_browser__hists_seek(struct ui_browser *self,
  378. off_t offset, int whence);
  379. static struct hist_browser *hist_browser__new(struct hists *hists)
  380. {
  381. struct hist_browser *self = zalloc(sizeof(*self));
  382. if (self) {
  383. self->hists = hists;
  384. self->b.refresh = hist_browser__refresh;
  385. self->b.seek = ui_browser__hists_seek;
  386. }
  387. return self;
  388. }
  389. static void hist_browser__delete(struct hist_browser *self)
  390. {
  391. newtFormDestroy(self->b.form);
  392. newtPopWindow();
  393. free(self);
  394. }
  395. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  396. {
  397. return self->he_selection;
  398. }
  399. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  400. {
  401. return self->he_selection->thread;
  402. }
  403. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  404. const struct dso *dso, const struct thread *thread)
  405. {
  406. int printed = 0;
  407. if (thread)
  408. printed += snprintf(bf + printed, size - printed,
  409. "Thread: %s(%d)",
  410. (thread->comm_set ? thread->comm : ""),
  411. thread->pid);
  412. if (dso)
  413. printed += snprintf(bf + printed, size - printed,
  414. "%sDSO: %s", thread ? " " : "",
  415. dso->short_name);
  416. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  417. }
  418. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  419. {
  420. struct hist_browser *browser = hist_browser__new(self);
  421. struct pstack *fstack;
  422. const struct thread *thread_filter = NULL;
  423. const struct dso *dso_filter = NULL;
  424. struct newtExitStruct es;
  425. char msg[160];
  426. int key = -1;
  427. if (browser == NULL)
  428. return -1;
  429. fstack = pstack__new(2);
  430. if (fstack == NULL)
  431. goto out;
  432. ui_helpline__push(helpline);
  433. hist_browser__title(msg, sizeof(msg), ev_name,
  434. dso_filter, thread_filter);
  435. while (1) {
  436. const struct thread *thread;
  437. const struct dso *dso;
  438. char *options[16];
  439. int nr_options = 0, choice = 0, i,
  440. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  441. browse_map = -2;
  442. if (hist_browser__run(browser, msg, &es))
  443. break;
  444. thread = hist_browser__selected_thread(browser);
  445. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  446. if (es.reason == NEWT_EXIT_HOTKEY) {
  447. key = es.u.key;
  448. switch (key) {
  449. case NEWT_KEY_F1:
  450. goto do_help;
  451. case NEWT_KEY_TAB:
  452. case NEWT_KEY_UNTAB:
  453. /*
  454. * Exit the browser, let hists__browser_tree
  455. * go to the next or previous
  456. */
  457. goto out_free_stack;
  458. default:;
  459. }
  460. key = toupper(key);
  461. switch (key) {
  462. case 'A':
  463. if (browser->selection->map == NULL &&
  464. browser->selection->map->dso->annotate_warned)
  465. continue;
  466. goto do_annotate;
  467. case 'D':
  468. goto zoom_dso;
  469. case 'T':
  470. goto zoom_thread;
  471. case 'H':
  472. case '?':
  473. do_help:
  474. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  475. "<- Zoom out\n"
  476. "a Annotate current symbol\n"
  477. "h/?/F1 Show this window\n"
  478. "d Zoom into current DSO\n"
  479. "t Zoom into current Thread\n"
  480. "q/CTRL+C Exit browser");
  481. continue;
  482. default:;
  483. }
  484. if (is_exit_key(key)) {
  485. if (key == NEWT_KEY_ESCAPE) {
  486. if (dialog_yesno("Do you really want to exit?"))
  487. break;
  488. else
  489. continue;
  490. } else
  491. break;
  492. }
  493. if (es.u.key == NEWT_KEY_LEFT) {
  494. const void *top;
  495. if (pstack__empty(fstack))
  496. continue;
  497. top = pstack__pop(fstack);
  498. if (top == &dso_filter)
  499. goto zoom_out_dso;
  500. if (top == &thread_filter)
  501. goto zoom_out_thread;
  502. continue;
  503. }
  504. }
  505. if (browser->selection->sym != NULL &&
  506. !browser->selection->map->dso->annotate_warned &&
  507. asprintf(&options[nr_options], "Annotate %s",
  508. browser->selection->sym->name) > 0)
  509. annotate = nr_options++;
  510. if (thread != NULL &&
  511. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  512. (thread_filter ? "out of" : "into"),
  513. (thread->comm_set ? thread->comm : ""),
  514. thread->pid) > 0)
  515. zoom_thread = nr_options++;
  516. if (dso != NULL &&
  517. asprintf(&options[nr_options], "Zoom %s %s DSO",
  518. (dso_filter ? "out of" : "into"),
  519. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  520. zoom_dso = nr_options++;
  521. if (browser->selection->map != NULL &&
  522. asprintf(&options[nr_options], "Browse map details") > 0)
  523. browse_map = nr_options++;
  524. options[nr_options++] = (char *)"Exit";
  525. choice = popup_menu(nr_options, options);
  526. for (i = 0; i < nr_options - 1; ++i)
  527. free(options[i]);
  528. if (choice == nr_options - 1)
  529. break;
  530. if (choice == -1)
  531. continue;
  532. if (choice == annotate) {
  533. struct hist_entry *he;
  534. do_annotate:
  535. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  536. browser->selection->map->dso->annotate_warned = 1;
  537. ui_helpline__puts("No vmlinux file found, can't "
  538. "annotate with just a "
  539. "kallsyms file");
  540. continue;
  541. }
  542. he = hist_browser__selected_entry(browser);
  543. if (he == NULL)
  544. continue;
  545. hist_entry__tui_annotate(he);
  546. } else if (choice == browse_map)
  547. map__browse(browser->selection->map);
  548. else if (choice == zoom_dso) {
  549. zoom_dso:
  550. if (dso_filter) {
  551. pstack__remove(fstack, &dso_filter);
  552. zoom_out_dso:
  553. ui_helpline__pop();
  554. dso_filter = NULL;
  555. } else {
  556. if (dso == NULL)
  557. continue;
  558. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  559. dso->kernel ? "the Kernel" : dso->short_name);
  560. dso_filter = dso;
  561. pstack__push(fstack, &dso_filter);
  562. }
  563. hists__filter_by_dso(self, dso_filter);
  564. hist_browser__title(msg, sizeof(msg), ev_name,
  565. dso_filter, thread_filter);
  566. hist_browser__reset(browser);
  567. } else if (choice == zoom_thread) {
  568. zoom_thread:
  569. if (thread_filter) {
  570. pstack__remove(fstack, &thread_filter);
  571. zoom_out_thread:
  572. ui_helpline__pop();
  573. thread_filter = NULL;
  574. } else {
  575. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  576. thread->comm_set ? thread->comm : "",
  577. thread->pid);
  578. thread_filter = thread;
  579. pstack__push(fstack, &thread_filter);
  580. }
  581. hists__filter_by_thread(self, thread_filter);
  582. hist_browser__title(msg, sizeof(msg), ev_name,
  583. dso_filter, thread_filter);
  584. hist_browser__reset(browser);
  585. }
  586. }
  587. out_free_stack:
  588. pstack__delete(fstack);
  589. out:
  590. hist_browser__delete(browser);
  591. return key;
  592. }
  593. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  594. {
  595. struct rb_node *first = rb_first(self), *nd = first, *next;
  596. int key = 0;
  597. while (nd) {
  598. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  599. const char *ev_name = __event_name(hists->type, hists->config);
  600. key = hists__browse(hists, help, ev_name);
  601. if (is_exit_key(key))
  602. break;
  603. switch (key) {
  604. case NEWT_KEY_TAB:
  605. next = rb_next(nd);
  606. if (next)
  607. nd = next;
  608. break;
  609. case NEWT_KEY_UNTAB:
  610. if (nd == first)
  611. continue;
  612. nd = rb_prev(nd);
  613. default:
  614. break;
  615. }
  616. }
  617. return key;
  618. }
  619. static void newt_suspend(void *d __used)
  620. {
  621. newtSuspend();
  622. raise(SIGTSTP);
  623. newtResume();
  624. }
  625. void setup_browser(void)
  626. {
  627. if (!isatty(1) || !use_browser || dump_trace) {
  628. use_browser = 0;
  629. setup_pager();
  630. return;
  631. }
  632. use_browser = 1;
  633. newtInit();
  634. newtCls();
  635. newtSetSuspendCallback(newt_suspend, NULL);
  636. ui_helpline__puts(" ");
  637. ui_browser__init();
  638. }
  639. void exit_browser(bool wait_for_ok)
  640. {
  641. if (use_browser > 0) {
  642. if (wait_for_ok) {
  643. char title[] = "Fatal Error", ok[] = "Ok";
  644. newtWinMessage(title, ok, browser__last_msg);
  645. }
  646. newtFinished();
  647. }
  648. }
  649. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  650. {
  651. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  652. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  653. sizeof("[k]"));
  654. }
  655. static void hist_browser__reset(struct hist_browser *self)
  656. {
  657. self->b.nr_entries = self->hists->nr_entries;
  658. hist_browser__refresh_dimensions(self);
  659. ui_browser__reset_index(&self->b);
  660. }
  661. static char tree__folded_sign(bool unfolded)
  662. {
  663. return unfolded ? '-' : '+';
  664. }
  665. static char map_symbol__folded(const struct map_symbol *self)
  666. {
  667. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  668. }
  669. static char hist_entry__folded(const struct hist_entry *self)
  670. {
  671. return map_symbol__folded(&self->ms);
  672. }
  673. static char callchain_list__folded(const struct callchain_list *self)
  674. {
  675. return map_symbol__folded(&self->ms);
  676. }
  677. static bool map_symbol__toggle_fold(struct map_symbol *self)
  678. {
  679. if (!self->has_children)
  680. return false;
  681. self->unfolded = !self->unfolded;
  682. return true;
  683. }
  684. #define LEVEL_OFFSET_STEP 3
  685. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
  686. struct callchain_node *chain_node,
  687. u64 total, int level,
  688. unsigned short row,
  689. off_t *row_offset,
  690. bool *is_current_entry)
  691. {
  692. struct rb_node *node;
  693. int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
  694. u64 new_total, remaining;
  695. if (callchain_param.mode == CHAIN_GRAPH_REL)
  696. new_total = chain_node->children_hit;
  697. else
  698. new_total = total;
  699. remaining = new_total;
  700. node = rb_first(&chain_node->rb_root);
  701. while (node) {
  702. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  703. struct rb_node *next = rb_next(node);
  704. u64 cumul = cumul_hits(child);
  705. struct callchain_list *chain;
  706. char folded_sign = ' ';
  707. int first = true;
  708. int extra_offset = 0;
  709. remaining -= cumul;
  710. list_for_each_entry(chain, &child->val, list) {
  711. char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
  712. const char *str;
  713. int color;
  714. bool was_first = first;
  715. if (first) {
  716. first = false;
  717. chain->ms.has_children = chain->list.next != &child->val ||
  718. rb_first(&child->rb_root) != NULL;
  719. } else {
  720. extra_offset = LEVEL_OFFSET_STEP;
  721. chain->ms.has_children = chain->list.next == &child->val &&
  722. rb_first(&child->rb_root) != NULL;
  723. }
  724. folded_sign = callchain_list__folded(chain);
  725. if (*row_offset != 0) {
  726. --*row_offset;
  727. goto do_next;
  728. }
  729. alloc_str = NULL;
  730. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  731. if (was_first) {
  732. double percent = cumul * 100.0 / new_total;
  733. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  734. str = "Not enough memory!";
  735. else
  736. str = alloc_str;
  737. }
  738. color = HE_COLORSET_NORMAL;
  739. width = self->b.width - (offset + extra_offset + 2);
  740. if (ui_browser__is_current_entry(&self->b, row)) {
  741. self->selection = &chain->ms;
  742. color = HE_COLORSET_SELECTED;
  743. *is_current_entry = true;
  744. }
  745. SLsmg_set_color(color);
  746. SLsmg_gotorc(self->b.y + row, self->b.x);
  747. slsmg_write_nstring(" ", offset + extra_offset);
  748. slsmg_printf("%c ", folded_sign);
  749. slsmg_write_nstring(str, width);
  750. free(alloc_str);
  751. if (++row == self->b.height)
  752. goto out;
  753. do_next:
  754. if (folded_sign == '+')
  755. break;
  756. }
  757. if (folded_sign == '-') {
  758. const int new_level = level + (extra_offset ? 2 : 1);
  759. row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
  760. new_level, row, row_offset,
  761. is_current_entry);
  762. }
  763. if (row == self->b.height)
  764. goto out;
  765. node = next;
  766. }
  767. out:
  768. return row - first_row;
  769. }
  770. static int hist_browser__show_callchain_node(struct hist_browser *self,
  771. struct callchain_node *node,
  772. int level, unsigned short row,
  773. off_t *row_offset,
  774. bool *is_current_entry)
  775. {
  776. struct callchain_list *chain;
  777. int first_row = row,
  778. offset = level * LEVEL_OFFSET_STEP,
  779. width = self->b.width - offset;
  780. char folded_sign = ' ';
  781. list_for_each_entry(chain, &node->val, list) {
  782. char ipstr[BITS_PER_LONG / 4 + 1], *s;
  783. int color;
  784. /*
  785. * FIXME: This should be moved to somewhere else,
  786. * probably when the callchain is created, so as not to
  787. * traverse it all over again
  788. */
  789. chain->ms.has_children = rb_first(&node->rb_root) != NULL;
  790. folded_sign = callchain_list__folded(chain);
  791. if (*row_offset != 0) {
  792. --*row_offset;
  793. continue;
  794. }
  795. color = HE_COLORSET_NORMAL;
  796. if (ui_browser__is_current_entry(&self->b, row)) {
  797. self->selection = &chain->ms;
  798. color = HE_COLORSET_SELECTED;
  799. *is_current_entry = true;
  800. }
  801. s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  802. SLsmg_gotorc(self->b.y + row, self->b.x);
  803. SLsmg_set_color(color);
  804. slsmg_write_nstring(" ", offset);
  805. slsmg_printf("%c ", folded_sign);
  806. slsmg_write_nstring(s, width - 2);
  807. if (++row == self->b.height)
  808. goto out;
  809. }
  810. if (folded_sign == '-')
  811. row += hist_browser__show_callchain_node_rb_tree(self, node,
  812. self->hists->stats.total_period,
  813. level + 1, row,
  814. row_offset,
  815. is_current_entry);
  816. out:
  817. return row - first_row;
  818. }
  819. static int hist_browser__show_callchain(struct hist_browser *self,
  820. struct rb_root *chain,
  821. int level, unsigned short row,
  822. off_t *row_offset,
  823. bool *is_current_entry)
  824. {
  825. struct rb_node *nd;
  826. int first_row = row;
  827. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  828. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  829. row += hist_browser__show_callchain_node(self, node, level,
  830. row, row_offset,
  831. is_current_entry);
  832. if (row == self->b.height)
  833. break;
  834. }
  835. return row - first_row;
  836. }
  837. static int hist_browser__show_entry(struct hist_browser *self,
  838. struct hist_entry *entry,
  839. unsigned short row)
  840. {
  841. char s[256];
  842. double percent;
  843. int printed = 0;
  844. int color, width = self->b.width;
  845. char folded_sign = ' ';
  846. bool current_entry = ui_browser__is_current_entry(&self->b, row);
  847. off_t row_offset = entry->row_offset;
  848. if (current_entry) {
  849. self->he_selection = entry;
  850. self->selection = &entry->ms;
  851. }
  852. if (symbol_conf.use_callchain) {
  853. entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
  854. folded_sign = hist_entry__folded(entry);
  855. }
  856. if (row_offset == 0) {
  857. hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
  858. 0, false, self->hists->stats.total_period);
  859. percent = (entry->period * 100.0) / self->hists->stats.total_period;
  860. color = HE_COLORSET_SELECTED;
  861. if (!current_entry) {
  862. if (percent >= MIN_RED)
  863. color = HE_COLORSET_TOP;
  864. else if (percent >= MIN_GREEN)
  865. color = HE_COLORSET_MEDIUM;
  866. else
  867. color = HE_COLORSET_NORMAL;
  868. }
  869. SLsmg_set_color(color);
  870. SLsmg_gotorc(self->b.y + row, self->b.x);
  871. if (symbol_conf.use_callchain) {
  872. slsmg_printf("%c ", folded_sign);
  873. width -= 2;
  874. }
  875. slsmg_write_nstring(s, width);
  876. ++row;
  877. ++printed;
  878. } else
  879. --row_offset;
  880. if (folded_sign == '-' && row != self->b.height) {
  881. printed += hist_browser__show_callchain(self, &entry->sorted_chain,
  882. 1, row, &row_offset,
  883. &current_entry);
  884. if (current_entry)
  885. self->he_selection = entry;
  886. }
  887. return printed;
  888. }
  889. static unsigned int hist_browser__refresh(struct ui_browser *self)
  890. {
  891. unsigned row = 0;
  892. struct rb_node *nd;
  893. struct hist_browser *hb = container_of(self, struct hist_browser, b);
  894. if (self->top == NULL)
  895. self->top = rb_first(&hb->hists->entries);
  896. for (nd = self->top; nd; nd = rb_next(nd)) {
  897. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  898. if (h->filtered)
  899. continue;
  900. row += hist_browser__show_entry(hb, h, row);
  901. if (row == self->height)
  902. break;
  903. }
  904. return row;
  905. }
  906. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  907. {
  908. struct rb_node *nd = rb_first(&self->rb_root);
  909. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  910. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  911. struct callchain_list *chain;
  912. int first = true;
  913. list_for_each_entry(chain, &child->val, list) {
  914. if (first) {
  915. first = false;
  916. chain->ms.has_children = chain->list.next != &child->val ||
  917. rb_first(&child->rb_root) != NULL;
  918. } else
  919. chain->ms.has_children = chain->list.next == &child->val &&
  920. rb_first(&child->rb_root) != NULL;
  921. }
  922. callchain_node__init_have_children_rb_tree(child);
  923. }
  924. }
  925. static void callchain_node__init_have_children(struct callchain_node *self)
  926. {
  927. struct callchain_list *chain;
  928. list_for_each_entry(chain, &self->val, list)
  929. chain->ms.has_children = rb_first(&self->rb_root) != NULL;
  930. callchain_node__init_have_children_rb_tree(self);
  931. }
  932. static void callchain__init_have_children(struct rb_root *self)
  933. {
  934. struct rb_node *nd;
  935. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  936. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  937. callchain_node__init_have_children(node);
  938. }
  939. }
  940. static void hist_entry__init_have_children(struct hist_entry *self)
  941. {
  942. if (!self->init_have_children) {
  943. callchain__init_have_children(&self->sorted_chain);
  944. self->init_have_children = true;
  945. }
  946. }
  947. static struct rb_node *hists__filter_entries(struct rb_node *nd)
  948. {
  949. while (nd != NULL) {
  950. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  951. if (!h->filtered)
  952. return nd;
  953. nd = rb_next(nd);
  954. }
  955. return NULL;
  956. }
  957. static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
  958. {
  959. while (nd != NULL) {
  960. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  961. if (!h->filtered)
  962. return nd;
  963. nd = rb_prev(nd);
  964. }
  965. return NULL;
  966. }
  967. static void ui_browser__hists_seek(struct ui_browser *self,
  968. off_t offset, int whence)
  969. {
  970. struct hist_entry *h;
  971. struct rb_node *nd;
  972. bool first = true;
  973. switch (whence) {
  974. case SEEK_SET:
  975. nd = hists__filter_entries(rb_first(self->entries));
  976. break;
  977. case SEEK_CUR:
  978. nd = self->top;
  979. goto do_offset;
  980. case SEEK_END:
  981. nd = hists__filter_prev_entries(rb_last(self->entries));
  982. first = false;
  983. break;
  984. default:
  985. return;
  986. }
  987. /*
  988. * Moves not relative to the first visible entry invalidates its
  989. * row_offset:
  990. */
  991. h = rb_entry(self->top, struct hist_entry, rb_node);
  992. h->row_offset = 0;
  993. /*
  994. * Here we have to check if nd is expanded (+), if it is we can't go
  995. * the next top level hist_entry, instead we must compute an offset of
  996. * what _not_ to show and not change the first visible entry.
  997. *
  998. * This offset increments when we are going from top to bottom and
  999. * decreases when we're going from bottom to top.
  1000. *
  1001. * As we don't have backpointers to the top level in the callchains
  1002. * structure, we need to always print the whole hist_entry callchain,
  1003. * skipping the first ones that are before the first visible entry
  1004. * and stop when we printed enough lines to fill the screen.
  1005. */
  1006. do_offset:
  1007. if (offset > 0) {
  1008. do {
  1009. h = rb_entry(nd, struct hist_entry, rb_node);
  1010. if (h->ms.unfolded) {
  1011. u16 remaining = h->nr_rows - h->row_offset;
  1012. if (offset > remaining) {
  1013. offset -= remaining;
  1014. h->row_offset = 0;
  1015. } else {
  1016. h->row_offset += offset;
  1017. offset = 0;
  1018. self->top = nd;
  1019. break;
  1020. }
  1021. }
  1022. nd = hists__filter_entries(rb_next(nd));
  1023. if (nd == NULL)
  1024. break;
  1025. --offset;
  1026. self->top = nd;
  1027. } while (offset != 0);
  1028. } else if (offset < 0) {
  1029. while (1) {
  1030. h = rb_entry(nd, struct hist_entry, rb_node);
  1031. if (h->ms.unfolded) {
  1032. if (first) {
  1033. if (-offset > h->row_offset) {
  1034. offset += h->row_offset;
  1035. h->row_offset = 0;
  1036. } else {
  1037. h->row_offset += offset;
  1038. offset = 0;
  1039. self->top = nd;
  1040. break;
  1041. }
  1042. } else {
  1043. if (-offset > h->nr_rows) {
  1044. offset += h->nr_rows;
  1045. h->row_offset = 0;
  1046. } else {
  1047. h->row_offset = h->nr_rows + offset;
  1048. offset = 0;
  1049. self->top = nd;
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. nd = hists__filter_prev_entries(rb_prev(nd));
  1055. if (nd == NULL)
  1056. break;
  1057. ++offset;
  1058. self->top = nd;
  1059. if (offset == 0) {
  1060. /*
  1061. * Last unfiltered hist_entry, check if it is
  1062. * unfolded, if it is then we should have
  1063. * row_offset at its last entry.
  1064. */
  1065. h = rb_entry(nd, struct hist_entry, rb_node);
  1066. if (h->ms.unfolded)
  1067. h->row_offset = h->nr_rows;
  1068. break;
  1069. }
  1070. first = false;
  1071. }
  1072. } else {
  1073. self->top = nd;
  1074. h = rb_entry(nd, struct hist_entry, rb_node);
  1075. h->row_offset = 0;
  1076. }
  1077. }
  1078. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  1079. {
  1080. int n = 0;
  1081. struct rb_node *nd;
  1082. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  1083. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  1084. struct callchain_list *chain;
  1085. char folded_sign = ' '; /* No children */
  1086. list_for_each_entry(chain, &child->val, list) {
  1087. ++n;
  1088. /* We need this because we may not have children */
  1089. folded_sign = callchain_list__folded(chain);
  1090. if (folded_sign == '+')
  1091. break;
  1092. }
  1093. if (folded_sign == '-') /* Have children and they're unfolded */
  1094. n += callchain_node__count_rows_rb_tree(child);
  1095. }
  1096. return n;
  1097. }
  1098. static int callchain_node__count_rows(struct callchain_node *node)
  1099. {
  1100. struct callchain_list *chain;
  1101. bool unfolded = false;
  1102. int n = 0;
  1103. list_for_each_entry(chain, &node->val, list) {
  1104. ++n;
  1105. unfolded = chain->ms.unfolded;
  1106. }
  1107. if (unfolded)
  1108. n += callchain_node__count_rows_rb_tree(node);
  1109. return n;
  1110. }
  1111. static int callchain__count_rows(struct rb_root *chain)
  1112. {
  1113. struct rb_node *nd;
  1114. int n = 0;
  1115. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  1116. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  1117. n += callchain_node__count_rows(node);
  1118. }
  1119. return n;
  1120. }
  1121. static bool hist_browser__toggle_fold(struct hist_browser *self)
  1122. {
  1123. if (map_symbol__toggle_fold(self->selection)) {
  1124. struct hist_entry *he = self->he_selection;
  1125. hist_entry__init_have_children(he);
  1126. self->hists->nr_entries -= he->nr_rows;
  1127. if (he->ms.unfolded)
  1128. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  1129. else
  1130. he->nr_rows = 0;
  1131. self->hists->nr_entries += he->nr_rows;
  1132. self->b.nr_entries = self->hists->nr_entries;
  1133. return true;
  1134. }
  1135. /* If it doesn't have children, no toggling performed */
  1136. return false;
  1137. }
  1138. static int hist_browser__run(struct hist_browser *self, const char *title,
  1139. struct newtExitStruct *es)
  1140. {
  1141. char str[256], unit;
  1142. unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
  1143. self->b.entries = &self->hists->entries;
  1144. self->b.nr_entries = self->hists->nr_entries;
  1145. hist_browser__refresh_dimensions(self);
  1146. nr_events = convert_unit(nr_events, &unit);
  1147. snprintf(str, sizeof(str), "Events: %lu%c ",
  1148. nr_events, unit);
  1149. newtDrawRootText(0, 0, str);
  1150. if (ui_browser__show(&self->b, title) < 0)
  1151. return -1;
  1152. newtFormAddHotKey(self->b.form, 'A');
  1153. newtFormAddHotKey(self->b.form, 'a');
  1154. newtFormAddHotKey(self->b.form, '?');
  1155. newtFormAddHotKey(self->b.form, 'h');
  1156. newtFormAddHotKey(self->b.form, 'H');
  1157. newtFormAddHotKey(self->b.form, 'd');
  1158. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  1159. newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
  1160. newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
  1161. while (1) {
  1162. ui_browser__run(&self->b, es);
  1163. if (es->reason != NEWT_EXIT_HOTKEY)
  1164. break;
  1165. switch (es->u.key) {
  1166. case 'd': { /* Debug */
  1167. static int seq;
  1168. struct hist_entry *h = rb_entry(self->b.top,
  1169. struct hist_entry, rb_node);
  1170. ui_helpline__pop();
  1171. ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
  1172. seq++, self->b.nr_entries,
  1173. self->hists->nr_entries,
  1174. self->b.height,
  1175. self->b.index,
  1176. self->b.top_idx,
  1177. h->row_offset, h->nr_rows);
  1178. }
  1179. continue;
  1180. case NEWT_KEY_ENTER:
  1181. if (hist_browser__toggle_fold(self))
  1182. break;
  1183. /* fall thru */
  1184. default:
  1185. return 0;
  1186. }
  1187. }
  1188. return 0;
  1189. }