probe-finder.c 50 KB

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