map.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../libslang.h"
  2. #include <elf.h>
  3. #include <inttypes.h>
  4. #include <sys/ttydefaults.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include <linux/bitops.h>
  8. #include "../../debug.h"
  9. #include "../../symbol.h"
  10. #include "../browser.h"
  11. #include "../helpline.h"
  12. #include "map.h"
  13. static int ui_entry__read(const char *title, char *bf, size_t size, int width)
  14. {
  15. struct newtExitStruct es;
  16. newtComponent form, entry;
  17. const char *result;
  18. int err = -1;
  19. newtCenteredWindow(width, 1, title);
  20. form = newtForm(NULL, NULL, 0);
  21. if (form == NULL)
  22. return -1;
  23. entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
  24. if (entry == NULL)
  25. goto out_free_form;
  26. newtFormAddComponent(form, entry);
  27. newtFormAddHotKey(form, NEWT_KEY_ENTER);
  28. newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
  29. newtFormAddHotKey(form, NEWT_KEY_LEFT);
  30. newtFormAddHotKey(form, CTRL('c'));
  31. newtFormRun(form, &es);
  32. if (result != NULL) {
  33. strncpy(bf, result, size);
  34. err = 0;
  35. }
  36. out_free_form:
  37. newtPopWindow();
  38. newtFormDestroy(form);
  39. return err;
  40. }
  41. struct map_browser {
  42. struct ui_browser b;
  43. struct map *map;
  44. u8 addrlen;
  45. };
  46. static void map_browser__write(struct ui_browser *self, void *nd, int row)
  47. {
  48. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  49. struct map_browser *mb = container_of(self, struct map_browser, b);
  50. bool current_entry = ui_browser__is_current_entry(self, row);
  51. int width;
  52. ui_browser__set_percent_color(self, 0, current_entry);
  53. slsmg_printf("%*" PRIx64 " %*" PRIx64 " %c ",
  54. mb->addrlen, sym->start, mb->addrlen, sym->end,
  55. sym->binding == STB_GLOBAL ? 'g' :
  56. sym->binding == STB_LOCAL ? 'l' : 'w');
  57. width = self->width - ((mb->addrlen * 2) + 4);
  58. if (width > 0)
  59. slsmg_write_nstring(sym->name, width);
  60. }
  61. /* FIXME uber-kludgy, see comment on cmd_report... */
  62. static u32 *symbol__browser_index(struct symbol *self)
  63. {
  64. return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
  65. }
  66. static int map_browser__search(struct map_browser *self)
  67. {
  68. char target[512];
  69. struct symbol *sym;
  70. int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
  71. if (err)
  72. return err;
  73. if (target[0] == '0' && tolower(target[1]) == 'x') {
  74. u64 addr = strtoull(target, NULL, 16);
  75. sym = map__find_symbol(self->map, addr, NULL);
  76. } else
  77. sym = map__find_symbol_by_name(self->map, target, NULL);
  78. if (sym != NULL) {
  79. u32 *idx = symbol__browser_index(sym);
  80. self->b.top = &sym->rb_node;
  81. self->b.index = self->b.top_idx = *idx;
  82. } else
  83. ui_helpline__fpush("%s not found!", target);
  84. return 0;
  85. }
  86. static int map_browser__run(struct map_browser *self)
  87. {
  88. int key;
  89. if (ui_browser__show(&self->b, self->map->dso->long_name,
  90. "Press <- or ESC to exit, %s / to search",
  91. verbose ? "" : "restart with -v to use") < 0)
  92. return -1;
  93. if (verbose)
  94. ui_browser__add_exit_key(&self->b, '/');
  95. while (1) {
  96. key = ui_browser__run(&self->b);
  97. if (verbose && key == '/')
  98. map_browser__search(self);
  99. else
  100. break;
  101. }
  102. ui_browser__hide(&self->b);
  103. return key;
  104. }
  105. int map__browse(struct map *self)
  106. {
  107. struct map_browser mb = {
  108. .b = {
  109. .entries = &self->dso->symbols[self->type],
  110. .refresh = ui_browser__rb_tree_refresh,
  111. .seek = ui_browser__rb_tree_seek,
  112. .write = map_browser__write,
  113. },
  114. .map = self,
  115. };
  116. struct rb_node *nd;
  117. char tmp[BITS_PER_LONG / 4];
  118. u64 maxaddr = 0;
  119. for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
  120. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  121. if (maxaddr < pos->end)
  122. maxaddr = pos->end;
  123. if (verbose) {
  124. u32 *idx = symbol__browser_index(pos);
  125. *idx = mb.b.nr_entries;
  126. }
  127. ++mb.b.nr_entries;
  128. }
  129. mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr);
  130. return map_browser__run(&mb);
  131. }