probe-finder.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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 <dwarf-regs.h>
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "symbol.h"
  38. #include "probe-finder.h"
  39. /* Kprobe tracer basic type is up to u64 */
  40. #define MAX_BASIC_TYPE_BITS 64
  41. /*
  42. * Compare the tail of two strings.
  43. * Return 0 if whole of either string is same as another's tail part.
  44. */
  45. static int strtailcmp(const char *s1, const char *s2)
  46. {
  47. int i1 = strlen(s1);
  48. int i2 = strlen(s2);
  49. while (--i1 >= 0 && --i2 >= 0) {
  50. if (s1[i1] != s2[i2])
  51. return s1[i1] - s2[i2];
  52. }
  53. return 0;
  54. }
  55. /* Line number list operations */
  56. /* Add a line to line number list */
  57. static int line_list__add_line(struct list_head *head, int line)
  58. {
  59. struct line_node *ln;
  60. struct list_head *p;
  61. /* Reverse search, because new line will be the last one */
  62. list_for_each_entry_reverse(ln, head, list) {
  63. if (ln->line < line) {
  64. p = &ln->list;
  65. goto found;
  66. } else if (ln->line == line) /* Already exist */
  67. return 1;
  68. }
  69. /* List is empty, or the smallest entry */
  70. p = head;
  71. found:
  72. pr_debug("line list: add a line %u\n", line);
  73. ln = zalloc(sizeof(struct line_node));
  74. if (ln == NULL)
  75. return -ENOMEM;
  76. ln->line = line;
  77. INIT_LIST_HEAD(&ln->list);
  78. list_add(&ln->list, p);
  79. return 0;
  80. }
  81. /* Check if the line in line number list */
  82. static int line_list__has_line(struct list_head *head, int line)
  83. {
  84. struct line_node *ln;
  85. /* Reverse search, because new line will be the last one */
  86. list_for_each_entry(ln, head, list)
  87. if (ln->line == line)
  88. return 1;
  89. return 0;
  90. }
  91. /* Init line number list */
  92. static void line_list__init(struct list_head *head)
  93. {
  94. INIT_LIST_HEAD(head);
  95. }
  96. /* Free line number list */
  97. static void line_list__free(struct list_head *head)
  98. {
  99. struct line_node *ln;
  100. while (!list_empty(head)) {
  101. ln = list_first_entry(head, struct line_node, list);
  102. list_del(&ln->list);
  103. free(ln);
  104. }
  105. }
  106. /* Dwarf FL wrappers */
  107. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  108. void **userdata,
  109. const char *module_name,
  110. Dwarf_Addr base,
  111. char **file_name, Elf **elfp)
  112. {
  113. int fd;
  114. const char *path = kernel_get_module_path(module_name);
  115. if (path) {
  116. fd = open(path, O_RDONLY);
  117. if (fd >= 0) {
  118. *file_name = strdup(path);
  119. return fd;
  120. }
  121. }
  122. /* If failed, try to call standard method */
  123. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  124. file_name, elfp);
  125. }
  126. static char *debuginfo_path; /* Currently dummy */
  127. static const Dwfl_Callbacks offline_callbacks = {
  128. .find_debuginfo = dwfl_standard_find_debuginfo,
  129. .debuginfo_path = &debuginfo_path,
  130. .section_address = dwfl_offline_section_address,
  131. /* We use this table for core files too. */
  132. .find_elf = dwfl_build_id_find_elf,
  133. };
  134. static const Dwfl_Callbacks kernel_callbacks = {
  135. .find_debuginfo = dwfl_standard_find_debuginfo,
  136. .debuginfo_path = &debuginfo_path,
  137. .find_elf = __linux_kernel_find_elf,
  138. .section_address = dwfl_linux_kernel_module_section_address,
  139. };
  140. /* Get a Dwarf from offline image */
  141. static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
  142. {
  143. Dwfl_Module *mod;
  144. Dwarf *dbg = NULL;
  145. if (!dwflp)
  146. return NULL;
  147. *dwflp = dwfl_begin(&offline_callbacks);
  148. if (!*dwflp)
  149. return NULL;
  150. mod = dwfl_report_offline(*dwflp, "", "", fd);
  151. if (!mod)
  152. goto error;
  153. dbg = dwfl_module_getdwarf(mod, bias);
  154. if (!dbg) {
  155. error:
  156. dwfl_end(*dwflp);
  157. *dwflp = NULL;
  158. }
  159. return dbg;
  160. }
  161. /* Get a Dwarf from live kernel image */
  162. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
  163. Dwarf_Addr *bias)
  164. {
  165. Dwarf *dbg;
  166. if (!dwflp)
  167. return NULL;
  168. *dwflp = dwfl_begin(&kernel_callbacks);
  169. if (!*dwflp)
  170. return NULL;
  171. /* Load the kernel dwarves: Don't care the result here */
  172. dwfl_linux_kernel_report_kernel(*dwflp);
  173. dwfl_linux_kernel_report_modules(*dwflp);
  174. dbg = dwfl_addrdwarf(*dwflp, addr, bias);
  175. /* Here, check whether we could get a real dwarf */
  176. if (!dbg) {
  177. dwfl_end(*dwflp);
  178. *dwflp = NULL;
  179. }
  180. return dbg;
  181. }
  182. /* Dwarf wrappers */
  183. /* Find the realpath of the target file. */
  184. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  185. {
  186. Dwarf_Files *files;
  187. size_t nfiles, i;
  188. const char *src = NULL;
  189. int ret;
  190. if (!fname)
  191. return NULL;
  192. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  193. if (ret != 0)
  194. return NULL;
  195. for (i = 0; i < nfiles; i++) {
  196. src = dwarf_filesrc(files, i, NULL, NULL);
  197. if (strtailcmp(src, fname) == 0)
  198. break;
  199. }
  200. if (i == nfiles)
  201. return NULL;
  202. return src;
  203. }
  204. /* Get DW_AT_comp_dir (should be NULL with older gcc) */
  205. static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  206. {
  207. Dwarf_Attribute attr;
  208. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  209. return NULL;
  210. return dwarf_formstring(&attr);
  211. }
  212. /* Compare diename and tname */
  213. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  214. {
  215. const char *name;
  216. name = dwarf_diename(dw_die);
  217. return name ? (strcmp(tname, name) == 0) : false;
  218. }
  219. /* Get type die */
  220. static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  221. {
  222. Dwarf_Attribute attr;
  223. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  224. dwarf_formref_die(&attr, die_mem))
  225. return die_mem;
  226. else
  227. return NULL;
  228. }
  229. /* Get a type die, but skip qualifiers */
  230. static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  231. {
  232. int tag;
  233. do {
  234. vr_die = die_get_type(vr_die, die_mem);
  235. if (!vr_die)
  236. break;
  237. tag = dwarf_tag(vr_die);
  238. } while (tag == DW_TAG_const_type ||
  239. tag == DW_TAG_restrict_type ||
  240. tag == DW_TAG_volatile_type ||
  241. tag == DW_TAG_shared_type);
  242. return vr_die;
  243. }
  244. /* Get a type die, but skip qualifiers and typedef */
  245. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  246. {
  247. do {
  248. vr_die = __die_get_real_type(vr_die, die_mem);
  249. } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
  250. return vr_die;
  251. }
  252. static bool die_is_signed_type(Dwarf_Die *tp_die)
  253. {
  254. Dwarf_Attribute attr;
  255. Dwarf_Word ret;
  256. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  257. dwarf_formudata(&attr, &ret) != 0)
  258. return false;
  259. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  260. ret == DW_ATE_signed_fixed);
  261. }
  262. static int die_get_byte_size(Dwarf_Die *tp_die)
  263. {
  264. Dwarf_Attribute attr;
  265. Dwarf_Word ret;
  266. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  267. dwarf_formudata(&attr, &ret) != 0)
  268. return 0;
  269. return (int)ret;
  270. }
  271. /* Get data_member_location offset */
  272. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  273. {
  274. Dwarf_Attribute attr;
  275. Dwarf_Op *expr;
  276. size_t nexpr;
  277. int ret;
  278. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  279. return -ENOENT;
  280. if (dwarf_formudata(&attr, offs) != 0) {
  281. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  282. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  283. if (ret < 0 || nexpr == 0)
  284. return -ENOENT;
  285. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  286. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  287. expr[0].atom, nexpr);
  288. return -ENOTSUP;
  289. }
  290. *offs = (Dwarf_Word)expr[0].number;
  291. }
  292. return 0;
  293. }
  294. /* Return values for die_find callbacks */
  295. enum {
  296. DIE_FIND_CB_FOUND = 0, /* End of Search */
  297. DIE_FIND_CB_CHILD = 1, /* Search only children */
  298. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  299. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  300. };
  301. /* Search a child die */
  302. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  303. int (*callback)(Dwarf_Die *, void *),
  304. void *data, Dwarf_Die *die_mem)
  305. {
  306. Dwarf_Die child_die;
  307. int ret;
  308. ret = dwarf_child(rt_die, die_mem);
  309. if (ret != 0)
  310. return NULL;
  311. do {
  312. ret = callback(die_mem, data);
  313. if (ret == DIE_FIND_CB_FOUND)
  314. return die_mem;
  315. if ((ret & DIE_FIND_CB_CHILD) &&
  316. die_find_child(die_mem, callback, data, &child_die)) {
  317. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  318. return die_mem;
  319. }
  320. } while ((ret & DIE_FIND_CB_SIBLING) &&
  321. dwarf_siblingof(die_mem, die_mem) == 0);
  322. return NULL;
  323. }
  324. struct __addr_die_search_param {
  325. Dwarf_Addr addr;
  326. Dwarf_Die *die_mem;
  327. };
  328. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  329. {
  330. struct __addr_die_search_param *ad = data;
  331. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  332. dwarf_haspc(fn_die, ad->addr)) {
  333. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  334. return DWARF_CB_ABORT;
  335. }
  336. return DWARF_CB_OK;
  337. }
  338. /* Search a real subprogram including this line, */
  339. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  340. Dwarf_Die *die_mem)
  341. {
  342. struct __addr_die_search_param ad;
  343. ad.addr = addr;
  344. ad.die_mem = die_mem;
  345. /* dwarf_getscopes can't find subprogram. */
  346. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  347. return NULL;
  348. else
  349. return die_mem;
  350. }
  351. /* die_find callback for inline function search */
  352. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  353. {
  354. Dwarf_Addr *addr = data;
  355. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  356. dwarf_haspc(die_mem, *addr))
  357. return DIE_FIND_CB_FOUND;
  358. return DIE_FIND_CB_CONTINUE;
  359. }
  360. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  361. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  362. Dwarf_Die *die_mem)
  363. {
  364. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  365. }
  366. struct __find_variable_param {
  367. const char *name;
  368. Dwarf_Addr addr;
  369. };
  370. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  371. {
  372. struct __find_variable_param *fvp = data;
  373. int tag;
  374. tag = dwarf_tag(die_mem);
  375. if ((tag == DW_TAG_formal_parameter ||
  376. tag == DW_TAG_variable) &&
  377. die_compare_name(die_mem, fvp->name))
  378. return DIE_FIND_CB_FOUND;
  379. if (dwarf_haspc(die_mem, fvp->addr))
  380. return DIE_FIND_CB_CONTINUE;
  381. else
  382. return DIE_FIND_CB_SIBLING;
  383. }
  384. /* Find a variable called 'name' at given address */
  385. static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  386. Dwarf_Addr addr, Dwarf_Die *die_mem)
  387. {
  388. struct __find_variable_param fvp = { .name = name, .addr = addr};
  389. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  390. die_mem);
  391. }
  392. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  393. {
  394. const char *name = data;
  395. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  396. die_compare_name(die_mem, name))
  397. return DIE_FIND_CB_FOUND;
  398. return DIE_FIND_CB_SIBLING;
  399. }
  400. /* Find a member called 'name' */
  401. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  402. Dwarf_Die *die_mem)
  403. {
  404. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  405. die_mem);
  406. }
  407. /* Get the name of given variable DIE */
  408. static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
  409. {
  410. Dwarf_Die type;
  411. int tag, ret, ret2;
  412. const char *tmp = "";
  413. if (__die_get_real_type(vr_die, &type) == NULL)
  414. return -ENOENT;
  415. tag = dwarf_tag(&type);
  416. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  417. tmp = "*";
  418. else if (tag == DW_TAG_subroutine_type) {
  419. /* Function pointer */
  420. ret = snprintf(buf, len, "(function_type)");
  421. return (ret >= len) ? -E2BIG : ret;
  422. } else {
  423. if (!dwarf_diename(&type))
  424. return -ENOENT;
  425. if (tag == DW_TAG_union_type)
  426. tmp = "union ";
  427. else if (tag == DW_TAG_structure_type)
  428. tmp = "struct ";
  429. /* Write a base name */
  430. ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
  431. return (ret >= len) ? -E2BIG : ret;
  432. }
  433. ret = die_get_typename(&type, buf, len);
  434. if (ret > 0) {
  435. ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
  436. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  437. }
  438. return ret;
  439. }
  440. /* Get the name and type of given variable DIE, stored as "type\tname" */
  441. static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
  442. {
  443. int ret, ret2;
  444. ret = die_get_typename(vr_die, buf, len);
  445. if (ret < 0) {
  446. pr_debug("Failed to get type, make it unknown.\n");
  447. ret = snprintf(buf, len, "(unknown_type)");
  448. }
  449. if (ret > 0) {
  450. ret2 = snprintf(buf + ret, len - ret, "\t%s",
  451. dwarf_diename(vr_die));
  452. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  453. }
  454. return ret;
  455. }
  456. /*
  457. * Probe finder related functions
  458. */
  459. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  460. {
  461. struct probe_trace_arg_ref *ref;
  462. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  463. if (ref != NULL)
  464. ref->offset = offs;
  465. return ref;
  466. }
  467. /*
  468. * Convert a location into trace_arg.
  469. * If tvar == NULL, this just checks variable can be converted.
  470. */
  471. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  472. Dwarf_Op *fb_ops,
  473. struct probe_trace_arg *tvar)
  474. {
  475. Dwarf_Attribute attr;
  476. Dwarf_Op *op;
  477. size_t nops;
  478. unsigned int regn;
  479. Dwarf_Word offs = 0;
  480. bool ref = false;
  481. const char *regs;
  482. int ret;
  483. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  484. goto static_var;
  485. /* TODO: handle more than 1 exprs */
  486. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  487. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  488. nops == 0) {
  489. /* TODO: Support const_value */
  490. return -ENOENT;
  491. }
  492. if (op->atom == DW_OP_addr) {
  493. static_var:
  494. if (!tvar)
  495. return 0;
  496. /* Static variables on memory (not stack), make @varname */
  497. ret = strlen(dwarf_diename(vr_die));
  498. tvar->value = zalloc(ret + 2);
  499. if (tvar->value == NULL)
  500. return -ENOMEM;
  501. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  502. tvar->ref = alloc_trace_arg_ref((long)offs);
  503. if (tvar->ref == NULL)
  504. return -ENOMEM;
  505. return 0;
  506. }
  507. /* If this is based on frame buffer, set the offset */
  508. if (op->atom == DW_OP_fbreg) {
  509. if (fb_ops == NULL)
  510. return -ENOTSUP;
  511. ref = true;
  512. offs = op->number;
  513. op = &fb_ops[0];
  514. }
  515. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  516. regn = op->atom - DW_OP_breg0;
  517. offs += op->number;
  518. ref = true;
  519. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  520. regn = op->atom - DW_OP_reg0;
  521. } else if (op->atom == DW_OP_bregx) {
  522. regn = op->number;
  523. offs += op->number2;
  524. ref = true;
  525. } else if (op->atom == DW_OP_regx) {
  526. regn = op->number;
  527. } else {
  528. pr_debug("DW_OP %x is not supported.\n", op->atom);
  529. return -ENOTSUP;
  530. }
  531. if (!tvar)
  532. return 0;
  533. regs = get_arch_regstr(regn);
  534. if (!regs) {
  535. /* This should be a bug in DWARF or this tool */
  536. pr_warning("Mapping for DWARF register number %u "
  537. "missing on this architecture.", regn);
  538. return -ERANGE;
  539. }
  540. tvar->value = strdup(regs);
  541. if (tvar->value == NULL)
  542. return -ENOMEM;
  543. if (ref) {
  544. tvar->ref = alloc_trace_arg_ref((long)offs);
  545. if (tvar->ref == NULL)
  546. return -ENOMEM;
  547. }
  548. return 0;
  549. }
  550. static int convert_variable_type(Dwarf_Die *vr_die,
  551. struct probe_trace_arg *tvar,
  552. const char *cast)
  553. {
  554. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  555. Dwarf_Die type;
  556. char buf[16];
  557. int ret;
  558. /* TODO: check all types */
  559. if (cast && strcmp(cast, "string") != 0) {
  560. /* Non string type is OK */
  561. tvar->type = strdup(cast);
  562. return (tvar->type == NULL) ? -ENOMEM : 0;
  563. }
  564. if (die_get_real_type(vr_die, &type) == NULL) {
  565. pr_warning("Failed to get a type information of %s.\n",
  566. dwarf_diename(vr_die));
  567. return -ENOENT;
  568. }
  569. pr_debug("%s type is %s.\n",
  570. dwarf_diename(vr_die), dwarf_diename(&type));
  571. if (cast && strcmp(cast, "string") == 0) { /* String type */
  572. ret = dwarf_tag(&type);
  573. if (ret != DW_TAG_pointer_type &&
  574. ret != DW_TAG_array_type) {
  575. pr_warning("Failed to cast into string: "
  576. "%s(%s) is not a pointer nor array.",
  577. dwarf_diename(vr_die), dwarf_diename(&type));
  578. return -EINVAL;
  579. }
  580. if (ret == DW_TAG_pointer_type) {
  581. if (die_get_real_type(&type, &type) == NULL) {
  582. pr_warning("Failed to get a type information.");
  583. return -ENOENT;
  584. }
  585. while (*ref_ptr)
  586. ref_ptr = &(*ref_ptr)->next;
  587. /* Add new reference with offset +0 */
  588. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  589. if (*ref_ptr == NULL) {
  590. pr_warning("Out of memory error\n");
  591. return -ENOMEM;
  592. }
  593. }
  594. if (!die_compare_name(&type, "char") &&
  595. !die_compare_name(&type, "unsigned char")) {
  596. pr_warning("Failed to cast into string: "
  597. "%s is not (unsigned) char *.",
  598. dwarf_diename(vr_die));
  599. return -EINVAL;
  600. }
  601. tvar->type = strdup(cast);
  602. return (tvar->type == NULL) ? -ENOMEM : 0;
  603. }
  604. ret = die_get_byte_size(&type) * 8;
  605. if (ret) {
  606. /* Check the bitwidth */
  607. if (ret > MAX_BASIC_TYPE_BITS) {
  608. pr_info("%s exceeds max-bitwidth."
  609. " Cut down to %d bits.\n",
  610. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  611. ret = MAX_BASIC_TYPE_BITS;
  612. }
  613. ret = snprintf(buf, 16, "%c%d",
  614. die_is_signed_type(&type) ? 's' : 'u', ret);
  615. if (ret < 0 || ret >= 16) {
  616. if (ret >= 16)
  617. ret = -E2BIG;
  618. pr_warning("Failed to convert variable type: %s\n",
  619. strerror(-ret));
  620. return ret;
  621. }
  622. tvar->type = strdup(buf);
  623. if (tvar->type == NULL)
  624. return -ENOMEM;
  625. }
  626. return 0;
  627. }
  628. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  629. struct perf_probe_arg_field *field,
  630. struct probe_trace_arg_ref **ref_ptr,
  631. Dwarf_Die *die_mem)
  632. {
  633. struct probe_trace_arg_ref *ref = *ref_ptr;
  634. Dwarf_Die type;
  635. Dwarf_Word offs;
  636. int ret, tag;
  637. pr_debug("converting %s in %s\n", field->name, varname);
  638. if (die_get_real_type(vr_die, &type) == NULL) {
  639. pr_warning("Failed to get the type of %s.\n", varname);
  640. return -ENOENT;
  641. }
  642. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  643. tag = dwarf_tag(&type);
  644. if (field->name[0] == '[' &&
  645. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  646. if (field->next)
  647. /* Save original type for next field */
  648. memcpy(die_mem, &type, sizeof(*die_mem));
  649. /* Get the type of this array */
  650. if (die_get_real_type(&type, &type) == NULL) {
  651. pr_warning("Failed to get the type of %s.\n", varname);
  652. return -ENOENT;
  653. }
  654. pr_debug2("Array real type: (%x)\n",
  655. (unsigned)dwarf_dieoffset(&type));
  656. if (tag == DW_TAG_pointer_type) {
  657. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  658. if (ref == NULL)
  659. return -ENOMEM;
  660. if (*ref_ptr)
  661. (*ref_ptr)->next = ref;
  662. else
  663. *ref_ptr = ref;
  664. }
  665. ref->offset += die_get_byte_size(&type) * field->index;
  666. if (!field->next)
  667. /* Save vr_die for converting types */
  668. memcpy(die_mem, vr_die, sizeof(*die_mem));
  669. goto next;
  670. } else if (tag == DW_TAG_pointer_type) {
  671. /* Check the pointer and dereference */
  672. if (!field->ref) {
  673. pr_err("Semantic error: %s must be referred by '->'\n",
  674. field->name);
  675. return -EINVAL;
  676. }
  677. /* Get the type pointed by this pointer */
  678. if (die_get_real_type(&type, &type) == NULL) {
  679. pr_warning("Failed to get the type of %s.\n", varname);
  680. return -ENOENT;
  681. }
  682. /* Verify it is a data structure */
  683. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  684. pr_warning("%s is not a data structure.\n", varname);
  685. return -EINVAL;
  686. }
  687. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  688. if (ref == NULL)
  689. return -ENOMEM;
  690. if (*ref_ptr)
  691. (*ref_ptr)->next = ref;
  692. else
  693. *ref_ptr = ref;
  694. } else {
  695. /* Verify it is a data structure */
  696. if (tag != DW_TAG_structure_type) {
  697. pr_warning("%s is not a data structure.\n", varname);
  698. return -EINVAL;
  699. }
  700. if (field->name[0] == '[') {
  701. pr_err("Semantic error: %s is not a pointor nor array.",
  702. varname);
  703. return -EINVAL;
  704. }
  705. if (field->ref) {
  706. pr_err("Semantic error: %s must be referred by '.'\n",
  707. field->name);
  708. return -EINVAL;
  709. }
  710. if (!ref) {
  711. pr_warning("Structure on a register is not "
  712. "supported yet.\n");
  713. return -ENOTSUP;
  714. }
  715. }
  716. if (die_find_member(&type, field->name, die_mem) == NULL) {
  717. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  718. dwarf_diename(&type), field->name);
  719. return -EINVAL;
  720. }
  721. /* Get the offset of the field */
  722. ret = die_get_data_member_location(die_mem, &offs);
  723. if (ret < 0) {
  724. pr_warning("Failed to get the offset of %s.\n", field->name);
  725. return ret;
  726. }
  727. ref->offset += (long)offs;
  728. next:
  729. /* Converting next field */
  730. if (field->next)
  731. return convert_variable_fields(die_mem, field->name,
  732. field->next, &ref, die_mem);
  733. else
  734. return 0;
  735. }
  736. /* Show a variables in kprobe event format */
  737. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  738. {
  739. Dwarf_Die die_mem;
  740. int ret;
  741. pr_debug("Converting variable %s into trace event.\n",
  742. dwarf_diename(vr_die));
  743. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  744. pf->tvar);
  745. if (ret == -ENOENT)
  746. pr_err("Failed to find the location of %s at this address.\n"
  747. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  748. else if (ret == -ENOTSUP)
  749. pr_err("Sorry, we don't support this variable location yet.\n");
  750. else if (pf->pvar->field) {
  751. ret = convert_variable_fields(vr_die, pf->pvar->var,
  752. pf->pvar->field, &pf->tvar->ref,
  753. &die_mem);
  754. vr_die = &die_mem;
  755. }
  756. if (ret == 0)
  757. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  758. /* *expr will be cached in libdw. Don't free it. */
  759. return ret;
  760. }
  761. /* Find a variable in a subprogram die */
  762. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  763. {
  764. Dwarf_Die vr_die, *scopes;
  765. char buf[32], *ptr;
  766. int ret, nscopes;
  767. if (!is_c_varname(pf->pvar->var)) {
  768. /* Copy raw parameters */
  769. pf->tvar->value = strdup(pf->pvar->var);
  770. if (pf->tvar->value == NULL)
  771. return -ENOMEM;
  772. if (pf->pvar->type) {
  773. pf->tvar->type = strdup(pf->pvar->type);
  774. if (pf->tvar->type == NULL)
  775. return -ENOMEM;
  776. }
  777. if (pf->pvar->name) {
  778. pf->tvar->name = strdup(pf->pvar->name);
  779. if (pf->tvar->name == NULL)
  780. return -ENOMEM;
  781. } else
  782. pf->tvar->name = NULL;
  783. return 0;
  784. }
  785. if (pf->pvar->name)
  786. pf->tvar->name = strdup(pf->pvar->name);
  787. else {
  788. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  789. if (ret < 0)
  790. return ret;
  791. ptr = strchr(buf, ':'); /* Change type separator to _ */
  792. if (ptr)
  793. *ptr = '_';
  794. pf->tvar->name = strdup(buf);
  795. }
  796. if (pf->tvar->name == NULL)
  797. return -ENOMEM;
  798. pr_debug("Searching '%s' variable in context.\n",
  799. pf->pvar->var);
  800. /* Search child die for local variables and parameters. */
  801. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  802. ret = convert_variable(&vr_die, pf);
  803. else {
  804. /* Search upper class */
  805. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  806. while (nscopes-- > 1) {
  807. pr_debug("Searching variables in %s\n",
  808. dwarf_diename(&scopes[nscopes]));
  809. /* We should check this scope, so give dummy address */
  810. if (die_find_variable_at(&scopes[nscopes],
  811. pf->pvar->var, 0,
  812. &vr_die)) {
  813. ret = convert_variable(&vr_die, pf);
  814. goto found;
  815. }
  816. }
  817. if (scopes)
  818. free(scopes);
  819. ret = -ENOENT;
  820. }
  821. found:
  822. if (ret < 0)
  823. pr_warning("Failed to find '%s' in this function.\n",
  824. pf->pvar->var);
  825. return ret;
  826. }
  827. /* Convert subprogram DIE to trace point */
  828. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  829. bool retprobe, struct probe_trace_point *tp)
  830. {
  831. Dwarf_Addr eaddr;
  832. const char *name;
  833. /* Copy the name of probe point */
  834. name = dwarf_diename(sp_die);
  835. if (name) {
  836. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  837. pr_warning("Failed to get entry pc of %s\n",
  838. dwarf_diename(sp_die));
  839. return -ENOENT;
  840. }
  841. tp->symbol = strdup(name);
  842. if (tp->symbol == NULL)
  843. return -ENOMEM;
  844. tp->offset = (unsigned long)(paddr - eaddr);
  845. } else
  846. /* This function has no name. */
  847. tp->offset = (unsigned long)paddr;
  848. /* Return probe must be on the head of a subprogram */
  849. if (retprobe) {
  850. if (eaddr != paddr) {
  851. pr_warning("Return probe must be on the head of"
  852. " a real function\n");
  853. return -EINVAL;
  854. }
  855. tp->retprobe = true;
  856. }
  857. return 0;
  858. }
  859. /* Call probe_finder callback with real subprogram DIE */
  860. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  861. {
  862. Dwarf_Die die_mem;
  863. Dwarf_Attribute fb_attr;
  864. size_t nops;
  865. int ret;
  866. /* If no real subprogram, find a real one */
  867. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  868. sp_die = die_find_real_subprogram(&pf->cu_die,
  869. pf->addr, &die_mem);
  870. if (!sp_die) {
  871. pr_warning("Failed to find probe point in any "
  872. "functions.\n");
  873. return -ENOENT;
  874. }
  875. }
  876. /* Get the frame base attribute/ops */
  877. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  878. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  879. if (ret <= 0 || nops == 0) {
  880. pf->fb_ops = NULL;
  881. #if _ELFUTILS_PREREQ(0, 142)
  882. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  883. pf->cfi != NULL) {
  884. Dwarf_Frame *frame;
  885. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  886. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  887. pr_warning("Failed to get CFA on 0x%jx\n",
  888. (uintmax_t)pf->addr);
  889. return -ENOENT;
  890. }
  891. #endif
  892. }
  893. /* Call finder's callback handler */
  894. ret = pf->callback(sp_die, pf);
  895. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  896. pf->fb_ops = NULL;
  897. return ret;
  898. }
  899. /* Find probe point from its line number */
  900. static int find_probe_point_by_line(struct probe_finder *pf)
  901. {
  902. Dwarf_Lines *lines;
  903. Dwarf_Line *line;
  904. size_t nlines, i;
  905. Dwarf_Addr addr;
  906. int lineno;
  907. int ret = 0;
  908. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  909. pr_warning("No source lines found in this CU.\n");
  910. return -ENOENT;
  911. }
  912. for (i = 0; i < nlines && ret == 0; i++) {
  913. line = dwarf_onesrcline(lines, i);
  914. if (dwarf_lineno(line, &lineno) != 0 ||
  915. lineno != pf->lno)
  916. continue;
  917. /* TODO: Get fileno from line, but how? */
  918. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  919. continue;
  920. if (dwarf_lineaddr(line, &addr) != 0) {
  921. pr_warning("Failed to get the address of the line.\n");
  922. return -ENOENT;
  923. }
  924. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  925. (int)i, lineno, (uintmax_t)addr);
  926. pf->addr = addr;
  927. ret = call_probe_finder(NULL, pf);
  928. /* Continuing, because target line might be inlined. */
  929. }
  930. return ret;
  931. }
  932. /* Find lines which match lazy pattern */
  933. static int find_lazy_match_lines(struct list_head *head,
  934. const char *fname, const char *pat)
  935. {
  936. char *fbuf, *p1, *p2;
  937. int fd, line, nlines = -1;
  938. struct stat st;
  939. fd = open(fname, O_RDONLY);
  940. if (fd < 0) {
  941. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  942. return -errno;
  943. }
  944. if (fstat(fd, &st) < 0) {
  945. pr_warning("Failed to get the size of %s: %s\n",
  946. fname, strerror(errno));
  947. nlines = -errno;
  948. goto out_close;
  949. }
  950. nlines = -ENOMEM;
  951. fbuf = malloc(st.st_size + 2);
  952. if (fbuf == NULL)
  953. goto out_close;
  954. if (read(fd, fbuf, st.st_size) < 0) {
  955. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  956. nlines = -errno;
  957. goto out_free_fbuf;
  958. }
  959. fbuf[st.st_size] = '\n'; /* Dummy line */
  960. fbuf[st.st_size + 1] = '\0';
  961. p1 = fbuf;
  962. line = 1;
  963. nlines = 0;
  964. while ((p2 = strchr(p1, '\n')) != NULL) {
  965. *p2 = '\0';
  966. if (strlazymatch(p1, pat)) {
  967. line_list__add_line(head, line);
  968. nlines++;
  969. }
  970. line++;
  971. p1 = p2 + 1;
  972. }
  973. out_free_fbuf:
  974. free(fbuf);
  975. out_close:
  976. close(fd);
  977. return nlines;
  978. }
  979. /* Find probe points from lazy pattern */
  980. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  981. {
  982. Dwarf_Lines *lines;
  983. Dwarf_Line *line;
  984. size_t nlines, i;
  985. Dwarf_Addr addr;
  986. Dwarf_Die die_mem;
  987. int lineno;
  988. int ret = 0;
  989. if (list_empty(&pf->lcache)) {
  990. /* Matching lazy line pattern */
  991. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  992. pf->pev->point.lazy_line);
  993. if (ret == 0) {
  994. pr_debug("No matched lines found in %s.\n", pf->fname);
  995. return 0;
  996. } else if (ret < 0)
  997. return ret;
  998. }
  999. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  1000. pr_warning("No source lines found in this CU.\n");
  1001. return -ENOENT;
  1002. }
  1003. for (i = 0; i < nlines && ret >= 0; i++) {
  1004. line = dwarf_onesrcline(lines, i);
  1005. if (dwarf_lineno(line, &lineno) != 0 ||
  1006. !line_list__has_line(&pf->lcache, lineno))
  1007. continue;
  1008. /* TODO: Get fileno from line, but how? */
  1009. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  1010. continue;
  1011. if (dwarf_lineaddr(line, &addr) != 0) {
  1012. pr_debug("Failed to get the address of line %d.\n",
  1013. lineno);
  1014. continue;
  1015. }
  1016. if (sp_die) {
  1017. /* Address filtering 1: does sp_die include addr? */
  1018. if (!dwarf_haspc(sp_die, addr))
  1019. continue;
  1020. /* Address filtering 2: No child include addr? */
  1021. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1022. continue;
  1023. }
  1024. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  1025. (int)i, lineno, (unsigned long long)addr);
  1026. pf->addr = addr;
  1027. ret = call_probe_finder(sp_die, pf);
  1028. /* Continuing, because target line might be inlined. */
  1029. }
  1030. /* TODO: deallocate lines, but how? */
  1031. return ret;
  1032. }
  1033. /* Callback parameter with return value */
  1034. struct dwarf_callback_param {
  1035. void *data;
  1036. int retval;
  1037. };
  1038. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  1039. {
  1040. struct dwarf_callback_param *param = data;
  1041. struct probe_finder *pf = param->data;
  1042. struct perf_probe_point *pp = &pf->pev->point;
  1043. Dwarf_Addr addr;
  1044. if (pp->lazy_line)
  1045. param->retval = find_probe_point_lazy(in_die, pf);
  1046. else {
  1047. /* Get probe address */
  1048. if (dwarf_entrypc(in_die, &addr) != 0) {
  1049. pr_warning("Failed to get entry pc of %s.\n",
  1050. dwarf_diename(in_die));
  1051. param->retval = -ENOENT;
  1052. return DWARF_CB_ABORT;
  1053. }
  1054. pf->addr = addr;
  1055. pf->addr += pp->offset;
  1056. pr_debug("found inline addr: 0x%jx\n",
  1057. (uintmax_t)pf->addr);
  1058. param->retval = call_probe_finder(in_die, pf);
  1059. if (param->retval < 0)
  1060. return DWARF_CB_ABORT;
  1061. }
  1062. return DWARF_CB_OK;
  1063. }
  1064. /* Search function from function name */
  1065. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  1066. {
  1067. struct dwarf_callback_param *param = data;
  1068. struct probe_finder *pf = param->data;
  1069. struct perf_probe_point *pp = &pf->pev->point;
  1070. /* Check tag and diename */
  1071. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  1072. !die_compare_name(sp_die, pp->function))
  1073. return DWARF_CB_OK;
  1074. pf->fname = dwarf_decl_file(sp_die);
  1075. if (pp->line) { /* Function relative line */
  1076. dwarf_decl_line(sp_die, &pf->lno);
  1077. pf->lno += pp->line;
  1078. param->retval = find_probe_point_by_line(pf);
  1079. } else if (!dwarf_func_inline(sp_die)) {
  1080. /* Real function */
  1081. if (pp->lazy_line)
  1082. param->retval = find_probe_point_lazy(sp_die, pf);
  1083. else {
  1084. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  1085. pr_warning("Failed to get entry pc of %s.\n",
  1086. dwarf_diename(sp_die));
  1087. param->retval = -ENOENT;
  1088. return DWARF_CB_ABORT;
  1089. }
  1090. pf->addr += pp->offset;
  1091. /* TODO: Check the address in this function */
  1092. param->retval = call_probe_finder(sp_die, pf);
  1093. }
  1094. } else {
  1095. struct dwarf_callback_param _param = {.data = (void *)pf,
  1096. .retval = 0};
  1097. /* Inlined function: search instances */
  1098. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  1099. &_param);
  1100. param->retval = _param.retval;
  1101. }
  1102. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  1103. }
  1104. static int find_probe_point_by_func(struct probe_finder *pf)
  1105. {
  1106. struct dwarf_callback_param _param = {.data = (void *)pf,
  1107. .retval = 0};
  1108. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  1109. return _param.retval;
  1110. }
  1111. /* Find probe points from debuginfo */
  1112. static int find_probes(int fd, struct probe_finder *pf)
  1113. {
  1114. struct perf_probe_point *pp = &pf->pev->point;
  1115. Dwarf_Off off, noff;
  1116. size_t cuhl;
  1117. Dwarf_Die *diep;
  1118. Dwarf *dbg = NULL;
  1119. Dwfl *dwfl;
  1120. Dwarf_Addr bias; /* Currently ignored */
  1121. int ret = 0;
  1122. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1123. if (!dbg) {
  1124. pr_warning("No dwarf info found in the vmlinux - "
  1125. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1126. return -EBADF;
  1127. }
  1128. #if _ELFUTILS_PREREQ(0, 142)
  1129. /* Get the call frame information from this dwarf */
  1130. pf->cfi = dwarf_getcfi(dbg);
  1131. #endif
  1132. off = 0;
  1133. line_list__init(&pf->lcache);
  1134. /* Loop on CUs (Compilation Unit) */
  1135. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  1136. ret >= 0) {
  1137. /* Get the DIE(Debugging Information Entry) of this CU */
  1138. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  1139. if (!diep)
  1140. continue;
  1141. /* Check if target file is included. */
  1142. if (pp->file)
  1143. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  1144. else
  1145. pf->fname = NULL;
  1146. if (!pp->file || pf->fname) {
  1147. if (pp->function)
  1148. ret = find_probe_point_by_func(pf);
  1149. else if (pp->lazy_line)
  1150. ret = find_probe_point_lazy(NULL, pf);
  1151. else {
  1152. pf->lno = pp->line;
  1153. ret = find_probe_point_by_line(pf);
  1154. }
  1155. }
  1156. off = noff;
  1157. }
  1158. line_list__free(&pf->lcache);
  1159. if (dwfl)
  1160. dwfl_end(dwfl);
  1161. return ret;
  1162. }
  1163. /* Add a found probe point into trace event list */
  1164. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  1165. {
  1166. struct trace_event_finder *tf =
  1167. container_of(pf, struct trace_event_finder, pf);
  1168. struct probe_trace_event *tev;
  1169. int ret, i;
  1170. /* Check number of tevs */
  1171. if (tf->ntevs == tf->max_tevs) {
  1172. pr_warning("Too many( > %d) probe point found.\n",
  1173. tf->max_tevs);
  1174. return -ERANGE;
  1175. }
  1176. tev = &tf->tevs[tf->ntevs++];
  1177. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1178. &tev->point);
  1179. if (ret < 0)
  1180. return ret;
  1181. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1182. tev->point.offset);
  1183. /* Find each argument */
  1184. tev->nargs = pf->pev->nargs;
  1185. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1186. if (tev->args == NULL)
  1187. return -ENOMEM;
  1188. for (i = 0; i < pf->pev->nargs; i++) {
  1189. pf->pvar = &pf->pev->args[i];
  1190. pf->tvar = &tev->args[i];
  1191. ret = find_variable(sp_die, pf);
  1192. if (ret != 0)
  1193. return ret;
  1194. }
  1195. return 0;
  1196. }
  1197. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1198. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  1199. struct probe_trace_event **tevs, int max_tevs)
  1200. {
  1201. struct trace_event_finder tf = {
  1202. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1203. .max_tevs = max_tevs};
  1204. int ret;
  1205. /* Allocate result tevs array */
  1206. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1207. if (*tevs == NULL)
  1208. return -ENOMEM;
  1209. tf.tevs = *tevs;
  1210. tf.ntevs = 0;
  1211. ret = find_probes(fd, &tf.pf);
  1212. if (ret < 0) {
  1213. free(*tevs);
  1214. *tevs = NULL;
  1215. return ret;
  1216. }
  1217. return (ret < 0) ? ret : tf.ntevs;
  1218. }
  1219. #define MAX_VAR_LEN 64
  1220. /* Collect available variables in this scope */
  1221. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1222. {
  1223. struct available_var_finder *af = data;
  1224. struct variable_list *vl;
  1225. char buf[MAX_VAR_LEN];
  1226. int tag, ret;
  1227. vl = &af->vls[af->nvls - 1];
  1228. tag = dwarf_tag(die_mem);
  1229. if (tag == DW_TAG_formal_parameter ||
  1230. tag == DW_TAG_variable) {
  1231. ret = convert_variable_location(die_mem, af->pf.addr,
  1232. af->pf.fb_ops, NULL);
  1233. if (ret == 0) {
  1234. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1235. pr_debug2("Add new var: %s\n", buf);
  1236. if (ret > 0)
  1237. strlist__add(vl->vars, buf);
  1238. }
  1239. }
  1240. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1241. return DIE_FIND_CB_CONTINUE;
  1242. else
  1243. return DIE_FIND_CB_SIBLING;
  1244. }
  1245. /* Add a found vars into available variables list */
  1246. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1247. {
  1248. struct available_var_finder *af =
  1249. container_of(pf, struct available_var_finder, pf);
  1250. struct variable_list *vl;
  1251. Dwarf_Die die_mem, *scopes = NULL;
  1252. int ret, nscopes;
  1253. /* Check number of tevs */
  1254. if (af->nvls == af->max_vls) {
  1255. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1256. return -ERANGE;
  1257. }
  1258. vl = &af->vls[af->nvls++];
  1259. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1260. &vl->point);
  1261. if (ret < 0)
  1262. return ret;
  1263. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1264. vl->point.offset);
  1265. /* Find local variables */
  1266. vl->vars = strlist__new(true, NULL);
  1267. if (vl->vars == NULL)
  1268. return -ENOMEM;
  1269. af->child = true;
  1270. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1271. /* Find external variables */
  1272. if (!af->externs)
  1273. goto out;
  1274. /* Don't need to search child DIE for externs. */
  1275. af->child = false;
  1276. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1277. while (nscopes-- > 1)
  1278. die_find_child(&scopes[nscopes], collect_variables_cb,
  1279. (void *)af, &die_mem);
  1280. if (scopes)
  1281. free(scopes);
  1282. out:
  1283. if (strlist__empty(vl->vars)) {
  1284. strlist__delete(vl->vars);
  1285. vl->vars = NULL;
  1286. }
  1287. return ret;
  1288. }
  1289. /* Find available variables at given probe point */
  1290. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1291. struct variable_list **vls, int max_vls,
  1292. bool externs)
  1293. {
  1294. struct available_var_finder af = {
  1295. .pf = {.pev = pev, .callback = add_available_vars},
  1296. .max_vls = max_vls, .externs = externs};
  1297. int ret;
  1298. /* Allocate result vls array */
  1299. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1300. if (*vls == NULL)
  1301. return -ENOMEM;
  1302. af.vls = *vls;
  1303. af.nvls = 0;
  1304. ret = find_probes(fd, &af.pf);
  1305. if (ret < 0) {
  1306. /* Free vlist for error */
  1307. while (af.nvls--) {
  1308. if (af.vls[af.nvls].point.symbol)
  1309. free(af.vls[af.nvls].point.symbol);
  1310. if (af.vls[af.nvls].vars)
  1311. strlist__delete(af.vls[af.nvls].vars);
  1312. }
  1313. free(af.vls);
  1314. *vls = NULL;
  1315. return ret;
  1316. }
  1317. return (ret < 0) ? ret : af.nvls;
  1318. }
  1319. /* Reverse search */
  1320. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1321. {
  1322. Dwarf_Die cudie, spdie, indie;
  1323. Dwarf *dbg = NULL;
  1324. Dwfl *dwfl = NULL;
  1325. Dwarf_Line *line;
  1326. Dwarf_Addr laddr, eaddr, bias = 0;
  1327. const char *tmp;
  1328. int lineno, ret = 0;
  1329. bool found = false;
  1330. /* Open the live linux kernel */
  1331. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1332. if (!dbg) {
  1333. pr_warning("No dwarf info found in the vmlinux - "
  1334. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1335. ret = -EINVAL;
  1336. goto end;
  1337. }
  1338. /* Adjust address with bias */
  1339. addr += bias;
  1340. /* Find cu die */
  1341. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1342. pr_warning("No CU DIE is found at %lx\n", addr);
  1343. ret = -EINVAL;
  1344. goto end;
  1345. }
  1346. /* Find a corresponding line */
  1347. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1348. if (line) {
  1349. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1350. (Dwarf_Addr)addr == laddr &&
  1351. dwarf_lineno(line, &lineno) == 0) {
  1352. tmp = dwarf_linesrc(line, NULL, NULL);
  1353. if (tmp) {
  1354. ppt->line = lineno;
  1355. ppt->file = strdup(tmp);
  1356. if (ppt->file == NULL) {
  1357. ret = -ENOMEM;
  1358. goto end;
  1359. }
  1360. found = true;
  1361. }
  1362. }
  1363. }
  1364. /* Find a corresponding function */
  1365. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1366. tmp = dwarf_diename(&spdie);
  1367. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1368. goto end;
  1369. if (ppt->line) {
  1370. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1371. &indie)) {
  1372. /* addr in an inline function */
  1373. tmp = dwarf_diename(&indie);
  1374. if (!tmp)
  1375. goto end;
  1376. ret = dwarf_decl_line(&indie, &lineno);
  1377. } else {
  1378. if (eaddr == addr) { /* Function entry */
  1379. lineno = ppt->line;
  1380. ret = 0;
  1381. } else
  1382. ret = dwarf_decl_line(&spdie, &lineno);
  1383. }
  1384. if (ret == 0) {
  1385. /* Make a relative line number */
  1386. ppt->line -= lineno;
  1387. goto found;
  1388. }
  1389. }
  1390. /* We don't have a line number, let's use offset */
  1391. ppt->offset = addr - (unsigned long)eaddr;
  1392. found:
  1393. ppt->function = strdup(tmp);
  1394. if (ppt->function == NULL) {
  1395. ret = -ENOMEM;
  1396. goto end;
  1397. }
  1398. found = true;
  1399. }
  1400. end:
  1401. if (dwfl)
  1402. dwfl_end(dwfl);
  1403. if (ret >= 0)
  1404. ret = found ? 1 : 0;
  1405. return ret;
  1406. }
  1407. /* Add a line and store the src path */
  1408. static int line_range_add_line(const char *src, unsigned int lineno,
  1409. struct line_range *lr)
  1410. {
  1411. /* Copy source path */
  1412. if (!lr->path) {
  1413. lr->path = strdup(src);
  1414. if (lr->path == NULL)
  1415. return -ENOMEM;
  1416. }
  1417. return line_list__add_line(&lr->line_list, lineno);
  1418. }
  1419. /* Search function declaration lines */
  1420. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  1421. {
  1422. struct dwarf_callback_param *param = data;
  1423. struct line_finder *lf = param->data;
  1424. const char *src;
  1425. int lineno;
  1426. src = dwarf_decl_file(sp_die);
  1427. if (src && strtailcmp(src, lf->fname) != 0)
  1428. return DWARF_CB_OK;
  1429. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  1430. (lf->lno_s > lineno || lf->lno_e < lineno))
  1431. return DWARF_CB_OK;
  1432. param->retval = line_range_add_line(src, lineno, lf->lr);
  1433. if (param->retval < 0)
  1434. return DWARF_CB_ABORT;
  1435. return DWARF_CB_OK;
  1436. }
  1437. static int find_line_range_func_decl_lines(struct line_finder *lf)
  1438. {
  1439. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1440. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  1441. return param.retval;
  1442. }
  1443. /* Find line range from its line number */
  1444. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1445. {
  1446. Dwarf_Lines *lines;
  1447. Dwarf_Line *line;
  1448. size_t nlines, i;
  1449. Dwarf_Addr addr;
  1450. int lineno, ret = 0;
  1451. const char *src;
  1452. Dwarf_Die die_mem;
  1453. line_list__init(&lf->lr->line_list);
  1454. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1455. pr_warning("No source lines found in this CU.\n");
  1456. return -ENOENT;
  1457. }
  1458. /* Search probable lines on lines list */
  1459. for (i = 0; i < nlines; i++) {
  1460. line = dwarf_onesrcline(lines, i);
  1461. if (dwarf_lineno(line, &lineno) != 0 ||
  1462. (lf->lno_s > lineno || lf->lno_e < lineno))
  1463. continue;
  1464. if (sp_die) {
  1465. /* Address filtering 1: does sp_die include addr? */
  1466. if (dwarf_lineaddr(line, &addr) != 0 ||
  1467. !dwarf_haspc(sp_die, addr))
  1468. continue;
  1469. /* Address filtering 2: No child include addr? */
  1470. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1471. continue;
  1472. }
  1473. /* TODO: Get fileno from line, but how? */
  1474. src = dwarf_linesrc(line, NULL, NULL);
  1475. if (strtailcmp(src, lf->fname) != 0)
  1476. continue;
  1477. ret = line_range_add_line(src, lineno, lf->lr);
  1478. if (ret < 0)
  1479. return ret;
  1480. }
  1481. /*
  1482. * Dwarf lines doesn't include function declarations. We have to
  1483. * check functions list or given function.
  1484. */
  1485. if (sp_die) {
  1486. src = dwarf_decl_file(sp_die);
  1487. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1488. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1489. ret = line_range_add_line(src, lineno, lf->lr);
  1490. } else
  1491. ret = find_line_range_func_decl_lines(lf);
  1492. /* Update status */
  1493. if (ret >= 0)
  1494. if (!list_empty(&lf->lr->line_list))
  1495. ret = lf->found = 1;
  1496. else
  1497. ret = 0; /* Lines are not found */
  1498. else {
  1499. free(lf->lr->path);
  1500. lf->lr->path = NULL;
  1501. }
  1502. return ret;
  1503. }
  1504. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1505. {
  1506. struct dwarf_callback_param *param = data;
  1507. param->retval = find_line_range_by_line(in_die, param->data);
  1508. return DWARF_CB_ABORT; /* No need to find other instances */
  1509. }
  1510. /* Search function from function name */
  1511. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1512. {
  1513. struct dwarf_callback_param *param = data;
  1514. struct line_finder *lf = param->data;
  1515. struct line_range *lr = lf->lr;
  1516. pr_debug("find (%llx) %s\n",
  1517. (unsigned long long)dwarf_dieoffset(sp_die),
  1518. dwarf_diename(sp_die));
  1519. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1520. die_compare_name(sp_die, lr->function)) {
  1521. lf->fname = dwarf_decl_file(sp_die);
  1522. dwarf_decl_line(sp_die, &lr->offset);
  1523. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1524. lf->lno_s = lr->offset + lr->start;
  1525. if (lf->lno_s < 0) /* Overflow */
  1526. lf->lno_s = INT_MAX;
  1527. lf->lno_e = lr->offset + lr->end;
  1528. if (lf->lno_e < 0) /* Overflow */
  1529. lf->lno_e = INT_MAX;
  1530. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1531. lr->start = lf->lno_s;
  1532. lr->end = lf->lno_e;
  1533. if (dwarf_func_inline(sp_die)) {
  1534. struct dwarf_callback_param _param;
  1535. _param.data = (void *)lf;
  1536. _param.retval = 0;
  1537. dwarf_func_inline_instances(sp_die,
  1538. line_range_inline_cb,
  1539. &_param);
  1540. param->retval = _param.retval;
  1541. } else
  1542. param->retval = find_line_range_by_line(sp_die, lf);
  1543. return DWARF_CB_ABORT;
  1544. }
  1545. return DWARF_CB_OK;
  1546. }
  1547. static int find_line_range_by_func(struct line_finder *lf)
  1548. {
  1549. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1550. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1551. return param.retval;
  1552. }
  1553. int find_line_range(int fd, struct line_range *lr)
  1554. {
  1555. struct line_finder lf = {.lr = lr, .found = 0};
  1556. int ret = 0;
  1557. Dwarf_Off off = 0, noff;
  1558. size_t cuhl;
  1559. Dwarf_Die *diep;
  1560. Dwarf *dbg = NULL;
  1561. Dwfl *dwfl;
  1562. Dwarf_Addr bias; /* Currently ignored */
  1563. const char *comp_dir;
  1564. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1565. if (!dbg) {
  1566. pr_warning("No dwarf info found in the vmlinux - "
  1567. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1568. return -EBADF;
  1569. }
  1570. /* Loop on CUs (Compilation Unit) */
  1571. while (!lf.found && ret >= 0) {
  1572. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1573. break;
  1574. /* Get the DIE(Debugging Information Entry) of this CU */
  1575. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1576. if (!diep)
  1577. continue;
  1578. /* Check if target file is included. */
  1579. if (lr->file)
  1580. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1581. else
  1582. lf.fname = 0;
  1583. if (!lr->file || lf.fname) {
  1584. if (lr->function)
  1585. ret = find_line_range_by_func(&lf);
  1586. else {
  1587. lf.lno_s = lr->start;
  1588. lf.lno_e = lr->end;
  1589. ret = find_line_range_by_line(NULL, &lf);
  1590. }
  1591. }
  1592. off = noff;
  1593. }
  1594. /* Store comp_dir */
  1595. if (lf.found) {
  1596. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1597. if (comp_dir) {
  1598. lr->comp_dir = strdup(comp_dir);
  1599. if (!lr->comp_dir)
  1600. ret = -ENOMEM;
  1601. }
  1602. }
  1603. pr_debug("path: %s\n", lr->path);
  1604. dwfl_end(dwfl);
  1605. return (ret < 0) ? ret : lf.found;
  1606. }