probe-finder.c 30 KB

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