sym-handling.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #if !defined(_CALL_ELF) || _CALL_ELF != 2
  22. int arch__choose_best_symbol(struct symbol *syma,
  23. struct symbol *symb __maybe_unused)
  24. {
  25. char *sym = syma->name;
  26. /* Skip over any initial dot */
  27. if (*sym == '.')
  28. sym++;
  29. /* Avoid "SyS" kernel syscall aliases */
  30. if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
  31. return SYMBOL_B;
  32. if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
  33. return SYMBOL_B;
  34. return SYMBOL_A;
  35. }
  36. /* Allow matching against dot variants */
  37. int arch__compare_symbol_names(const char *namea, const char *nameb)
  38. {
  39. /* Skip over initial dot */
  40. if (*namea == '.')
  41. namea++;
  42. if (*nameb == '.')
  43. nameb++;
  44. return strcmp(namea, nameb);
  45. }
  46. int arch__compare_symbol_names_n(const char *namea, const char *nameb,
  47. unsigned int n)
  48. {
  49. /* Skip over initial dot */
  50. if (*namea == '.')
  51. namea++;
  52. if (*nameb == '.')
  53. nameb++;
  54. return strncmp(namea, nameb, n);
  55. }
  56. const char *arch__normalize_symbol_name(const char *name)
  57. {
  58. /* Skip over initial dot */
  59. if (name && *name == '.')
  60. name++;
  61. return name;
  62. }
  63. #endif
  64. #if defined(_CALL_ELF) && _CALL_ELF == 2
  65. #ifdef HAVE_LIBELF_SUPPORT
  66. void arch__sym_update(struct symbol *s, GElf_Sym *sym)
  67. {
  68. s->arch_sym = sym->st_other;
  69. }
  70. #endif
  71. #define PPC64LE_LEP_OFFSET 8
  72. void arch__fix_tev_from_maps(struct perf_probe_event *pev,
  73. struct probe_trace_event *tev, struct map *map,
  74. struct symbol *sym)
  75. {
  76. int lep_offset;
  77. /*
  78. * When probing at a function entry point, we normally always want the
  79. * LEP since that catches calls to the function through both the GEP and
  80. * the LEP. Hence, we would like to probe at an offset of 8 bytes if
  81. * the user only specified the function entry.
  82. *
  83. * However, if the user specifies an offset, we fall back to using the
  84. * GEP since all userspace applications (objdump/readelf) show function
  85. * disassembly with offsets from the GEP.
  86. */
  87. if (pev->point.offset || !map || !sym)
  88. return;
  89. /* For kretprobes, add an offset only if the kernel supports it */
  90. if (!pev->uprobes && pev->point.retprobe) {
  91. #ifdef HAVE_LIBELF_SUPPORT
  92. if (!kretprobe_offset_is_supported())
  93. #endif
  94. return;
  95. }
  96. lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
  97. if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
  98. tev->point.offset += PPC64LE_LEP_OFFSET;
  99. else if (lep_offset) {
  100. if (pev->uprobes)
  101. tev->point.address += lep_offset;
  102. else
  103. tev->point.offset += lep_offset;
  104. }
  105. }
  106. #ifdef HAVE_LIBELF_SUPPORT
  107. void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
  108. int ntevs)
  109. {
  110. struct probe_trace_event *tev;
  111. struct map *map;
  112. struct symbol *sym = NULL;
  113. struct rb_node *tmp;
  114. int i = 0;
  115. map = get_target_map(pev->target, pev->nsi, pev->uprobes);
  116. if (!map || map__load(map) < 0)
  117. return;
  118. for (i = 0; i < ntevs; i++) {
  119. tev = &pev->tevs[i];
  120. map__for_each_symbol(map, sym, tmp) {
  121. if (map->unmap_ip(map, sym->start) == tev->point.address)
  122. arch__fix_tev_from_maps(pev, tev, map, sym);
  123. }
  124. }
  125. }
  126. #endif /* HAVE_LIBELF_SUPPORT */
  127. #endif