map.c 3.2 KB

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