probe-finder.c 31 KB

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