map.c 3.2 KB

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