map.c 3.6 KB

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