probe-finder.c 46 KB

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