probe-finder.c 25 KB

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