probe-finder.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*
  2. * probe-finder.c : C expression to kprobe event converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <dwarf-regs.h>
  33. #include <linux/bitops.h>
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "symbol.h"
  38. #include "probe-finder.h"
  39. /* Kprobe tracer basic type is up to u64 */
  40. #define MAX_BASIC_TYPE_BITS 64
  41. /* Line number list operations */
  42. /* Add a line to line number list */
  43. static int line_list__add_line(struct list_head *head, int line)
  44. {
  45. struct line_node *ln;
  46. struct list_head *p;
  47. /* Reverse search, because new line will be the last one */
  48. list_for_each_entry_reverse(ln, head, list) {
  49. if (ln->line < line) {
  50. p = &ln->list;
  51. goto found;
  52. } else if (ln->line == line) /* Already exist */
  53. return 1;
  54. }
  55. /* List is empty, or the smallest entry */
  56. p = head;
  57. found:
  58. pr_debug("line list: add a line %u\n", line);
  59. ln = zalloc(sizeof(struct line_node));
  60. if (ln == NULL)
  61. return -ENOMEM;
  62. ln->line = line;
  63. INIT_LIST_HEAD(&ln->list);
  64. list_add(&ln->list, p);
  65. return 0;
  66. }
  67. /* Check if the line in line number list */
  68. static int line_list__has_line(struct list_head *head, int line)
  69. {
  70. struct line_node *ln;
  71. /* Reverse search, because new line will be the last one */
  72. list_for_each_entry(ln, head, list)
  73. if (ln->line == line)
  74. return 1;
  75. return 0;
  76. }
  77. /* Init line number list */
  78. static void line_list__init(struct list_head *head)
  79. {
  80. INIT_LIST_HEAD(head);
  81. }
  82. /* Free line number list */
  83. static void line_list__free(struct list_head *head)
  84. {
  85. struct line_node *ln;
  86. while (!list_empty(head)) {
  87. ln = list_first_entry(head, struct line_node, list);
  88. list_del(&ln->list);
  89. free(ln);
  90. }
  91. }
  92. /* Dwarf FL wrappers */
  93. static char *debuginfo_path; /* Currently dummy */
  94. static const Dwfl_Callbacks offline_callbacks = {
  95. .find_debuginfo = dwfl_standard_find_debuginfo,
  96. .debuginfo_path = &debuginfo_path,
  97. .section_address = dwfl_offline_section_address,
  98. /* We use this table for core files too. */
  99. .find_elf = dwfl_build_id_find_elf,
  100. };
  101. /* Get a Dwarf from offline image */
  102. static int debuginfo__init_offline_dwarf(struct debuginfo *self,
  103. const char *path)
  104. {
  105. Dwfl_Module *mod;
  106. int fd;
  107. fd = open(path, O_RDONLY);
  108. if (fd < 0)
  109. return fd;
  110. self->dwfl = dwfl_begin(&offline_callbacks);
  111. if (!self->dwfl)
  112. goto error;
  113. mod = dwfl_report_offline(self->dwfl, "", "", fd);
  114. if (!mod)
  115. goto error;
  116. self->dbg = dwfl_module_getdwarf(mod, &self->bias);
  117. if (!self->dbg)
  118. goto error;
  119. return 0;
  120. error:
  121. if (self->dwfl)
  122. dwfl_end(self->dwfl);
  123. else
  124. close(fd);
  125. memset(self, 0, sizeof(*self));
  126. return -ENOENT;
  127. }
  128. #if _ELFUTILS_PREREQ(0, 148)
  129. /* This method is buggy if elfutils is older than 0.148 */
  130. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  131. void **userdata,
  132. const char *module_name,
  133. Dwarf_Addr base,
  134. char **file_name, Elf **elfp)
  135. {
  136. int fd;
  137. const char *path = kernel_get_module_path(module_name);
  138. pr_debug2("Use file %s for %s\n", path, module_name);
  139. if (path) {
  140. fd = open(path, O_RDONLY);
  141. if (fd >= 0) {
  142. *file_name = strdup(path);
  143. return fd;
  144. }
  145. }
  146. /* If failed, try to call standard method */
  147. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  148. file_name, elfp);
  149. }
  150. static const Dwfl_Callbacks kernel_callbacks = {
  151. .find_debuginfo = dwfl_standard_find_debuginfo,
  152. .debuginfo_path = &debuginfo_path,
  153. .find_elf = __linux_kernel_find_elf,
  154. .section_address = dwfl_linux_kernel_module_section_address,
  155. };
  156. /* Get a Dwarf from live kernel image */
  157. static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
  158. Dwarf_Addr addr)
  159. {
  160. self->dwfl = dwfl_begin(&kernel_callbacks);
  161. if (!self->dwfl)
  162. return -EINVAL;
  163. /* Load the kernel dwarves: Don't care the result here */
  164. dwfl_linux_kernel_report_kernel(self->dwfl);
  165. dwfl_linux_kernel_report_modules(self->dwfl);
  166. self->dbg = dwfl_addrdwarf(self->dwfl, addr, &self->bias);
  167. /* Here, check whether we could get a real dwarf */
  168. if (!self->dbg) {
  169. pr_debug("Failed to find kernel dwarf at %lx\n",
  170. (unsigned long)addr);
  171. dwfl_end(self->dwfl);
  172. memset(self, 0, sizeof(*self));
  173. return -ENOENT;
  174. }
  175. return 0;
  176. }
  177. #else
  178. /* With older elfutils, this just support kernel module... */
  179. static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
  180. Dwarf_Addr addr __maybe_unused)
  181. {
  182. const char *path = kernel_get_module_path("kernel");
  183. if (!path) {
  184. pr_err("Failed to find vmlinux path\n");
  185. return -ENOENT;
  186. }
  187. pr_debug2("Use file %s for debuginfo\n", path);
  188. return debuginfo__init_offline_dwarf(self, path);
  189. }
  190. #endif
  191. struct debuginfo *debuginfo__new(const char *path)
  192. {
  193. struct debuginfo *self = zalloc(sizeof(struct debuginfo));
  194. if (!self)
  195. return NULL;
  196. if (debuginfo__init_offline_dwarf(self, path) < 0) {
  197. free(self);
  198. self = NULL;
  199. }
  200. return self;
  201. }
  202. struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
  203. {
  204. struct debuginfo *self = zalloc(sizeof(struct debuginfo));
  205. if (!self)
  206. return NULL;
  207. if (debuginfo__init_online_kernel_dwarf(self, (Dwarf_Addr)addr) < 0) {
  208. free(self);
  209. self = NULL;
  210. }
  211. return self;
  212. }
  213. void debuginfo__delete(struct debuginfo *self)
  214. {
  215. if (self) {
  216. if (self->dwfl)
  217. dwfl_end(self->dwfl);
  218. free(self);
  219. }
  220. }
  221. /*
  222. * Probe finder related functions
  223. */
  224. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  225. {
  226. struct probe_trace_arg_ref *ref;
  227. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  228. if (ref != NULL)
  229. ref->offset = offs;
  230. return ref;
  231. }
  232. /*
  233. * Convert a location into trace_arg.
  234. * If tvar == NULL, this just checks variable can be converted.
  235. */
  236. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  237. Dwarf_Op *fb_ops,
  238. struct probe_trace_arg *tvar)
  239. {
  240. Dwarf_Attribute attr;
  241. Dwarf_Op *op;
  242. size_t nops;
  243. unsigned int regn;
  244. Dwarf_Word offs = 0;
  245. bool ref = false;
  246. const char *regs;
  247. int ret;
  248. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  249. goto static_var;
  250. /* TODO: handle more than 1 exprs */
  251. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  252. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  253. nops == 0) {
  254. /* TODO: Support const_value */
  255. return -ENOENT;
  256. }
  257. if (op->atom == DW_OP_addr) {
  258. static_var:
  259. if (!tvar)
  260. return 0;
  261. /* Static variables on memory (not stack), make @varname */
  262. ret = strlen(dwarf_diename(vr_die));
  263. tvar->value = zalloc(ret + 2);
  264. if (tvar->value == NULL)
  265. return -ENOMEM;
  266. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  267. tvar->ref = alloc_trace_arg_ref((long)offs);
  268. if (tvar->ref == NULL)
  269. return -ENOMEM;
  270. return 0;
  271. }
  272. /* If this is based on frame buffer, set the offset */
  273. if (op->atom == DW_OP_fbreg) {
  274. if (fb_ops == NULL)
  275. return -ENOTSUP;
  276. ref = true;
  277. offs = op->number;
  278. op = &fb_ops[0];
  279. }
  280. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  281. regn = op->atom - DW_OP_breg0;
  282. offs += op->number;
  283. ref = true;
  284. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  285. regn = op->atom - DW_OP_reg0;
  286. } else if (op->atom == DW_OP_bregx) {
  287. regn = op->number;
  288. offs += op->number2;
  289. ref = true;
  290. } else if (op->atom == DW_OP_regx) {
  291. regn = op->number;
  292. } else {
  293. pr_debug("DW_OP %x is not supported.\n", op->atom);
  294. return -ENOTSUP;
  295. }
  296. if (!tvar)
  297. return 0;
  298. regs = get_arch_regstr(regn);
  299. if (!regs) {
  300. /* This should be a bug in DWARF or this tool */
  301. pr_warning("Mapping for the register number %u "
  302. "missing on this architecture.\n", regn);
  303. return -ERANGE;
  304. }
  305. tvar->value = strdup(regs);
  306. if (tvar->value == NULL)
  307. return -ENOMEM;
  308. if (ref) {
  309. tvar->ref = alloc_trace_arg_ref((long)offs);
  310. if (tvar->ref == NULL)
  311. return -ENOMEM;
  312. }
  313. return 0;
  314. }
  315. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  316. static int convert_variable_type(Dwarf_Die *vr_die,
  317. struct probe_trace_arg *tvar,
  318. const char *cast)
  319. {
  320. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  321. Dwarf_Die type;
  322. char buf[16];
  323. int bsize, boffs, total;
  324. int ret;
  325. /* TODO: check all types */
  326. if (cast && strcmp(cast, "string") != 0) {
  327. /* Non string type is OK */
  328. tvar->type = strdup(cast);
  329. return (tvar->type == NULL) ? -ENOMEM : 0;
  330. }
  331. bsize = dwarf_bitsize(vr_die);
  332. if (bsize > 0) {
  333. /* This is a bitfield */
  334. boffs = dwarf_bitoffset(vr_die);
  335. total = dwarf_bytesize(vr_die);
  336. if (boffs < 0 || total < 0)
  337. return -ENOENT;
  338. ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
  339. BYTES_TO_BITS(total));
  340. goto formatted;
  341. }
  342. if (die_get_real_type(vr_die, &type) == NULL) {
  343. pr_warning("Failed to get a type information of %s.\n",
  344. dwarf_diename(vr_die));
  345. return -ENOENT;
  346. }
  347. pr_debug("%s type is %s.\n",
  348. dwarf_diename(vr_die), dwarf_diename(&type));
  349. if (cast && strcmp(cast, "string") == 0) { /* String type */
  350. ret = dwarf_tag(&type);
  351. if (ret != DW_TAG_pointer_type &&
  352. ret != DW_TAG_array_type) {
  353. pr_warning("Failed to cast into string: "
  354. "%s(%s) is not a pointer nor array.\n",
  355. dwarf_diename(vr_die), dwarf_diename(&type));
  356. return -EINVAL;
  357. }
  358. if (die_get_real_type(&type, &type) == NULL) {
  359. pr_warning("Failed to get a type"
  360. " information.\n");
  361. return -ENOENT;
  362. }
  363. if (ret == DW_TAG_pointer_type) {
  364. while (*ref_ptr)
  365. ref_ptr = &(*ref_ptr)->next;
  366. /* Add new reference with offset +0 */
  367. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  368. if (*ref_ptr == NULL) {
  369. pr_warning("Out of memory error\n");
  370. return -ENOMEM;
  371. }
  372. }
  373. if (!die_compare_name(&type, "char") &&
  374. !die_compare_name(&type, "unsigned char")) {
  375. pr_warning("Failed to cast into string: "
  376. "%s is not (unsigned) char *.\n",
  377. dwarf_diename(vr_die));
  378. return -EINVAL;
  379. }
  380. tvar->type = strdup(cast);
  381. return (tvar->type == NULL) ? -ENOMEM : 0;
  382. }
  383. ret = dwarf_bytesize(&type);
  384. if (ret <= 0)
  385. /* No size ... try to use default type */
  386. return 0;
  387. ret = BYTES_TO_BITS(ret);
  388. /* Check the bitwidth */
  389. if (ret > MAX_BASIC_TYPE_BITS) {
  390. pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
  391. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  392. ret = MAX_BASIC_TYPE_BITS;
  393. }
  394. ret = snprintf(buf, 16, "%c%d",
  395. die_is_signed_type(&type) ? 's' : 'u', ret);
  396. formatted:
  397. if (ret < 0 || ret >= 16) {
  398. if (ret >= 16)
  399. ret = -E2BIG;
  400. pr_warning("Failed to convert variable type: %s\n",
  401. strerror(-ret));
  402. return ret;
  403. }
  404. tvar->type = strdup(buf);
  405. if (tvar->type == NULL)
  406. return -ENOMEM;
  407. return 0;
  408. }
  409. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  410. struct perf_probe_arg_field *field,
  411. struct probe_trace_arg_ref **ref_ptr,
  412. Dwarf_Die *die_mem)
  413. {
  414. struct probe_trace_arg_ref *ref = *ref_ptr;
  415. Dwarf_Die type;
  416. Dwarf_Word offs;
  417. int ret, tag;
  418. pr_debug("converting %s in %s\n", field->name, varname);
  419. if (die_get_real_type(vr_die, &type) == NULL) {
  420. pr_warning("Failed to get the type of %s.\n", varname);
  421. return -ENOENT;
  422. }
  423. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  424. tag = dwarf_tag(&type);
  425. if (field->name[0] == '[' &&
  426. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  427. if (field->next)
  428. /* Save original type for next field */
  429. memcpy(die_mem, &type, sizeof(*die_mem));
  430. /* Get the type of this array */
  431. if (die_get_real_type(&type, &type) == NULL) {
  432. pr_warning("Failed to get the type of %s.\n", varname);
  433. return -ENOENT;
  434. }
  435. pr_debug2("Array real type: (%x)\n",
  436. (unsigned)dwarf_dieoffset(&type));
  437. if (tag == DW_TAG_pointer_type) {
  438. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  439. if (ref == NULL)
  440. return -ENOMEM;
  441. if (*ref_ptr)
  442. (*ref_ptr)->next = ref;
  443. else
  444. *ref_ptr = ref;
  445. }
  446. ref->offset += dwarf_bytesize(&type) * field->index;
  447. if (!field->next)
  448. /* Save vr_die for converting types */
  449. memcpy(die_mem, vr_die, sizeof(*die_mem));
  450. goto next;
  451. } else if (tag == DW_TAG_pointer_type) {
  452. /* Check the pointer and dereference */
  453. if (!field->ref) {
  454. pr_err("Semantic error: %s must be referred by '->'\n",
  455. field->name);
  456. return -EINVAL;
  457. }
  458. /* Get the type pointed by this pointer */
  459. if (die_get_real_type(&type, &type) == NULL) {
  460. pr_warning("Failed to get the type of %s.\n", varname);
  461. return -ENOENT;
  462. }
  463. /* Verify it is a data structure */
  464. tag = dwarf_tag(&type);
  465. if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
  466. pr_warning("%s is not a data structure nor an union.\n",
  467. varname);
  468. return -EINVAL;
  469. }
  470. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  471. if (ref == NULL)
  472. return -ENOMEM;
  473. if (*ref_ptr)
  474. (*ref_ptr)->next = ref;
  475. else
  476. *ref_ptr = ref;
  477. } else {
  478. /* Verify it is a data structure */
  479. if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
  480. pr_warning("%s is not a data structure nor an union.\n",
  481. varname);
  482. return -EINVAL;
  483. }
  484. if (field->name[0] == '[') {
  485. pr_err("Semantic error: %s is not a pointor"
  486. " nor array.\n", varname);
  487. return -EINVAL;
  488. }
  489. if (field->ref) {
  490. pr_err("Semantic error: %s must be referred by '.'\n",
  491. field->name);
  492. return -EINVAL;
  493. }
  494. if (!ref) {
  495. pr_warning("Structure on a register is not "
  496. "supported yet.\n");
  497. return -ENOTSUP;
  498. }
  499. }
  500. if (die_find_member(&type, field->name, die_mem) == NULL) {
  501. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  502. dwarf_diename(&type), field->name);
  503. return -EINVAL;
  504. }
  505. /* Get the offset of the field */
  506. if (tag == DW_TAG_union_type) {
  507. offs = 0;
  508. } else {
  509. ret = die_get_data_member_location(die_mem, &offs);
  510. if (ret < 0) {
  511. pr_warning("Failed to get the offset of %s.\n",
  512. field->name);
  513. return ret;
  514. }
  515. }
  516. ref->offset += (long)offs;
  517. next:
  518. /* Converting next field */
  519. if (field->next)
  520. return convert_variable_fields(die_mem, field->name,
  521. field->next, &ref, die_mem);
  522. else
  523. return 0;
  524. }
  525. /* Show a variables in kprobe event format */
  526. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  527. {
  528. Dwarf_Die die_mem;
  529. int ret;
  530. pr_debug("Converting variable %s into trace event.\n",
  531. dwarf_diename(vr_die));
  532. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  533. pf->tvar);
  534. if (ret == -ENOENT)
  535. pr_err("Failed to find the location of %s at this address.\n"
  536. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  537. else if (ret == -ENOTSUP)
  538. pr_err("Sorry, we don't support this variable location yet.\n");
  539. else if (pf->pvar->field) {
  540. ret = convert_variable_fields(vr_die, pf->pvar->var,
  541. pf->pvar->field, &pf->tvar->ref,
  542. &die_mem);
  543. vr_die = &die_mem;
  544. }
  545. if (ret == 0)
  546. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  547. /* *expr will be cached in libdw. Don't free it. */
  548. return ret;
  549. }
  550. /* Find a variable in a scope DIE */
  551. static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
  552. {
  553. Dwarf_Die vr_die;
  554. char buf[32], *ptr;
  555. int ret = 0;
  556. if (!is_c_varname(pf->pvar->var)) {
  557. /* Copy raw parameters */
  558. pf->tvar->value = strdup(pf->pvar->var);
  559. if (pf->tvar->value == NULL)
  560. return -ENOMEM;
  561. if (pf->pvar->type) {
  562. pf->tvar->type = strdup(pf->pvar->type);
  563. if (pf->tvar->type == NULL)
  564. return -ENOMEM;
  565. }
  566. if (pf->pvar->name) {
  567. pf->tvar->name = strdup(pf->pvar->name);
  568. if (pf->tvar->name == NULL)
  569. return -ENOMEM;
  570. } else
  571. pf->tvar->name = NULL;
  572. return 0;
  573. }
  574. if (pf->pvar->name)
  575. pf->tvar->name = strdup(pf->pvar->name);
  576. else {
  577. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  578. if (ret < 0)
  579. return ret;
  580. ptr = strchr(buf, ':'); /* Change type separator to _ */
  581. if (ptr)
  582. *ptr = '_';
  583. pf->tvar->name = strdup(buf);
  584. }
  585. if (pf->tvar->name == NULL)
  586. return -ENOMEM;
  587. pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
  588. /* Search child die for local variables and parameters. */
  589. if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
  590. /* Search again in global variables */
  591. if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, 0, &vr_die))
  592. ret = -ENOENT;
  593. }
  594. if (ret >= 0)
  595. ret = convert_variable(&vr_die, pf);
  596. if (ret < 0)
  597. pr_warning("Failed to find '%s' in this function.\n",
  598. pf->pvar->var);
  599. return ret;
  600. }
  601. /* Convert subprogram DIE to trace point */
  602. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  603. bool retprobe, struct probe_trace_point *tp)
  604. {
  605. Dwarf_Addr eaddr, highaddr;
  606. const char *name;
  607. /* Copy the name of probe point */
  608. name = dwarf_diename(sp_die);
  609. if (name) {
  610. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  611. pr_warning("Failed to get entry address of %s\n",
  612. dwarf_diename(sp_die));
  613. return -ENOENT;
  614. }
  615. if (dwarf_highpc(sp_die, &highaddr) != 0) {
  616. pr_warning("Failed to get end address of %s\n",
  617. dwarf_diename(sp_die));
  618. return -ENOENT;
  619. }
  620. if (paddr > highaddr) {
  621. pr_warning("Offset specified is greater than size of %s\n",
  622. dwarf_diename(sp_die));
  623. return -EINVAL;
  624. }
  625. tp->symbol = strdup(name);
  626. if (tp->symbol == NULL)
  627. return -ENOMEM;
  628. tp->offset = (unsigned long)(paddr - eaddr);
  629. } else
  630. /* This function has no name. */
  631. tp->offset = (unsigned long)paddr;
  632. /* Return probe must be on the head of a subprogram */
  633. if (retprobe) {
  634. if (eaddr != paddr) {
  635. pr_warning("Return probe must be on the head of"
  636. " a real function.\n");
  637. return -EINVAL;
  638. }
  639. tp->retprobe = true;
  640. }
  641. return 0;
  642. }
  643. /* Call probe_finder callback with scope DIE */
  644. static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
  645. {
  646. Dwarf_Attribute fb_attr;
  647. size_t nops;
  648. int ret;
  649. if (!sc_die) {
  650. pr_err("Caller must pass a scope DIE. Program error.\n");
  651. return -EINVAL;
  652. }
  653. /* If not a real subprogram, find a real one */
  654. if (!die_is_func_def(sc_die)) {
  655. if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
  656. pr_warning("Failed to find probe point in any "
  657. "functions.\n");
  658. return -ENOENT;
  659. }
  660. } else
  661. memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
  662. /* Get the frame base attribute/ops from subprogram */
  663. dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
  664. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  665. if (ret <= 0 || nops == 0) {
  666. pf->fb_ops = NULL;
  667. #if _ELFUTILS_PREREQ(0, 142)
  668. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  669. pf->cfi != NULL) {
  670. Dwarf_Frame *frame;
  671. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  672. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  673. pr_warning("Failed to get call frame on 0x%jx\n",
  674. (uintmax_t)pf->addr);
  675. return -ENOENT;
  676. }
  677. #endif
  678. }
  679. /* Call finder's callback handler */
  680. ret = pf->callback(sc_die, pf);
  681. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  682. pf->fb_ops = NULL;
  683. return ret;
  684. }
  685. struct find_scope_param {
  686. const char *function;
  687. const char *file;
  688. int line;
  689. int diff;
  690. Dwarf_Die *die_mem;
  691. bool found;
  692. };
  693. static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
  694. {
  695. struct find_scope_param *fsp = data;
  696. const char *file;
  697. int lno;
  698. /* Skip if declared file name does not match */
  699. if (fsp->file) {
  700. file = dwarf_decl_file(fn_die);
  701. if (!file || strcmp(fsp->file, file) != 0)
  702. return 0;
  703. }
  704. /* If the function name is given, that's what user expects */
  705. if (fsp->function) {
  706. if (die_compare_name(fn_die, fsp->function)) {
  707. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  708. fsp->found = true;
  709. return 1;
  710. }
  711. } else {
  712. /* With the line number, find the nearest declared DIE */
  713. dwarf_decl_line(fn_die, &lno);
  714. if (lno < fsp->line && fsp->diff > fsp->line - lno) {
  715. /* Keep a candidate and continue */
  716. fsp->diff = fsp->line - lno;
  717. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  718. fsp->found = true;
  719. }
  720. }
  721. return 0;
  722. }
  723. /* Find an appropriate scope fits to given conditions */
  724. static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
  725. {
  726. struct find_scope_param fsp = {
  727. .function = pf->pev->point.function,
  728. .file = pf->fname,
  729. .line = pf->lno,
  730. .diff = INT_MAX,
  731. .die_mem = die_mem,
  732. .found = false,
  733. };
  734. cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
  735. return fsp.found ? die_mem : NULL;
  736. }
  737. static int probe_point_line_walker(const char *fname, int lineno,
  738. Dwarf_Addr addr, void *data)
  739. {
  740. struct probe_finder *pf = data;
  741. Dwarf_Die *sc_die, die_mem;
  742. int ret;
  743. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  744. return 0;
  745. pf->addr = addr;
  746. sc_die = find_best_scope(pf, &die_mem);
  747. if (!sc_die) {
  748. pr_warning("Failed to find scope of probe point.\n");
  749. return -ENOENT;
  750. }
  751. ret = call_probe_finder(sc_die, pf);
  752. /* Continue if no error, because the line will be in inline function */
  753. return ret < 0 ? ret : 0;
  754. }
  755. /* Find probe point from its line number */
  756. static int find_probe_point_by_line(struct probe_finder *pf)
  757. {
  758. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  759. }
  760. /* Find lines which match lazy pattern */
  761. static int find_lazy_match_lines(struct list_head *head,
  762. const char *fname, const char *pat)
  763. {
  764. FILE *fp;
  765. char *line = NULL;
  766. size_t line_len;
  767. ssize_t len;
  768. int count = 0, linenum = 1;
  769. fp = fopen(fname, "r");
  770. if (!fp) {
  771. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  772. return -errno;
  773. }
  774. while ((len = getline(&line, &line_len, fp)) > 0) {
  775. if (line[len - 1] == '\n')
  776. line[len - 1] = '\0';
  777. if (strlazymatch(line, pat)) {
  778. line_list__add_line(head, linenum);
  779. count++;
  780. }
  781. linenum++;
  782. }
  783. if (ferror(fp))
  784. count = -errno;
  785. free(line);
  786. fclose(fp);
  787. if (count == 0)
  788. pr_debug("No matched lines found in %s.\n", fname);
  789. return count;
  790. }
  791. static int probe_point_lazy_walker(const char *fname, int lineno,
  792. Dwarf_Addr addr, void *data)
  793. {
  794. struct probe_finder *pf = data;
  795. Dwarf_Die *sc_die, die_mem;
  796. int ret;
  797. if (!line_list__has_line(&pf->lcache, lineno) ||
  798. strtailcmp(fname, pf->fname) != 0)
  799. return 0;
  800. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  801. lineno, (unsigned long long)addr);
  802. pf->addr = addr;
  803. pf->lno = lineno;
  804. sc_die = find_best_scope(pf, &die_mem);
  805. if (!sc_die) {
  806. pr_warning("Failed to find scope of probe point.\n");
  807. return -ENOENT;
  808. }
  809. ret = call_probe_finder(sc_die, pf);
  810. /*
  811. * Continue if no error, because the lazy pattern will match
  812. * to other lines
  813. */
  814. return ret < 0 ? ret : 0;
  815. }
  816. /* Find probe points from lazy pattern */
  817. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  818. {
  819. int ret = 0;
  820. if (list_empty(&pf->lcache)) {
  821. /* Matching lazy line pattern */
  822. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  823. pf->pev->point.lazy_line);
  824. if (ret <= 0)
  825. return ret;
  826. }
  827. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  828. }
  829. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  830. {
  831. struct probe_finder *pf = data;
  832. struct perf_probe_point *pp = &pf->pev->point;
  833. Dwarf_Addr addr;
  834. int ret;
  835. if (pp->lazy_line)
  836. ret = find_probe_point_lazy(in_die, pf);
  837. else {
  838. /* Get probe address */
  839. if (dwarf_entrypc(in_die, &addr) != 0) {
  840. pr_warning("Failed to get entry address of %s.\n",
  841. dwarf_diename(in_die));
  842. return -ENOENT;
  843. }
  844. pf->addr = addr;
  845. pf->addr += pp->offset;
  846. pr_debug("found inline addr: 0x%jx\n",
  847. (uintmax_t)pf->addr);
  848. ret = call_probe_finder(in_die, pf);
  849. }
  850. return ret;
  851. }
  852. /* Callback parameter with return value for libdw */
  853. struct dwarf_callback_param {
  854. void *data;
  855. int retval;
  856. };
  857. /* Search function from function name */
  858. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  859. {
  860. struct dwarf_callback_param *param = data;
  861. struct probe_finder *pf = param->data;
  862. struct perf_probe_point *pp = &pf->pev->point;
  863. /* Check tag and diename */
  864. if (!die_is_func_def(sp_die) ||
  865. !die_compare_name(sp_die, pp->function))
  866. return DWARF_CB_OK;
  867. /* Check declared file */
  868. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  869. return DWARF_CB_OK;
  870. pf->fname = dwarf_decl_file(sp_die);
  871. if (pp->line) { /* Function relative line */
  872. dwarf_decl_line(sp_die, &pf->lno);
  873. pf->lno += pp->line;
  874. param->retval = find_probe_point_by_line(pf);
  875. } else if (!dwarf_func_inline(sp_die)) {
  876. /* Real function */
  877. if (pp->lazy_line)
  878. param->retval = find_probe_point_lazy(sp_die, pf);
  879. else {
  880. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  881. pr_warning("Failed to get entry address of "
  882. "%s.\n", dwarf_diename(sp_die));
  883. param->retval = -ENOENT;
  884. return DWARF_CB_ABORT;
  885. }
  886. pf->addr += pp->offset;
  887. /* TODO: Check the address in this function */
  888. param->retval = call_probe_finder(sp_die, pf);
  889. }
  890. } else
  891. /* Inlined function: search instances */
  892. param->retval = die_walk_instances(sp_die,
  893. probe_point_inline_cb, (void *)pf);
  894. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  895. }
  896. static int find_probe_point_by_func(struct probe_finder *pf)
  897. {
  898. struct dwarf_callback_param _param = {.data = (void *)pf,
  899. .retval = 0};
  900. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  901. return _param.retval;
  902. }
  903. struct pubname_callback_param {
  904. char *function;
  905. char *file;
  906. Dwarf_Die *cu_die;
  907. Dwarf_Die *sp_die;
  908. int found;
  909. };
  910. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  911. {
  912. struct pubname_callback_param *param = data;
  913. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  914. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  915. return DWARF_CB_OK;
  916. if (die_compare_name(param->sp_die, param->function)) {
  917. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  918. return DWARF_CB_OK;
  919. if (param->file &&
  920. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  921. return DWARF_CB_OK;
  922. param->found = 1;
  923. return DWARF_CB_ABORT;
  924. }
  925. }
  926. return DWARF_CB_OK;
  927. }
  928. /* Find probe points from debuginfo */
  929. static int debuginfo__find_probes(struct debuginfo *self,
  930. struct probe_finder *pf)
  931. {
  932. struct perf_probe_point *pp = &pf->pev->point;
  933. Dwarf_Off off, noff;
  934. size_t cuhl;
  935. Dwarf_Die *diep;
  936. int ret = 0;
  937. #if _ELFUTILS_PREREQ(0, 142)
  938. /* Get the call frame information from this dwarf */
  939. pf->cfi = dwarf_getcfi(self->dbg);
  940. #endif
  941. off = 0;
  942. line_list__init(&pf->lcache);
  943. /* Fastpath: lookup by function name from .debug_pubnames section */
  944. if (pp->function) {
  945. struct pubname_callback_param pubname_param = {
  946. .function = pp->function,
  947. .file = pp->file,
  948. .cu_die = &pf->cu_die,
  949. .sp_die = &pf->sp_die,
  950. .found = 0,
  951. };
  952. struct dwarf_callback_param probe_param = {
  953. .data = pf,
  954. };
  955. dwarf_getpubnames(self->dbg, pubname_search_cb,
  956. &pubname_param, 0);
  957. if (pubname_param.found) {
  958. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  959. if (ret)
  960. goto found;
  961. }
  962. }
  963. /* Loop on CUs (Compilation Unit) */
  964. while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  965. /* Get the DIE(Debugging Information Entry) of this CU */
  966. diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die);
  967. if (!diep)
  968. continue;
  969. /* Check if target file is included. */
  970. if (pp->file)
  971. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  972. else
  973. pf->fname = NULL;
  974. if (!pp->file || pf->fname) {
  975. if (pp->function)
  976. ret = find_probe_point_by_func(pf);
  977. else if (pp->lazy_line)
  978. ret = find_probe_point_lazy(NULL, pf);
  979. else {
  980. pf->lno = pp->line;
  981. ret = find_probe_point_by_line(pf);
  982. }
  983. if (ret < 0)
  984. break;
  985. }
  986. off = noff;
  987. }
  988. found:
  989. line_list__free(&pf->lcache);
  990. return ret;
  991. }
  992. /* Add a found probe point into trace event list */
  993. static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
  994. {
  995. struct trace_event_finder *tf =
  996. container_of(pf, struct trace_event_finder, pf);
  997. struct probe_trace_event *tev;
  998. int ret, i;
  999. /* Check number of tevs */
  1000. if (tf->ntevs == tf->max_tevs) {
  1001. pr_warning("Too many( > %d) probe point found.\n",
  1002. tf->max_tevs);
  1003. return -ERANGE;
  1004. }
  1005. tev = &tf->tevs[tf->ntevs++];
  1006. /* Trace point should be converted from subprogram DIE */
  1007. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  1008. pf->pev->point.retprobe, &tev->point);
  1009. if (ret < 0)
  1010. return ret;
  1011. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1012. tev->point.offset);
  1013. /* Find each argument */
  1014. tev->nargs = pf->pev->nargs;
  1015. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1016. if (tev->args == NULL)
  1017. return -ENOMEM;
  1018. for (i = 0; i < pf->pev->nargs; i++) {
  1019. pf->pvar = &pf->pev->args[i];
  1020. pf->tvar = &tev->args[i];
  1021. /* Variable should be found from scope DIE */
  1022. ret = find_variable(sc_die, pf);
  1023. if (ret != 0)
  1024. return ret;
  1025. }
  1026. return 0;
  1027. }
  1028. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1029. int debuginfo__find_trace_events(struct debuginfo *self,
  1030. struct perf_probe_event *pev,
  1031. struct probe_trace_event **tevs, int max_tevs)
  1032. {
  1033. struct trace_event_finder tf = {
  1034. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1035. .max_tevs = max_tevs};
  1036. int ret;
  1037. /* Allocate result tevs array */
  1038. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1039. if (*tevs == NULL)
  1040. return -ENOMEM;
  1041. tf.tevs = *tevs;
  1042. tf.ntevs = 0;
  1043. ret = debuginfo__find_probes(self, &tf.pf);
  1044. if (ret < 0) {
  1045. free(*tevs);
  1046. *tevs = NULL;
  1047. return ret;
  1048. }
  1049. return (ret < 0) ? ret : tf.ntevs;
  1050. }
  1051. #define MAX_VAR_LEN 64
  1052. /* Collect available variables in this scope */
  1053. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1054. {
  1055. struct available_var_finder *af = data;
  1056. struct variable_list *vl;
  1057. char buf[MAX_VAR_LEN];
  1058. int tag, ret;
  1059. vl = &af->vls[af->nvls - 1];
  1060. tag = dwarf_tag(die_mem);
  1061. if (tag == DW_TAG_formal_parameter ||
  1062. tag == DW_TAG_variable) {
  1063. ret = convert_variable_location(die_mem, af->pf.addr,
  1064. af->pf.fb_ops, NULL);
  1065. if (ret == 0) {
  1066. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1067. pr_debug2("Add new var: %s\n", buf);
  1068. if (ret > 0)
  1069. strlist__add(vl->vars, buf);
  1070. }
  1071. }
  1072. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1073. return DIE_FIND_CB_CONTINUE;
  1074. else
  1075. return DIE_FIND_CB_SIBLING;
  1076. }
  1077. /* Add a found vars into available variables list */
  1078. static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
  1079. {
  1080. struct available_var_finder *af =
  1081. container_of(pf, struct available_var_finder, pf);
  1082. struct variable_list *vl;
  1083. Dwarf_Die die_mem;
  1084. int ret;
  1085. /* Check number of tevs */
  1086. if (af->nvls == af->max_vls) {
  1087. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1088. return -ERANGE;
  1089. }
  1090. vl = &af->vls[af->nvls++];
  1091. /* Trace point should be converted from subprogram DIE */
  1092. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  1093. pf->pev->point.retprobe, &vl->point);
  1094. if (ret < 0)
  1095. return ret;
  1096. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1097. vl->point.offset);
  1098. /* Find local variables */
  1099. vl->vars = strlist__new(true, NULL);
  1100. if (vl->vars == NULL)
  1101. return -ENOMEM;
  1102. af->child = true;
  1103. die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
  1104. /* Find external variables */
  1105. if (!af->externs)
  1106. goto out;
  1107. /* Don't need to search child DIE for externs. */
  1108. af->child = false;
  1109. die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
  1110. out:
  1111. if (strlist__empty(vl->vars)) {
  1112. strlist__delete(vl->vars);
  1113. vl->vars = NULL;
  1114. }
  1115. return ret;
  1116. }
  1117. /* Find available variables at given probe point */
  1118. int debuginfo__find_available_vars_at(struct debuginfo *self,
  1119. struct perf_probe_event *pev,
  1120. struct variable_list **vls,
  1121. int max_vls, bool externs)
  1122. {
  1123. struct available_var_finder af = {
  1124. .pf = {.pev = pev, .callback = add_available_vars},
  1125. .max_vls = max_vls, .externs = externs};
  1126. int ret;
  1127. /* Allocate result vls array */
  1128. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1129. if (*vls == NULL)
  1130. return -ENOMEM;
  1131. af.vls = *vls;
  1132. af.nvls = 0;
  1133. ret = debuginfo__find_probes(self, &af.pf);
  1134. if (ret < 0) {
  1135. /* Free vlist for error */
  1136. while (af.nvls--) {
  1137. if (af.vls[af.nvls].point.symbol)
  1138. free(af.vls[af.nvls].point.symbol);
  1139. if (af.vls[af.nvls].vars)
  1140. strlist__delete(af.vls[af.nvls].vars);
  1141. }
  1142. free(af.vls);
  1143. *vls = NULL;
  1144. return ret;
  1145. }
  1146. return (ret < 0) ? ret : af.nvls;
  1147. }
  1148. /* Reverse search */
  1149. int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr,
  1150. struct perf_probe_point *ppt)
  1151. {
  1152. Dwarf_Die cudie, spdie, indie;
  1153. Dwarf_Addr _addr, baseaddr;
  1154. const char *fname = NULL, *func = NULL, *tmp;
  1155. int baseline = 0, lineno = 0, ret = 0;
  1156. /* Adjust address with bias */
  1157. addr += self->bias;
  1158. /* Find cu die */
  1159. if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) {
  1160. pr_warning("Failed to find debug information for address %lx\n",
  1161. addr);
  1162. ret = -EINVAL;
  1163. goto end;
  1164. }
  1165. /* Find a corresponding line (filename and lineno) */
  1166. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1167. /* Don't care whether it failed or not */
  1168. /* Find a corresponding function (name, baseline and baseaddr) */
  1169. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1170. /* Get function entry information */
  1171. tmp = dwarf_diename(&spdie);
  1172. if (!tmp ||
  1173. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1174. dwarf_decl_line(&spdie, &baseline) != 0)
  1175. goto post;
  1176. func = tmp;
  1177. if (addr == (unsigned long)baseaddr)
  1178. /* Function entry - Relative line number is 0 */
  1179. lineno = baseline;
  1180. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1181. &indie)) {
  1182. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1183. _addr == addr)
  1184. /*
  1185. * addr is at an inline function entry.
  1186. * In this case, lineno should be the call-site
  1187. * line number.
  1188. */
  1189. lineno = die_get_call_lineno(&indie);
  1190. else {
  1191. /*
  1192. * addr is in an inline function body.
  1193. * Since lineno points one of the lines
  1194. * of the inline function, baseline should
  1195. * be the entry line of the inline function.
  1196. */
  1197. tmp = dwarf_diename(&indie);
  1198. if (tmp &&
  1199. dwarf_decl_line(&spdie, &baseline) == 0)
  1200. func = tmp;
  1201. }
  1202. }
  1203. }
  1204. post:
  1205. /* Make a relative line number or an offset */
  1206. if (lineno)
  1207. ppt->line = lineno - baseline;
  1208. else if (func)
  1209. ppt->offset = addr - (unsigned long)baseaddr;
  1210. /* Duplicate strings */
  1211. if (func) {
  1212. ppt->function = strdup(func);
  1213. if (ppt->function == NULL) {
  1214. ret = -ENOMEM;
  1215. goto end;
  1216. }
  1217. }
  1218. if (fname) {
  1219. ppt->file = strdup(fname);
  1220. if (ppt->file == NULL) {
  1221. if (ppt->function) {
  1222. free(ppt->function);
  1223. ppt->function = NULL;
  1224. }
  1225. ret = -ENOMEM;
  1226. goto end;
  1227. }
  1228. }
  1229. end:
  1230. if (ret == 0 && (fname || func))
  1231. ret = 1; /* Found a point */
  1232. return ret;
  1233. }
  1234. /* Add a line and store the src path */
  1235. static int line_range_add_line(const char *src, unsigned int lineno,
  1236. struct line_range *lr)
  1237. {
  1238. /* Copy source path */
  1239. if (!lr->path) {
  1240. lr->path = strdup(src);
  1241. if (lr->path == NULL)
  1242. return -ENOMEM;
  1243. }
  1244. return line_list__add_line(&lr->line_list, lineno);
  1245. }
  1246. static int line_range_walk_cb(const char *fname, int lineno,
  1247. Dwarf_Addr addr __maybe_unused,
  1248. void *data)
  1249. {
  1250. struct line_finder *lf = data;
  1251. if ((strtailcmp(fname, lf->fname) != 0) ||
  1252. (lf->lno_s > lineno || lf->lno_e < lineno))
  1253. return 0;
  1254. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1255. return -EINVAL;
  1256. return 0;
  1257. }
  1258. /* Find line range from its line number */
  1259. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1260. {
  1261. int ret;
  1262. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1263. /* Update status */
  1264. if (ret >= 0)
  1265. if (!list_empty(&lf->lr->line_list))
  1266. ret = lf->found = 1;
  1267. else
  1268. ret = 0; /* Lines are not found */
  1269. else {
  1270. free(lf->lr->path);
  1271. lf->lr->path = NULL;
  1272. }
  1273. return ret;
  1274. }
  1275. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1276. {
  1277. find_line_range_by_line(in_die, data);
  1278. /*
  1279. * We have to check all instances of inlined function, because
  1280. * some execution paths can be optimized out depends on the
  1281. * function argument of instances
  1282. */
  1283. return 0;
  1284. }
  1285. /* Search function definition from function name */
  1286. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1287. {
  1288. struct dwarf_callback_param *param = data;
  1289. struct line_finder *lf = param->data;
  1290. struct line_range *lr = lf->lr;
  1291. /* Check declared file */
  1292. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1293. return DWARF_CB_OK;
  1294. if (die_is_func_def(sp_die) &&
  1295. die_compare_name(sp_die, lr->function)) {
  1296. lf->fname = dwarf_decl_file(sp_die);
  1297. dwarf_decl_line(sp_die, &lr->offset);
  1298. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1299. lf->lno_s = lr->offset + lr->start;
  1300. if (lf->lno_s < 0) /* Overflow */
  1301. lf->lno_s = INT_MAX;
  1302. lf->lno_e = lr->offset + lr->end;
  1303. if (lf->lno_e < 0) /* Overflow */
  1304. lf->lno_e = INT_MAX;
  1305. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1306. lr->start = lf->lno_s;
  1307. lr->end = lf->lno_e;
  1308. if (dwarf_func_inline(sp_die))
  1309. param->retval = die_walk_instances(sp_die,
  1310. line_range_inline_cb, lf);
  1311. else
  1312. param->retval = find_line_range_by_line(sp_die, lf);
  1313. return DWARF_CB_ABORT;
  1314. }
  1315. return DWARF_CB_OK;
  1316. }
  1317. static int find_line_range_by_func(struct line_finder *lf)
  1318. {
  1319. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1320. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1321. return param.retval;
  1322. }
  1323. int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
  1324. {
  1325. struct line_finder lf = {.lr = lr, .found = 0};
  1326. int ret = 0;
  1327. Dwarf_Off off = 0, noff;
  1328. size_t cuhl;
  1329. Dwarf_Die *diep;
  1330. const char *comp_dir;
  1331. /* Fastpath: lookup by function name from .debug_pubnames section */
  1332. if (lr->function) {
  1333. struct pubname_callback_param pubname_param = {
  1334. .function = lr->function, .file = lr->file,
  1335. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1336. struct dwarf_callback_param line_range_param = {
  1337. .data = (void *)&lf, .retval = 0};
  1338. dwarf_getpubnames(self->dbg, pubname_search_cb,
  1339. &pubname_param, 0);
  1340. if (pubname_param.found) {
  1341. line_range_search_cb(&lf.sp_die, &line_range_param);
  1342. if (lf.found)
  1343. goto found;
  1344. }
  1345. }
  1346. /* Loop on CUs (Compilation Unit) */
  1347. while (!lf.found && ret >= 0) {
  1348. if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
  1349. NULL, NULL, NULL) != 0)
  1350. break;
  1351. /* Get the DIE(Debugging Information Entry) of this CU */
  1352. diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
  1353. if (!diep)
  1354. continue;
  1355. /* Check if target file is included. */
  1356. if (lr->file)
  1357. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1358. else
  1359. lf.fname = 0;
  1360. if (!lr->file || lf.fname) {
  1361. if (lr->function)
  1362. ret = find_line_range_by_func(&lf);
  1363. else {
  1364. lf.lno_s = lr->start;
  1365. lf.lno_e = lr->end;
  1366. ret = find_line_range_by_line(NULL, &lf);
  1367. }
  1368. }
  1369. off = noff;
  1370. }
  1371. found:
  1372. /* Store comp_dir */
  1373. if (lf.found) {
  1374. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1375. if (comp_dir) {
  1376. lr->comp_dir = strdup(comp_dir);
  1377. if (!lr->comp_dir)
  1378. ret = -ENOMEM;
  1379. }
  1380. }
  1381. pr_debug("path: %s\n", lr->path);
  1382. return (ret < 0) ? ret : lf.found;
  1383. }