map.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <elf.h>
  2. #include <inttypes.h>
  3. #include <sys/ttydefaults.h>
  4. #include <string.h>
  5. #include <linux/bitops.h>
  6. #include "../../util/util.h"
  7. #include "../../util/debug.h"
  8. #include "../../util/symbol.h"
  9. #include "../browser.h"
  10. #include "../helpline.h"
  11. #include "../keysyms.h"
  12. #include "map.h"
  13. struct map_browser {
  14. struct ui_browser b;
  15. struct map *map;
  16. u8 addrlen;
  17. };
  18. static void map_browser__write(struct ui_browser *browser, void *nd, int row)
  19. {
  20. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  21. struct map_browser *mb = container_of(browser, struct map_browser, b);
  22. bool current_entry = ui_browser__is_current_entry(browser, row);
  23. int width;
  24. ui_browser__set_percent_color(browser, 0, current_entry);
  25. ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ",
  26. mb->addrlen, sym->start, mb->addrlen, sym->end,
  27. sym->binding == STB_GLOBAL ? 'g' :
  28. sym->binding == STB_LOCAL ? 'l' : 'w');
  29. width = browser->width - ((mb->addrlen * 2) + 4);
  30. if (width > 0)
  31. ui_browser__write_nstring(browser, sym->name, width);
  32. }
  33. /* FIXME uber-kludgy, see comment on cmd_report... */
  34. static u32 *symbol__browser_index(struct symbol *browser)
  35. {
  36. return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
  37. }
  38. static int map_browser__search(struct map_browser *browser)
  39. {
  40. char target[512];
  41. struct symbol *sym;
  42. int err = ui_browser__input_window("Search by name/addr",
  43. "Prefix with 0x to search by address",
  44. target, "ENTER: OK, ESC: Cancel", 0);
  45. if (err != K_ENTER)
  46. return -1;
  47. if (target[0] == '0' && tolower(target[1]) == 'x') {
  48. u64 addr = strtoull(target, NULL, 16);
  49. sym = map__find_symbol(browser->map, addr);
  50. } else
  51. sym = map__find_symbol_by_name(browser->map, target);
  52. if (sym != NULL) {
  53. u32 *idx = symbol__browser_index(sym);
  54. browser->b.top = &sym->rb_node;
  55. browser->b.index = browser->b.top_idx = *idx;
  56. } else
  57. ui_helpline__fpush("%s not found!", target);
  58. return 0;
  59. }
  60. static int map_browser__run(struct map_browser *browser)
  61. {
  62. int key;
  63. if (ui_browser__show(&browser->b, browser->map->dso->long_name,
  64. "Press ESC to exit, %s / to search",
  65. verbose ? "" : "restart with -v to use") < 0)
  66. return -1;
  67. while (1) {
  68. key = ui_browser__run(&browser->b, 0);
  69. switch (key) {
  70. case '/':
  71. if (verbose)
  72. map_browser__search(browser);
  73. default:
  74. break;
  75. case K_LEFT:
  76. case K_ESC:
  77. case 'q':
  78. case CTRL('c'):
  79. goto out;
  80. }
  81. }
  82. out:
  83. ui_browser__hide(&browser->b);
  84. return key;
  85. }
  86. int map__browse(struct map *map)
  87. {
  88. struct map_browser mb = {
  89. .b = {
  90. .entries = &map->dso->symbols[map->type],
  91. .refresh = ui_browser__rb_tree_refresh,
  92. .seek = ui_browser__rb_tree_seek,
  93. .write = map_browser__write,
  94. },
  95. .map = map,
  96. };
  97. struct rb_node *nd;
  98. char tmp[BITS_PER_LONG / 4];
  99. u64 maxaddr = 0;
  100. for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
  101. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  102. if (maxaddr < pos->end)
  103. maxaddr = pos->end;
  104. if (verbose) {
  105. u32 *idx = symbol__browser_index(pos);
  106. *idx = mb.b.nr_entries;
  107. }
  108. ++mb.b.nr_entries;
  109. }
  110. mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
  111. return map_browser__run(&mb);
  112. }