map.c 3.6 KB

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