sym-handling.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2015 Naveen N. Rao, IBM Corporation
  7. */
  8. #include "debug.h"
  9. #include "symbol.h"
  10. #include "map.h"
  11. #include "probe-event.h"
  12. #include "probe-file.h"
  13. #ifdef HAVE_LIBELF_SUPPORT
  14. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
  15. {
  16. return ehdr.e_type == ET_EXEC ||
  17. ehdr.e_type == ET_REL ||
  18. ehdr.e_type == ET_DYN;
  19. }
  20. #endif
  21. int arch__choose_best_symbol(struct symbol *syma,
  22. struct symbol *symb __maybe_unused)
  23. {
  24. char *sym = syma->name;
  25. #if !defined(_CALL_ELF) || _CALL_ELF != 2
  26. /* Skip over any initial dot */
  27. if (*sym == '.')
  28. sym++;
  29. #endif
  30. /* Avoid "SyS" kernel syscall aliases */
  31. if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
  32. return SYMBOL_B;
  33. if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
  34. return SYMBOL_B;
  35. return SYMBOL_A;
  36. }
  37. #if !defined(_CALL_ELF) || _CALL_ELF != 2
  38. /* Allow matching against dot variants */
  39. int arch__compare_symbol_names(const char *namea, const char *nameb)
  40. {
  41. /* Skip over initial dot */
  42. if (*namea == '.')
  43. namea++;
  44. if (*nameb == '.')
  45. nameb++;
  46. return strcmp(namea, nameb);
  47. }
  48. int arch__compare_symbol_names_n(const char *namea, const char *nameb,
  49. unsigned int n)
  50. {
  51. /* Skip over initial dot */
  52. if (*namea == '.')
  53. namea++;
  54. if (*nameb == '.')
  55. nameb++;
  56. return strncmp(namea, nameb, n);
  57. }
  58. const char *arch__normalize_symbol_name(const char *name)
  59. {
  60. /* Skip over initial dot */
  61. if (name && *name == '.')
  62. name++;
  63. return name;
  64. }
  65. #endif
  66. #if defined(_CALL_ELF) && _CALL_ELF == 2
  67. #ifdef HAVE_LIBELF_SUPPORT
  68. void arch__sym_update(struct symbol *s, GElf_Sym *sym)
  69. {
  70. s->arch_sym = sym->st_other;
  71. }
  72. #endif
  73. #define PPC64LE_LEP_OFFSET 8
  74. void arch__fix_tev_from_maps(struct perf_probe_event *pev,
  75. struct probe_trace_event *tev, struct map *map,
  76. struct symbol *sym)
  77. {
  78. int lep_offset;
  79. /*
  80. * When probing at a function entry point, we normally always want the
  81. * LEP since that catches calls to the function through both the GEP and
  82. * the LEP. Hence, we would like to probe at an offset of 8 bytes if
  83. * the user only specified the function entry.
  84. *
  85. * However, if the user specifies an offset, we fall back to using the
  86. * GEP since all userspace applications (objdump/readelf) show function
  87. * disassembly with offsets from the GEP.
  88. */
  89. if (pev->point.offset || !map || !sym)
  90. return;
  91. /* For kretprobes, add an offset only if the kernel supports it */
  92. if (!pev->uprobes && pev->point.retprobe) {
  93. #ifdef HAVE_LIBELF_SUPPORT
  94. if (!kretprobe_offset_is_supported())
  95. #endif
  96. return;
  97. }
  98. lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
  99. if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
  100. tev->point.offset += PPC64LE_LEP_OFFSET;
  101. else if (lep_offset) {
  102. if (pev->uprobes)
  103. tev->point.address += lep_offset;
  104. else
  105. tev->point.offset += lep_offset;
  106. }
  107. }
  108. #ifdef HAVE_LIBELF_SUPPORT
  109. void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
  110. int ntevs)
  111. {
  112. struct probe_trace_event *tev;
  113. struct map *map;
  114. struct symbol *sym = NULL;
  115. struct rb_node *tmp;
  116. int i = 0;
  117. map = get_target_map(pev->target, pev->nsi, pev->uprobes);
  118. if (!map || map__load(map) < 0)
  119. return;
  120. for (i = 0; i < ntevs; i++) {
  121. tev = &pev->tevs[i];
  122. map__for_each_symbol(map, sym, tmp) {
  123. if (map->unmap_ip(map, sym->start) == tev->point.address) {
  124. arch__fix_tev_from_maps(pev, tev, map, sym);
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. #endif /* HAVE_LIBELF_SUPPORT */
  131. #endif