probe-finder.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 <ctype.h>
  33. #include "string.h"
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "probe-finder.h"
  38. /*
  39. * Generic dwarf analysis helpers
  40. */
  41. #define X86_32_MAX_REGS 8
  42. const char *x86_32_regs_table[X86_32_MAX_REGS] = {
  43. "%ax",
  44. "%cx",
  45. "%dx",
  46. "%bx",
  47. "$stack", /* Stack address instead of %sp */
  48. "%bp",
  49. "%si",
  50. "%di",
  51. };
  52. #define X86_64_MAX_REGS 16
  53. const char *x86_64_regs_table[X86_64_MAX_REGS] = {
  54. "%ax",
  55. "%dx",
  56. "%cx",
  57. "%bx",
  58. "%si",
  59. "%di",
  60. "%bp",
  61. "%sp",
  62. "%r8",
  63. "%r9",
  64. "%r10",
  65. "%r11",
  66. "%r12",
  67. "%r13",
  68. "%r14",
  69. "%r15",
  70. };
  71. /* TODO: switching by dwarf address size */
  72. #ifdef __x86_64__
  73. #define ARCH_MAX_REGS X86_64_MAX_REGS
  74. #define arch_regs_table x86_64_regs_table
  75. #else
  76. #define ARCH_MAX_REGS X86_32_MAX_REGS
  77. #define arch_regs_table x86_32_regs_table
  78. #endif
  79. /* Return architecture dependent register string (for kprobe-tracer) */
  80. static const char *get_arch_regstr(unsigned int n)
  81. {
  82. return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
  83. }
  84. /*
  85. * Compare the tail of two strings.
  86. * Return 0 if whole of either string is same as another's tail part.
  87. */
  88. static int strtailcmp(const char *s1, const char *s2)
  89. {
  90. int i1 = strlen(s1);
  91. int i2 = strlen(s2);
  92. while (--i1 >= 0 && --i2 >= 0) {
  93. if (s1[i1] != s2[i2])
  94. return s1[i1] - s2[i2];
  95. }
  96. return 0;
  97. }
  98. /* Line number list operations */
  99. /* Add a line to line number list */
  100. static void line_list__add_line(struct list_head *head, unsigned int line)
  101. {
  102. struct line_node *ln;
  103. struct list_head *p;
  104. /* Reverse search, because new line will be the last one */
  105. list_for_each_entry_reverse(ln, head, list) {
  106. if (ln->line < line) {
  107. p = &ln->list;
  108. goto found;
  109. } else if (ln->line == line) /* Already exist */
  110. return ;
  111. }
  112. /* List is empty, or the smallest entry */
  113. p = head;
  114. found:
  115. pr_debug("line list: add a line %u\n", line);
  116. ln = xzalloc(sizeof(struct line_node));
  117. ln->line = line;
  118. INIT_LIST_HEAD(&ln->list);
  119. list_add(&ln->list, p);
  120. }
  121. /* Check if the line in line number list */
  122. static int line_list__has_line(struct list_head *head, unsigned int line)
  123. {
  124. struct line_node *ln;
  125. /* Reverse search, because new line will be the last one */
  126. list_for_each_entry(ln, head, list)
  127. if (ln->line == line)
  128. return 1;
  129. return 0;
  130. }
  131. /* Init line number list */
  132. static void line_list__init(struct list_head *head)
  133. {
  134. INIT_LIST_HEAD(head);
  135. }
  136. /* Free line number list */
  137. static void line_list__free(struct list_head *head)
  138. {
  139. struct line_node *ln;
  140. while (!list_empty(head)) {
  141. ln = list_first_entry(head, struct line_node, list);
  142. list_del(&ln->list);
  143. free(ln);
  144. }
  145. }
  146. /* Dwarf wrappers */
  147. /* Find the realpath of the target file. */
  148. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  149. {
  150. Dwarf_Files *files;
  151. size_t nfiles, i;
  152. const char *src = NULL;
  153. int ret;
  154. if (!fname)
  155. return NULL;
  156. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  157. if (ret != 0)
  158. return NULL;
  159. for (i = 0; i < nfiles; i++) {
  160. src = dwarf_filesrc(files, i, NULL, NULL);
  161. if (strtailcmp(src, fname) == 0)
  162. break;
  163. }
  164. return src;
  165. }
  166. /* Compare diename and tname */
  167. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  168. {
  169. const char *name;
  170. name = dwarf_diename(dw_die);
  171. DIE_IF(name == NULL);
  172. return strcmp(tname, name);
  173. }
  174. /* Get entry pc(or low pc, 1st entry of ranges) of the die */
  175. static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
  176. {
  177. Dwarf_Addr epc;
  178. int ret;
  179. ret = dwarf_entrypc(dw_die, &epc);
  180. DIE_IF(ret == -1);
  181. return epc;
  182. }
  183. /* Get type die, but skip qualifiers and typedef */
  184. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  185. {
  186. Dwarf_Attribute attr;
  187. int tag;
  188. do {
  189. if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
  190. dwarf_formref_die(&attr, die_mem) == NULL)
  191. return NULL;
  192. tag = dwarf_tag(die_mem);
  193. vr_die = die_mem;
  194. } while (tag == DW_TAG_const_type ||
  195. tag == DW_TAG_restrict_type ||
  196. tag == DW_TAG_volatile_type ||
  197. tag == DW_TAG_shared_type ||
  198. tag == DW_TAG_typedef);
  199. return die_mem;
  200. }
  201. /* Return values for die_find callbacks */
  202. enum {
  203. DIE_FIND_CB_FOUND = 0, /* End of Search */
  204. DIE_FIND_CB_CHILD = 1, /* Search only children */
  205. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  206. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  207. };
  208. /* Search a child die */
  209. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  210. int (*callback)(Dwarf_Die *, void *),
  211. void *data, Dwarf_Die *die_mem)
  212. {
  213. Dwarf_Die child_die;
  214. int ret;
  215. ret = dwarf_child(rt_die, die_mem);
  216. if (ret != 0)
  217. return NULL;
  218. do {
  219. ret = callback(die_mem, data);
  220. if (ret == DIE_FIND_CB_FOUND)
  221. return die_mem;
  222. if ((ret & DIE_FIND_CB_CHILD) &&
  223. die_find_child(die_mem, callback, data, &child_die)) {
  224. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  225. return die_mem;
  226. }
  227. } while ((ret & DIE_FIND_CB_SIBLING) &&
  228. dwarf_siblingof(die_mem, die_mem) == 0);
  229. return NULL;
  230. }
  231. struct __addr_die_search_param {
  232. Dwarf_Addr addr;
  233. Dwarf_Die *die_mem;
  234. };
  235. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  236. {
  237. struct __addr_die_search_param *ad = data;
  238. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  239. dwarf_haspc(fn_die, ad->addr)) {
  240. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  241. return DWARF_CB_ABORT;
  242. }
  243. return DWARF_CB_OK;
  244. }
  245. /* Search a real subprogram including this line, */
  246. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  247. Dwarf_Die *die_mem)
  248. {
  249. struct __addr_die_search_param ad;
  250. ad.addr = addr;
  251. ad.die_mem = die_mem;
  252. /* dwarf_getscopes can't find subprogram. */
  253. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  254. return NULL;
  255. else
  256. return die_mem;
  257. }
  258. /* die_find callback for inline function search */
  259. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  260. {
  261. Dwarf_Addr *addr = data;
  262. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  263. dwarf_haspc(die_mem, *addr))
  264. return DIE_FIND_CB_FOUND;
  265. return DIE_FIND_CB_CONTINUE;
  266. }
  267. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  268. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  269. Dwarf_Die *die_mem)
  270. {
  271. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  272. }
  273. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  274. {
  275. const char *name = data;
  276. int tag;
  277. tag = dwarf_tag(die_mem);
  278. if ((tag == DW_TAG_formal_parameter ||
  279. tag == DW_TAG_variable) &&
  280. (die_compare_name(die_mem, name) == 0))
  281. return DIE_FIND_CB_FOUND;
  282. return DIE_FIND_CB_CONTINUE;
  283. }
  284. /* Find a variable called 'name' */
  285. static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
  286. Dwarf_Die *die_mem)
  287. {
  288. return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
  289. die_mem);
  290. }
  291. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  292. {
  293. const char *name = data;
  294. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  295. (die_compare_name(die_mem, name) == 0))
  296. return DIE_FIND_CB_FOUND;
  297. return DIE_FIND_CB_SIBLING;
  298. }
  299. /* Find a member called 'name' */
  300. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  301. Dwarf_Die *die_mem)
  302. {
  303. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  304. die_mem);
  305. }
  306. /*
  307. * Probe finder related functions
  308. */
  309. /* Show a location */
  310. static void convert_location(Dwarf_Op *op, struct probe_finder *pf)
  311. {
  312. unsigned int regn;
  313. Dwarf_Word offs = 0;
  314. bool ref = false;
  315. const char *regs;
  316. struct kprobe_trace_arg *tvar = pf->tvar;
  317. /* TODO: support CFA */
  318. /* If this is based on frame buffer, set the offset */
  319. if (op->atom == DW_OP_fbreg) {
  320. if (pf->fb_ops == NULL)
  321. die("The attribute of frame base is not supported.\n");
  322. ref = true;
  323. offs = op->number;
  324. op = &pf->fb_ops[0];
  325. }
  326. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  327. regn = op->atom - DW_OP_breg0;
  328. offs += op->number;
  329. ref = true;
  330. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  331. regn = op->atom - DW_OP_reg0;
  332. } else if (op->atom == DW_OP_bregx) {
  333. regn = op->number;
  334. offs += op->number2;
  335. ref = true;
  336. } else if (op->atom == DW_OP_regx) {
  337. regn = op->number;
  338. } else
  339. die("DW_OP %d is not supported.", op->atom);
  340. regs = get_arch_regstr(regn);
  341. if (!regs)
  342. die("%u exceeds max register number.", regn);
  343. tvar->value = xstrdup(regs);
  344. if (ref) {
  345. tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
  346. tvar->ref->offset = (long)offs;
  347. }
  348. }
  349. static void convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  350. struct perf_probe_arg_field *field,
  351. struct kprobe_trace_arg_ref **ref_ptr)
  352. {
  353. struct kprobe_trace_arg_ref *ref = *ref_ptr;
  354. Dwarf_Attribute attr;
  355. Dwarf_Die member;
  356. Dwarf_Die type;
  357. Dwarf_Word offs;
  358. pr_debug("converting %s in %s\n", field->name, varname);
  359. if (die_get_real_type(vr_die, &type) == NULL)
  360. die("Failed to get a type information of %s.", varname);
  361. /* Check the pointer and dereference */
  362. if (dwarf_tag(&type) == DW_TAG_pointer_type) {
  363. if (!field->ref)
  364. die("Semantic error: %s must be referred by '->'",
  365. field->name);
  366. /* Get the type pointed by this pointer */
  367. if (die_get_real_type(&type, &type) == NULL)
  368. die("Failed to get a type information of %s.", varname);
  369. ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
  370. if (*ref_ptr)
  371. (*ref_ptr)->next = ref;
  372. else
  373. *ref_ptr = ref;
  374. } else {
  375. if (field->ref)
  376. die("Semantic error: %s must be referred by '.'",
  377. field->name);
  378. if (!ref)
  379. die("Structure on a register is not supported yet.");
  380. }
  381. /* Verify it is a data structure */
  382. if (dwarf_tag(&type) != DW_TAG_structure_type)
  383. die("%s is not a data structure.", varname);
  384. if (die_find_member(&type, field->name, &member) == NULL)
  385. die("%s(tyep:%s) has no member %s.", varname,
  386. dwarf_diename(&type), field->name);
  387. /* Get the offset of the field */
  388. if (dwarf_attr(&member, DW_AT_data_member_location, &attr) == NULL ||
  389. dwarf_formudata(&attr, &offs) != 0)
  390. die("Failed to get the offset of %s.", field->name);
  391. ref->offset += (long)offs;
  392. /* Converting next field */
  393. if (field->next)
  394. convert_variable_fields(&member, field->name, field->next,
  395. &ref);
  396. }
  397. /* Show a variables in kprobe event format */
  398. static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  399. {
  400. Dwarf_Attribute attr;
  401. Dwarf_Op *expr;
  402. size_t nexpr;
  403. int ret;
  404. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  405. goto error;
  406. /* TODO: handle more than 1 exprs */
  407. ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
  408. if (ret <= 0 || nexpr == 0)
  409. goto error;
  410. convert_location(expr, pf);
  411. if (pf->pvar->field)
  412. convert_variable_fields(vr_die, pf->pvar->name,
  413. pf->pvar->field, &pf->tvar->ref);
  414. /* *expr will be cached in libdw. Don't free it. */
  415. return ;
  416. error:
  417. /* TODO: Support const_value */
  418. die("Failed to find the location of %s at this address.\n"
  419. " Perhaps, it has been optimized out.", pf->pvar->name);
  420. }
  421. /* Find a variable in a subprogram die */
  422. static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  423. {
  424. Dwarf_Die vr_die;
  425. char buf[128];
  426. /* TODO: Support struct members and arrays */
  427. if (!is_c_varname(pf->pvar->name)) {
  428. /* Copy raw parameters */
  429. pf->tvar->value = xstrdup(pf->pvar->name);
  430. } else {
  431. synthesize_perf_probe_arg(pf->pvar, buf, 128);
  432. pf->tvar->name = xstrdup(buf);
  433. pr_debug("Searching '%s' variable in context.\n",
  434. pf->pvar->name);
  435. /* Search child die for local variables and parameters. */
  436. if (!die_find_variable(sp_die, pf->pvar->name, &vr_die))
  437. die("Failed to find '%s' in this function.",
  438. pf->pvar->name);
  439. convert_variable(&vr_die, pf);
  440. }
  441. }
  442. /* Show a probe point to output buffer */
  443. static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  444. {
  445. struct kprobe_trace_event *tev;
  446. Dwarf_Addr eaddr;
  447. Dwarf_Die die_mem;
  448. const char *name;
  449. int ret, i;
  450. Dwarf_Attribute fb_attr;
  451. size_t nops;
  452. if (pf->ntevs == MAX_PROBES)
  453. die("Too many( > %d) probe point found.\n", MAX_PROBES);
  454. tev = &pf->tevs[pf->ntevs++];
  455. /* If no real subprogram, find a real one */
  456. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  457. sp_die = die_find_real_subprogram(&pf->cu_die,
  458. pf->addr, &die_mem);
  459. if (!sp_die)
  460. die("Probe point is not found in subprograms.");
  461. }
  462. /* Copy the name of probe point */
  463. name = dwarf_diename(sp_die);
  464. if (name) {
  465. dwarf_entrypc(sp_die, &eaddr);
  466. tev->point.symbol = xstrdup(name);
  467. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  468. } else
  469. /* This function has no name. */
  470. tev->point.offset = (unsigned long)pf->addr;
  471. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  472. tev->point.offset);
  473. /* Get the frame base attribute/ops */
  474. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  475. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  476. if (ret <= 0 || nops == 0)
  477. pf->fb_ops = NULL;
  478. /* Find each argument */
  479. /* TODO: use dwarf_cfi_addrframe */
  480. tev->nargs = pf->pev->nargs;
  481. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  482. for (i = 0; i < pf->pev->nargs; i++) {
  483. pf->pvar = &pf->pev->args[i];
  484. pf->tvar = &tev->args[i];
  485. find_variable(sp_die, pf);
  486. }
  487. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  488. pf->fb_ops = NULL;
  489. }
  490. /* Find probe point from its line number */
  491. static void find_probe_point_by_line(struct probe_finder *pf)
  492. {
  493. Dwarf_Lines *lines;
  494. Dwarf_Line *line;
  495. size_t nlines, i;
  496. Dwarf_Addr addr;
  497. int lineno;
  498. int ret;
  499. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  500. DIE_IF(ret != 0);
  501. for (i = 0; i < nlines; i++) {
  502. line = dwarf_onesrcline(lines, i);
  503. dwarf_lineno(line, &lineno);
  504. if (lineno != pf->lno)
  505. continue;
  506. /* TODO: Get fileno from line, but how? */
  507. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  508. continue;
  509. ret = dwarf_lineaddr(line, &addr);
  510. DIE_IF(ret != 0);
  511. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  512. (int)i, lineno, (uintmax_t)addr);
  513. pf->addr = addr;
  514. convert_probe_point(NULL, pf);
  515. /* Continuing, because target line might be inlined. */
  516. }
  517. }
  518. /* Find lines which match lazy pattern */
  519. static int find_lazy_match_lines(struct list_head *head,
  520. const char *fname, const char *pat)
  521. {
  522. char *fbuf, *p1, *p2;
  523. int fd, line, nlines = 0;
  524. struct stat st;
  525. fd = open(fname, O_RDONLY);
  526. if (fd < 0)
  527. die("failed to open %s", fname);
  528. DIE_IF(fstat(fd, &st) < 0);
  529. fbuf = xmalloc(st.st_size + 2);
  530. DIE_IF(read(fd, fbuf, st.st_size) < 0);
  531. close(fd);
  532. fbuf[st.st_size] = '\n'; /* Dummy line */
  533. fbuf[st.st_size + 1] = '\0';
  534. p1 = fbuf;
  535. line = 1;
  536. while ((p2 = strchr(p1, '\n')) != NULL) {
  537. *p2 = '\0';
  538. if (strlazymatch(p1, pat)) {
  539. line_list__add_line(head, line);
  540. nlines++;
  541. }
  542. line++;
  543. p1 = p2 + 1;
  544. }
  545. free(fbuf);
  546. return nlines;
  547. }
  548. /* Find probe points from lazy pattern */
  549. static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  550. {
  551. Dwarf_Lines *lines;
  552. Dwarf_Line *line;
  553. size_t nlines, i;
  554. Dwarf_Addr addr;
  555. Dwarf_Die die_mem;
  556. int lineno;
  557. int ret;
  558. if (list_empty(&pf->lcache)) {
  559. /* Matching lazy line pattern */
  560. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  561. pf->pev->point.lazy_line);
  562. if (ret <= 0)
  563. die("No matched lines found in %s.", pf->fname);
  564. }
  565. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  566. DIE_IF(ret != 0);
  567. for (i = 0; i < nlines; i++) {
  568. line = dwarf_onesrcline(lines, i);
  569. dwarf_lineno(line, &lineno);
  570. if (!line_list__has_line(&pf->lcache, lineno))
  571. continue;
  572. /* TODO: Get fileno from line, but how? */
  573. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  574. continue;
  575. ret = dwarf_lineaddr(line, &addr);
  576. DIE_IF(ret != 0);
  577. if (sp_die) {
  578. /* Address filtering 1: does sp_die include addr? */
  579. if (!dwarf_haspc(sp_die, addr))
  580. continue;
  581. /* Address filtering 2: No child include addr? */
  582. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  583. continue;
  584. }
  585. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  586. (int)i, lineno, (unsigned long long)addr);
  587. pf->addr = addr;
  588. convert_probe_point(sp_die, pf);
  589. /* Continuing, because target line might be inlined. */
  590. }
  591. /* TODO: deallocate lines, but how? */
  592. }
  593. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  594. {
  595. struct probe_finder *pf = (struct probe_finder *)data;
  596. struct perf_probe_point *pp = &pf->pev->point;
  597. if (pp->lazy_line)
  598. find_probe_point_lazy(in_die, pf);
  599. else {
  600. /* Get probe address */
  601. pf->addr = die_get_entrypc(in_die);
  602. pf->addr += pp->offset;
  603. pr_debug("found inline addr: 0x%jx\n",
  604. (uintmax_t)pf->addr);
  605. convert_probe_point(in_die, pf);
  606. }
  607. return DWARF_CB_OK;
  608. }
  609. /* Search function from function name */
  610. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  611. {
  612. struct probe_finder *pf = (struct probe_finder *)data;
  613. struct perf_probe_point *pp = &pf->pev->point;
  614. /* Check tag and diename */
  615. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  616. die_compare_name(sp_die, pp->function) != 0)
  617. return 0;
  618. pf->fname = dwarf_decl_file(sp_die);
  619. if (pp->line) { /* Function relative line */
  620. dwarf_decl_line(sp_die, &pf->lno);
  621. pf->lno += pp->line;
  622. find_probe_point_by_line(pf);
  623. } else if (!dwarf_func_inline(sp_die)) {
  624. /* Real function */
  625. if (pp->lazy_line)
  626. find_probe_point_lazy(sp_die, pf);
  627. else {
  628. pf->addr = die_get_entrypc(sp_die);
  629. pf->addr += pp->offset;
  630. /* TODO: Check the address in this function */
  631. convert_probe_point(sp_die, pf);
  632. }
  633. } else
  634. /* Inlined function: search instances */
  635. dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
  636. return 1; /* Exit; no same symbol in this CU. */
  637. }
  638. static void find_probe_point_by_func(struct probe_finder *pf)
  639. {
  640. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
  641. }
  642. /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
  643. int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
  644. struct kprobe_trace_event **tevs)
  645. {
  646. struct probe_finder pf = {.pev = pev};
  647. struct perf_probe_point *pp = &pev->point;
  648. Dwarf_Off off, noff;
  649. size_t cuhl;
  650. Dwarf_Die *diep;
  651. Dwarf *dbg;
  652. pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
  653. *tevs = pf.tevs;
  654. pf.ntevs = 0;
  655. dbg = dwarf_begin(fd, DWARF_C_READ);
  656. if (!dbg)
  657. return -ENOENT;
  658. off = 0;
  659. line_list__init(&pf.lcache);
  660. /* Loop on CUs (Compilation Unit) */
  661. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  662. /* Get the DIE(Debugging Information Entry) of this CU */
  663. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  664. if (!diep)
  665. continue;
  666. /* Check if target file is included. */
  667. if (pp->file)
  668. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  669. else
  670. pf.fname = NULL;
  671. if (!pp->file || pf.fname) {
  672. if (pp->function)
  673. find_probe_point_by_func(&pf);
  674. else if (pp->lazy_line)
  675. find_probe_point_lazy(NULL, &pf);
  676. else {
  677. pf.lno = pp->line;
  678. find_probe_point_by_line(&pf);
  679. }
  680. }
  681. off = noff;
  682. }
  683. line_list__free(&pf.lcache);
  684. dwarf_end(dbg);
  685. return pf.ntevs;
  686. }
  687. /* Reverse search */
  688. int find_perf_probe_point(int fd, unsigned long addr,
  689. struct perf_probe_point *ppt)
  690. {
  691. Dwarf_Die cudie, spdie, indie;
  692. Dwarf *dbg;
  693. Dwarf_Line *line;
  694. Dwarf_Addr laddr, eaddr;
  695. const char *tmp;
  696. int lineno, ret = 0;
  697. dbg = dwarf_begin(fd, DWARF_C_READ);
  698. if (!dbg)
  699. return -ENOENT;
  700. /* Find cu die */
  701. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie))
  702. return -EINVAL;
  703. /* Find a corresponding line */
  704. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  705. if (line) {
  706. dwarf_lineaddr(line, &laddr);
  707. if ((Dwarf_Addr)addr == laddr) {
  708. dwarf_lineno(line, &lineno);
  709. ppt->line = lineno;
  710. tmp = dwarf_linesrc(line, NULL, NULL);
  711. DIE_IF(!tmp);
  712. ppt->file = xstrdup(tmp);
  713. ret = 1;
  714. }
  715. }
  716. /* Find a corresponding function */
  717. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  718. tmp = dwarf_diename(&spdie);
  719. if (!tmp)
  720. goto end;
  721. dwarf_entrypc(&spdie, &eaddr);
  722. if (!lineno) {
  723. /* We don't have a line number, let's use offset */
  724. ppt->function = xstrdup(tmp);
  725. ppt->offset = addr - (unsigned long)eaddr;
  726. ret = 1;
  727. goto end;
  728. }
  729. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) {
  730. /* addr in an inline function */
  731. tmp = dwarf_diename(&indie);
  732. if (!tmp)
  733. goto end;
  734. dwarf_decl_line(&indie, &lineno);
  735. } else {
  736. if (eaddr == addr) /* No offset: function entry */
  737. lineno = ppt->line;
  738. else
  739. dwarf_decl_line(&spdie, &lineno);
  740. }
  741. ppt->function = xstrdup(tmp);
  742. ppt->line -= lineno; /* Make a relative line number */
  743. }
  744. end:
  745. dwarf_end(dbg);
  746. return ret;
  747. }
  748. /* Find line range from its line number */
  749. static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  750. {
  751. Dwarf_Lines *lines;
  752. Dwarf_Line *line;
  753. size_t nlines, i;
  754. Dwarf_Addr addr;
  755. int lineno;
  756. int ret;
  757. const char *src;
  758. Dwarf_Die die_mem;
  759. line_list__init(&lf->lr->line_list);
  760. ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
  761. DIE_IF(ret != 0);
  762. for (i = 0; i < nlines; i++) {
  763. line = dwarf_onesrcline(lines, i);
  764. ret = dwarf_lineno(line, &lineno);
  765. DIE_IF(ret != 0);
  766. if (lf->lno_s > lineno || lf->lno_e < lineno)
  767. continue;
  768. if (sp_die) {
  769. /* Address filtering 1: does sp_die include addr? */
  770. ret = dwarf_lineaddr(line, &addr);
  771. DIE_IF(ret != 0);
  772. if (!dwarf_haspc(sp_die, addr))
  773. continue;
  774. /* Address filtering 2: No child include addr? */
  775. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  776. continue;
  777. }
  778. /* TODO: Get fileno from line, but how? */
  779. src = dwarf_linesrc(line, NULL, NULL);
  780. if (strtailcmp(src, lf->fname) != 0)
  781. continue;
  782. /* Copy real path */
  783. if (!lf->lr->path)
  784. lf->lr->path = xstrdup(src);
  785. line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
  786. }
  787. /* Update status */
  788. if (!list_empty(&lf->lr->line_list))
  789. lf->found = 1;
  790. else {
  791. free(lf->lr->path);
  792. lf->lr->path = NULL;
  793. }
  794. }
  795. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  796. {
  797. find_line_range_by_line(in_die, (struct line_finder *)data);
  798. return DWARF_CB_ABORT; /* No need to find other instances */
  799. }
  800. /* Search function from function name */
  801. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  802. {
  803. struct line_finder *lf = (struct line_finder *)data;
  804. struct line_range *lr = lf->lr;
  805. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  806. die_compare_name(sp_die, lr->function) == 0) {
  807. lf->fname = dwarf_decl_file(sp_die);
  808. dwarf_decl_line(sp_die, &lr->offset);
  809. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  810. lf->lno_s = lr->offset + lr->start;
  811. if (!lr->end)
  812. lf->lno_e = INT_MAX;
  813. else
  814. lf->lno_e = lr->offset + lr->end;
  815. lr->start = lf->lno_s;
  816. lr->end = lf->lno_e;
  817. if (dwarf_func_inline(sp_die))
  818. dwarf_func_inline_instances(sp_die,
  819. line_range_inline_cb, lf);
  820. else
  821. find_line_range_by_line(sp_die, lf);
  822. return 1;
  823. }
  824. return 0;
  825. }
  826. static void find_line_range_by_func(struct line_finder *lf)
  827. {
  828. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
  829. }
  830. int find_line_range(int fd, struct line_range *lr)
  831. {
  832. struct line_finder lf = {.lr = lr, .found = 0};
  833. int ret;
  834. Dwarf_Off off = 0, noff;
  835. size_t cuhl;
  836. Dwarf_Die *diep;
  837. Dwarf *dbg;
  838. dbg = dwarf_begin(fd, DWARF_C_READ);
  839. if (!dbg)
  840. return -ENOENT;
  841. /* Loop on CUs (Compilation Unit) */
  842. while (!lf.found) {
  843. ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
  844. if (ret != 0)
  845. break;
  846. /* Get the DIE(Debugging Information Entry) of this CU */
  847. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  848. if (!diep)
  849. continue;
  850. /* Check if target file is included. */
  851. if (lr->file)
  852. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  853. else
  854. lf.fname = 0;
  855. if (!lr->file || lf.fname) {
  856. if (lr->function)
  857. find_line_range_by_func(&lf);
  858. else {
  859. lf.lno_s = lr->start;
  860. if (!lr->end)
  861. lf.lno_e = INT_MAX;
  862. else
  863. lf.lno_e = lr->end;
  864. find_line_range_by_line(NULL, &lf);
  865. }
  866. }
  867. off = noff;
  868. }
  869. pr_debug("path: %lx\n", (unsigned long)lr->path);
  870. dwarf_end(dbg);
  871. return lf.found;
  872. }