probe-finder.c 27 KB

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