probe-finder.c 46 KB

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