probe-finder.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * probe-finder.c : C expression to kprobe event converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <dwarf-regs.h>
  34. #include <linux/bitops.h>
  35. #include "event.h"
  36. #include "debug.h"
  37. #include "util.h"
  38. #include "symbol.h"
  39. #include "probe-finder.h"
  40. /* Kprobe tracer basic type is up to u64 */
  41. #define MAX_BASIC_TYPE_BITS 64
  42. /* Line number list operations */
  43. /* Add a line to line number list */
  44. static int line_list__add_line(struct list_head *head, int line)
  45. {
  46. struct line_node *ln;
  47. struct list_head *p;
  48. /* Reverse search, because new line will be the last one */
  49. list_for_each_entry_reverse(ln, head, list) {
  50. if (ln->line < line) {
  51. p = &ln->list;
  52. goto found;
  53. } else if (ln->line == line) /* Already exist */
  54. return 1;
  55. }
  56. /* List is empty, or the smallest entry */
  57. p = head;
  58. found:
  59. pr_debug("line list: add a line %u\n", line);
  60. ln = zalloc(sizeof(struct line_node));
  61. if (ln == NULL)
  62. return -ENOMEM;
  63. ln->line = line;
  64. INIT_LIST_HEAD(&ln->list);
  65. list_add(&ln->list, p);
  66. return 0;
  67. }
  68. /* Check if the line in line number list */
  69. static int line_list__has_line(struct list_head *head, int line)
  70. {
  71. struct line_node *ln;
  72. /* Reverse search, because new line will be the last one */
  73. list_for_each_entry(ln, head, list)
  74. if (ln->line == line)
  75. return 1;
  76. return 0;
  77. }
  78. /* Init line number list */
  79. static void line_list__init(struct list_head *head)
  80. {
  81. INIT_LIST_HEAD(head);
  82. }
  83. /* Free line number list */
  84. static void line_list__free(struct list_head *head)
  85. {
  86. struct line_node *ln;
  87. while (!list_empty(head)) {
  88. ln = list_first_entry(head, struct line_node, list);
  89. list_del(&ln->list);
  90. free(ln);
  91. }
  92. }
  93. /* Dwarf FL wrappers */
  94. static char *debuginfo_path; /* Currently dummy */
  95. static const Dwfl_Callbacks offline_callbacks = {
  96. .find_debuginfo = dwfl_standard_find_debuginfo,
  97. .debuginfo_path = &debuginfo_path,
  98. .section_address = dwfl_offline_section_address,
  99. /* We use this table for core files too. */
  100. .find_elf = dwfl_build_id_find_elf,
  101. };
  102. /* Get a Dwarf from offline image */
  103. static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
  104. {
  105. Dwfl_Module *mod;
  106. Dwarf *dbg = NULL;
  107. if (!dwflp)
  108. return NULL;
  109. *dwflp = dwfl_begin(&offline_callbacks);
  110. if (!*dwflp)
  111. return NULL;
  112. mod = dwfl_report_offline(*dwflp, "", "", fd);
  113. if (!mod)
  114. goto error;
  115. dbg = dwfl_module_getdwarf(mod, bias);
  116. if (!dbg) {
  117. error:
  118. dwfl_end(*dwflp);
  119. *dwflp = NULL;
  120. }
  121. return dbg;
  122. }
  123. #if _ELFUTILS_PREREQ(0, 148)
  124. /* This method is buggy if elfutils is older than 0.148 */
  125. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  126. void **userdata,
  127. const char *module_name,
  128. Dwarf_Addr base,
  129. char **file_name, Elf **elfp)
  130. {
  131. int fd;
  132. const char *path = kernel_get_module_path(module_name);
  133. pr_debug2("Use file %s for %s\n", path, module_name);
  134. if (path) {
  135. fd = open(path, O_RDONLY);
  136. if (fd >= 0) {
  137. *file_name = strdup(path);
  138. return fd;
  139. }
  140. }
  141. /* If failed, try to call standard method */
  142. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  143. file_name, elfp);
  144. }
  145. static const Dwfl_Callbacks kernel_callbacks = {
  146. .find_debuginfo = dwfl_standard_find_debuginfo,
  147. .debuginfo_path = &debuginfo_path,
  148. .find_elf = __linux_kernel_find_elf,
  149. .section_address = dwfl_linux_kernel_module_section_address,
  150. };
  151. /* Get a Dwarf from live kernel image */
  152. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
  153. Dwarf_Addr *bias)
  154. {
  155. Dwarf *dbg;
  156. if (!dwflp)
  157. return NULL;
  158. *dwflp = dwfl_begin(&kernel_callbacks);
  159. if (!*dwflp)
  160. return NULL;
  161. /* Load the kernel dwarves: Don't care the result here */
  162. dwfl_linux_kernel_report_kernel(*dwflp);
  163. dwfl_linux_kernel_report_modules(*dwflp);
  164. dbg = dwfl_addrdwarf(*dwflp, addr, bias);
  165. /* Here, check whether we could get a real dwarf */
  166. if (!dbg) {
  167. pr_debug("Failed to find kernel dwarf at %lx\n",
  168. (unsigned long)addr);
  169. dwfl_end(*dwflp);
  170. *dwflp = NULL;
  171. }
  172. return dbg;
  173. }
  174. #else
  175. /* With older elfutils, this just support kernel module... */
  176. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
  177. Dwarf_Addr *bias)
  178. {
  179. int fd;
  180. const char *path = kernel_get_module_path("kernel");
  181. if (!path) {
  182. pr_err("Failed to find vmlinux path\n");
  183. return NULL;
  184. }
  185. pr_debug2("Use file %s for debuginfo\n", path);
  186. fd = open(path, O_RDONLY);
  187. if (fd < 0)
  188. return NULL;
  189. return dwfl_init_offline_dwarf(fd, dwflp, bias);
  190. }
  191. #endif
  192. /*
  193. * Probe finder related functions
  194. */
  195. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  196. {
  197. struct probe_trace_arg_ref *ref;
  198. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  199. if (ref != NULL)
  200. ref->offset = offs;
  201. return ref;
  202. }
  203. /*
  204. * Convert a location into trace_arg.
  205. * If tvar == NULL, this just checks variable can be converted.
  206. */
  207. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  208. Dwarf_Op *fb_ops,
  209. struct probe_trace_arg *tvar)
  210. {
  211. Dwarf_Attribute attr;
  212. Dwarf_Op *op;
  213. size_t nops;
  214. unsigned int regn;
  215. Dwarf_Word offs = 0;
  216. bool ref = false;
  217. const char *regs;
  218. int ret;
  219. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  220. goto static_var;
  221. /* TODO: handle more than 1 exprs */
  222. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  223. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  224. nops == 0) {
  225. /* TODO: Support const_value */
  226. return -ENOENT;
  227. }
  228. if (op->atom == DW_OP_addr) {
  229. static_var:
  230. if (!tvar)
  231. return 0;
  232. /* Static variables on memory (not stack), make @varname */
  233. ret = strlen(dwarf_diename(vr_die));
  234. tvar->value = zalloc(ret + 2);
  235. if (tvar->value == NULL)
  236. return -ENOMEM;
  237. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  238. tvar->ref = alloc_trace_arg_ref((long)offs);
  239. if (tvar->ref == NULL)
  240. return -ENOMEM;
  241. return 0;
  242. }
  243. /* If this is based on frame buffer, set the offset */
  244. if (op->atom == DW_OP_fbreg) {
  245. if (fb_ops == NULL)
  246. return -ENOTSUP;
  247. ref = true;
  248. offs = op->number;
  249. op = &fb_ops[0];
  250. }
  251. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  252. regn = op->atom - DW_OP_breg0;
  253. offs += op->number;
  254. ref = true;
  255. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  256. regn = op->atom - DW_OP_reg0;
  257. } else if (op->atom == DW_OP_bregx) {
  258. regn = op->number;
  259. offs += op->number2;
  260. ref = true;
  261. } else if (op->atom == DW_OP_regx) {
  262. regn = op->number;
  263. } else {
  264. pr_debug("DW_OP %x is not supported.\n", op->atom);
  265. return -ENOTSUP;
  266. }
  267. if (!tvar)
  268. return 0;
  269. regs = get_arch_regstr(regn);
  270. if (!regs) {
  271. /* This should be a bug in DWARF or this tool */
  272. pr_warning("Mapping for the register number %u "
  273. "missing on this architecture.\n", regn);
  274. return -ERANGE;
  275. }
  276. tvar->value = strdup(regs);
  277. if (tvar->value == NULL)
  278. return -ENOMEM;
  279. if (ref) {
  280. tvar->ref = alloc_trace_arg_ref((long)offs);
  281. if (tvar->ref == NULL)
  282. return -ENOMEM;
  283. }
  284. return 0;
  285. }
  286. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  287. static int convert_variable_type(Dwarf_Die *vr_die,
  288. struct probe_trace_arg *tvar,
  289. const char *cast)
  290. {
  291. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  292. Dwarf_Die type;
  293. char buf[16];
  294. int bsize, boffs, total;
  295. int ret;
  296. /* TODO: check all types */
  297. if (cast && strcmp(cast, "string") != 0) {
  298. /* Non string type is OK */
  299. tvar->type = strdup(cast);
  300. return (tvar->type == NULL) ? -ENOMEM : 0;
  301. }
  302. bsize = dwarf_bitsize(vr_die);
  303. if (bsize > 0) {
  304. /* This is a bitfield */
  305. boffs = dwarf_bitoffset(vr_die);
  306. total = dwarf_bytesize(vr_die);
  307. if (boffs < 0 || total < 0)
  308. return -ENOENT;
  309. ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
  310. BYTES_TO_BITS(total));
  311. goto formatted;
  312. }
  313. if (die_get_real_type(vr_die, &type) == NULL) {
  314. pr_warning("Failed to get a type information of %s.\n",
  315. dwarf_diename(vr_die));
  316. return -ENOENT;
  317. }
  318. pr_debug("%s type is %s.\n",
  319. dwarf_diename(vr_die), dwarf_diename(&type));
  320. if (cast && strcmp(cast, "string") == 0) { /* String type */
  321. ret = dwarf_tag(&type);
  322. if (ret != DW_TAG_pointer_type &&
  323. ret != DW_TAG_array_type) {
  324. pr_warning("Failed to cast into string: "
  325. "%s(%s) is not a pointer nor array.\n",
  326. dwarf_diename(vr_die), dwarf_diename(&type));
  327. return -EINVAL;
  328. }
  329. if (ret == DW_TAG_pointer_type) {
  330. if (die_get_real_type(&type, &type) == NULL) {
  331. pr_warning("Failed to get a type"
  332. " information.\n");
  333. return -ENOENT;
  334. }
  335. while (*ref_ptr)
  336. ref_ptr = &(*ref_ptr)->next;
  337. /* Add new reference with offset +0 */
  338. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  339. if (*ref_ptr == NULL) {
  340. pr_warning("Out of memory error\n");
  341. return -ENOMEM;
  342. }
  343. }
  344. if (!die_compare_name(&type, "char") &&
  345. !die_compare_name(&type, "unsigned char")) {
  346. pr_warning("Failed to cast into string: "
  347. "%s is not (unsigned) char *.\n",
  348. dwarf_diename(vr_die));
  349. return -EINVAL;
  350. }
  351. tvar->type = strdup(cast);
  352. return (tvar->type == NULL) ? -ENOMEM : 0;
  353. }
  354. ret = dwarf_bytesize(&type);
  355. if (ret <= 0)
  356. /* No size ... try to use default type */
  357. return 0;
  358. ret = BYTES_TO_BITS(ret);
  359. /* Check the bitwidth */
  360. if (ret > MAX_BASIC_TYPE_BITS) {
  361. pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
  362. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  363. ret = MAX_BASIC_TYPE_BITS;
  364. }
  365. ret = snprintf(buf, 16, "%c%d",
  366. die_is_signed_type(&type) ? 's' : 'u', ret);
  367. formatted:
  368. if (ret < 0 || ret >= 16) {
  369. if (ret >= 16)
  370. ret = -E2BIG;
  371. pr_warning("Failed to convert variable type: %s\n",
  372. strerror(-ret));
  373. return ret;
  374. }
  375. tvar->type = strdup(buf);
  376. if (tvar->type == NULL)
  377. return -ENOMEM;
  378. return 0;
  379. }
  380. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  381. struct perf_probe_arg_field *field,
  382. struct probe_trace_arg_ref **ref_ptr,
  383. Dwarf_Die *die_mem)
  384. {
  385. struct probe_trace_arg_ref *ref = *ref_ptr;
  386. Dwarf_Die type;
  387. Dwarf_Word offs;
  388. int ret, tag;
  389. pr_debug("converting %s in %s\n", field->name, varname);
  390. if (die_get_real_type(vr_die, &type) == NULL) {
  391. pr_warning("Failed to get the type of %s.\n", varname);
  392. return -ENOENT;
  393. }
  394. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  395. tag = dwarf_tag(&type);
  396. if (field->name[0] == '[' &&
  397. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  398. if (field->next)
  399. /* Save original type for next field */
  400. memcpy(die_mem, &type, sizeof(*die_mem));
  401. /* Get the type of this array */
  402. if (die_get_real_type(&type, &type) == NULL) {
  403. pr_warning("Failed to get the type of %s.\n", varname);
  404. return -ENOENT;
  405. }
  406. pr_debug2("Array real type: (%x)\n",
  407. (unsigned)dwarf_dieoffset(&type));
  408. if (tag == DW_TAG_pointer_type) {
  409. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  410. if (ref == NULL)
  411. return -ENOMEM;
  412. if (*ref_ptr)
  413. (*ref_ptr)->next = ref;
  414. else
  415. *ref_ptr = ref;
  416. }
  417. ref->offset += dwarf_bytesize(&type) * field->index;
  418. if (!field->next)
  419. /* Save vr_die for converting types */
  420. memcpy(die_mem, vr_die, sizeof(*die_mem));
  421. goto next;
  422. } else if (tag == DW_TAG_pointer_type) {
  423. /* Check the pointer and dereference */
  424. if (!field->ref) {
  425. pr_err("Semantic error: %s must be referred by '->'\n",
  426. field->name);
  427. return -EINVAL;
  428. }
  429. /* Get the type pointed by this pointer */
  430. if (die_get_real_type(&type, &type) == NULL) {
  431. pr_warning("Failed to get the type of %s.\n", varname);
  432. return -ENOENT;
  433. }
  434. /* Verify it is a data structure */
  435. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  436. pr_warning("%s is not a data structure.\n", varname);
  437. return -EINVAL;
  438. }
  439. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  440. if (ref == NULL)
  441. return -ENOMEM;
  442. if (*ref_ptr)
  443. (*ref_ptr)->next = ref;
  444. else
  445. *ref_ptr = ref;
  446. } else {
  447. /* Verify it is a data structure */
  448. if (tag != DW_TAG_structure_type) {
  449. pr_warning("%s is not a data structure.\n", varname);
  450. return -EINVAL;
  451. }
  452. if (field->name[0] == '[') {
  453. pr_err("Semantic error: %s is not a pointor"
  454. " nor array.\n", varname);
  455. return -EINVAL;
  456. }
  457. if (field->ref) {
  458. pr_err("Semantic error: %s must be referred by '.'\n",
  459. field->name);
  460. return -EINVAL;
  461. }
  462. if (!ref) {
  463. pr_warning("Structure on a register is not "
  464. "supported yet.\n");
  465. return -ENOTSUP;
  466. }
  467. }
  468. if (die_find_member(&type, field->name, die_mem) == NULL) {
  469. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  470. dwarf_diename(&type), field->name);
  471. return -EINVAL;
  472. }
  473. /* Get the offset of the field */
  474. ret = die_get_data_member_location(die_mem, &offs);
  475. if (ret < 0) {
  476. pr_warning("Failed to get the offset of %s.\n", field->name);
  477. return ret;
  478. }
  479. ref->offset += (long)offs;
  480. next:
  481. /* Converting next field */
  482. if (field->next)
  483. return convert_variable_fields(die_mem, field->name,
  484. field->next, &ref, die_mem);
  485. else
  486. return 0;
  487. }
  488. /* Show a variables in kprobe event format */
  489. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  490. {
  491. Dwarf_Die die_mem;
  492. int ret;
  493. pr_debug("Converting variable %s into trace event.\n",
  494. dwarf_diename(vr_die));
  495. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  496. pf->tvar);
  497. if (ret == -ENOENT)
  498. pr_err("Failed to find the location of %s at this address.\n"
  499. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  500. else if (ret == -ENOTSUP)
  501. pr_err("Sorry, we don't support this variable location yet.\n");
  502. else if (pf->pvar->field) {
  503. ret = convert_variable_fields(vr_die, pf->pvar->var,
  504. pf->pvar->field, &pf->tvar->ref,
  505. &die_mem);
  506. vr_die = &die_mem;
  507. }
  508. if (ret == 0)
  509. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  510. /* *expr will be cached in libdw. Don't free it. */
  511. return ret;
  512. }
  513. /* Find a variable in a subprogram die */
  514. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  515. {
  516. Dwarf_Die vr_die, *scopes;
  517. char buf[32], *ptr;
  518. int ret, nscopes;
  519. if (!is_c_varname(pf->pvar->var)) {
  520. /* Copy raw parameters */
  521. pf->tvar->value = strdup(pf->pvar->var);
  522. if (pf->tvar->value == NULL)
  523. return -ENOMEM;
  524. if (pf->pvar->type) {
  525. pf->tvar->type = strdup(pf->pvar->type);
  526. if (pf->tvar->type == NULL)
  527. return -ENOMEM;
  528. }
  529. if (pf->pvar->name) {
  530. pf->tvar->name = strdup(pf->pvar->name);
  531. if (pf->tvar->name == NULL)
  532. return -ENOMEM;
  533. } else
  534. pf->tvar->name = NULL;
  535. return 0;
  536. }
  537. if (pf->pvar->name)
  538. pf->tvar->name = strdup(pf->pvar->name);
  539. else {
  540. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  541. if (ret < 0)
  542. return ret;
  543. ptr = strchr(buf, ':'); /* Change type separator to _ */
  544. if (ptr)
  545. *ptr = '_';
  546. pf->tvar->name = strdup(buf);
  547. }
  548. if (pf->tvar->name == NULL)
  549. return -ENOMEM;
  550. pr_debug("Searching '%s' variable in context.\n",
  551. pf->pvar->var);
  552. /* Search child die for local variables and parameters. */
  553. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  554. ret = convert_variable(&vr_die, pf);
  555. else {
  556. /* Search upper class */
  557. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  558. while (nscopes-- > 1) {
  559. pr_debug("Searching variables in %s\n",
  560. dwarf_diename(&scopes[nscopes]));
  561. /* We should check this scope, so give dummy address */
  562. if (die_find_variable_at(&scopes[nscopes],
  563. pf->pvar->var, 0,
  564. &vr_die)) {
  565. ret = convert_variable(&vr_die, pf);
  566. goto found;
  567. }
  568. }
  569. if (scopes)
  570. free(scopes);
  571. ret = -ENOENT;
  572. }
  573. found:
  574. if (ret < 0)
  575. pr_warning("Failed to find '%s' in this function.\n",
  576. pf->pvar->var);
  577. return ret;
  578. }
  579. /* Convert subprogram DIE to trace point */
  580. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  581. bool retprobe, struct probe_trace_point *tp)
  582. {
  583. Dwarf_Addr eaddr;
  584. const char *name;
  585. /* Copy the name of probe point */
  586. name = dwarf_diename(sp_die);
  587. if (name) {
  588. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  589. pr_warning("Failed to get entry address of %s\n",
  590. dwarf_diename(sp_die));
  591. return -ENOENT;
  592. }
  593. tp->symbol = strdup(name);
  594. if (tp->symbol == NULL)
  595. return -ENOMEM;
  596. tp->offset = (unsigned long)(paddr - eaddr);
  597. } else
  598. /* This function has no name. */
  599. tp->offset = (unsigned long)paddr;
  600. /* Return probe must be on the head of a subprogram */
  601. if (retprobe) {
  602. if (eaddr != paddr) {
  603. pr_warning("Return probe must be on the head of"
  604. " a real function.\n");
  605. return -EINVAL;
  606. }
  607. tp->retprobe = true;
  608. }
  609. return 0;
  610. }
  611. /* Call probe_finder callback with real subprogram DIE */
  612. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  613. {
  614. Dwarf_Die die_mem;
  615. Dwarf_Attribute fb_attr;
  616. size_t nops;
  617. int ret;
  618. /* If no real subprogram, find a real one */
  619. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  620. sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem);
  621. if (!sp_die) {
  622. pr_warning("Failed to find probe point in any "
  623. "functions.\n");
  624. return -ENOENT;
  625. }
  626. }
  627. /* Get the frame base attribute/ops */
  628. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  629. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  630. if (ret <= 0 || nops == 0) {
  631. pf->fb_ops = NULL;
  632. #if _ELFUTILS_PREREQ(0, 142)
  633. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  634. pf->cfi != NULL) {
  635. Dwarf_Frame *frame;
  636. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  637. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  638. pr_warning("Failed to get call frame on 0x%jx\n",
  639. (uintmax_t)pf->addr);
  640. return -ENOENT;
  641. }
  642. #endif
  643. }
  644. /* Call finder's callback handler */
  645. ret = pf->callback(sp_die, pf);
  646. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  647. pf->fb_ops = NULL;
  648. return ret;
  649. }
  650. static int probe_point_line_walker(const char *fname, int lineno,
  651. Dwarf_Addr addr, void *data)
  652. {
  653. struct probe_finder *pf = data;
  654. int ret;
  655. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  656. return 0;
  657. pf->addr = addr;
  658. ret = call_probe_finder(NULL, pf);
  659. /* Continue if no error, because the line will be in inline function */
  660. return ret < 0 ? ret : 0;
  661. }
  662. /* Find probe point from its line number */
  663. static int find_probe_point_by_line(struct probe_finder *pf)
  664. {
  665. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  666. }
  667. /* Find lines which match lazy pattern */
  668. static int find_lazy_match_lines(struct list_head *head,
  669. const char *fname, const char *pat)
  670. {
  671. FILE *fp;
  672. char *line = NULL;
  673. size_t line_len;
  674. ssize_t len;
  675. int count = 0, linenum = 1;
  676. fp = fopen(fname, "r");
  677. if (!fp) {
  678. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  679. return -errno;
  680. }
  681. while ((len = getline(&line, &line_len, fp)) > 0) {
  682. if (line[len - 1] == '\n')
  683. line[len - 1] = '\0';
  684. if (strlazymatch(line, pat)) {
  685. line_list__add_line(head, linenum);
  686. count++;
  687. }
  688. linenum++;
  689. }
  690. if (ferror(fp))
  691. count = -errno;
  692. free(line);
  693. fclose(fp);
  694. if (count == 0)
  695. pr_debug("No matched lines found in %s.\n", fname);
  696. return count;
  697. }
  698. static int probe_point_lazy_walker(const char *fname, int lineno,
  699. Dwarf_Addr addr, void *data)
  700. {
  701. struct probe_finder *pf = data;
  702. int ret;
  703. if (!line_list__has_line(&pf->lcache, lineno) ||
  704. strtailcmp(fname, pf->fname) != 0)
  705. return 0;
  706. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  707. lineno, (unsigned long long)addr);
  708. pf->addr = addr;
  709. ret = call_probe_finder(NULL, pf);
  710. /*
  711. * Continue if no error, because the lazy pattern will match
  712. * to other lines
  713. */
  714. return ret < 0 ? ret : 0;
  715. }
  716. /* Find probe points from lazy pattern */
  717. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  718. {
  719. int ret = 0;
  720. if (list_empty(&pf->lcache)) {
  721. /* Matching lazy line pattern */
  722. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  723. pf->pev->point.lazy_line);
  724. if (ret <= 0)
  725. return ret;
  726. }
  727. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  728. }
  729. /* Callback parameter with return value */
  730. struct dwarf_callback_param {
  731. void *data;
  732. int retval;
  733. };
  734. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  735. {
  736. struct dwarf_callback_param *param = data;
  737. struct probe_finder *pf = param->data;
  738. struct perf_probe_point *pp = &pf->pev->point;
  739. Dwarf_Addr addr;
  740. if (pp->lazy_line)
  741. param->retval = find_probe_point_lazy(in_die, pf);
  742. else {
  743. /* Get probe address */
  744. if (dwarf_entrypc(in_die, &addr) != 0) {
  745. pr_warning("Failed to get entry address of %s.\n",
  746. dwarf_diename(in_die));
  747. param->retval = -ENOENT;
  748. return DWARF_CB_ABORT;
  749. }
  750. pf->addr = addr;
  751. pf->addr += pp->offset;
  752. pr_debug("found inline addr: 0x%jx\n",
  753. (uintmax_t)pf->addr);
  754. param->retval = call_probe_finder(in_die, pf);
  755. if (param->retval < 0)
  756. return DWARF_CB_ABORT;
  757. }
  758. return DWARF_CB_OK;
  759. }
  760. /* Search function from function name */
  761. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  762. {
  763. struct dwarf_callback_param *param = data;
  764. struct probe_finder *pf = param->data;
  765. struct perf_probe_point *pp = &pf->pev->point;
  766. /* Check tag and diename */
  767. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  768. !die_compare_name(sp_die, pp->function))
  769. return DWARF_CB_OK;
  770. /* Check declared file */
  771. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  772. return DWARF_CB_OK;
  773. pf->fname = dwarf_decl_file(sp_die);
  774. if (pp->line) { /* Function relative line */
  775. dwarf_decl_line(sp_die, &pf->lno);
  776. pf->lno += pp->line;
  777. param->retval = find_probe_point_by_line(pf);
  778. } else if (!dwarf_func_inline(sp_die)) {
  779. /* Real function */
  780. if (pp->lazy_line)
  781. param->retval = find_probe_point_lazy(sp_die, pf);
  782. else {
  783. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  784. pr_warning("Failed to get entry address of "
  785. "%s.\n", dwarf_diename(sp_die));
  786. param->retval = -ENOENT;
  787. return DWARF_CB_ABORT;
  788. }
  789. pf->addr += pp->offset;
  790. /* TODO: Check the address in this function */
  791. param->retval = call_probe_finder(sp_die, pf);
  792. }
  793. } else {
  794. struct dwarf_callback_param _param = {.data = (void *)pf,
  795. .retval = 0};
  796. /* Inlined function: search instances */
  797. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  798. &_param);
  799. param->retval = _param.retval;
  800. }
  801. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  802. }
  803. static int find_probe_point_by_func(struct probe_finder *pf)
  804. {
  805. struct dwarf_callback_param _param = {.data = (void *)pf,
  806. .retval = 0};
  807. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  808. return _param.retval;
  809. }
  810. struct pubname_callback_param {
  811. char *function;
  812. char *file;
  813. Dwarf_Die *cu_die;
  814. Dwarf_Die *sp_die;
  815. int found;
  816. };
  817. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  818. {
  819. struct pubname_callback_param *param = data;
  820. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  821. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  822. return DWARF_CB_OK;
  823. if (die_compare_name(param->sp_die, param->function)) {
  824. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  825. return DWARF_CB_OK;
  826. if (param->file &&
  827. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  828. return DWARF_CB_OK;
  829. param->found = 1;
  830. return DWARF_CB_ABORT;
  831. }
  832. }
  833. return DWARF_CB_OK;
  834. }
  835. /* Find probe points from debuginfo */
  836. static int find_probes(int fd, struct probe_finder *pf)
  837. {
  838. struct perf_probe_point *pp = &pf->pev->point;
  839. Dwarf_Off off, noff;
  840. size_t cuhl;
  841. Dwarf_Die *diep;
  842. Dwarf *dbg = NULL;
  843. Dwfl *dwfl;
  844. Dwarf_Addr bias; /* Currently ignored */
  845. int ret = 0;
  846. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  847. if (!dbg) {
  848. pr_warning("No debug information found in the vmlinux - "
  849. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  850. close(fd); /* Without dwfl_end(), fd isn't closed. */
  851. return -EBADF;
  852. }
  853. #if _ELFUTILS_PREREQ(0, 142)
  854. /* Get the call frame information from this dwarf */
  855. pf->cfi = dwarf_getcfi(dbg);
  856. #endif
  857. off = 0;
  858. line_list__init(&pf->lcache);
  859. /* Fastpath: lookup by function name from .debug_pubnames section */
  860. if (pp->function) {
  861. struct pubname_callback_param pubname_param = {
  862. .function = pp->function,
  863. .file = pp->file,
  864. .cu_die = &pf->cu_die,
  865. .sp_die = &pf->sp_die,
  866. .found = 0,
  867. };
  868. struct dwarf_callback_param probe_param = {
  869. .data = pf,
  870. };
  871. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  872. if (pubname_param.found) {
  873. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  874. if (ret)
  875. goto found;
  876. }
  877. }
  878. /* Loop on CUs (Compilation Unit) */
  879. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  880. /* Get the DIE(Debugging Information Entry) of this CU */
  881. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  882. if (!diep)
  883. continue;
  884. /* Check if target file is included. */
  885. if (pp->file)
  886. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  887. else
  888. pf->fname = NULL;
  889. if (!pp->file || pf->fname) {
  890. if (pp->function)
  891. ret = find_probe_point_by_func(pf);
  892. else if (pp->lazy_line)
  893. ret = find_probe_point_lazy(NULL, pf);
  894. else {
  895. pf->lno = pp->line;
  896. ret = find_probe_point_by_line(pf);
  897. }
  898. if (ret < 0)
  899. break;
  900. }
  901. off = noff;
  902. }
  903. found:
  904. line_list__free(&pf->lcache);
  905. if (dwfl)
  906. dwfl_end(dwfl);
  907. return ret;
  908. }
  909. /* Add a found probe point into trace event list */
  910. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  911. {
  912. struct trace_event_finder *tf =
  913. container_of(pf, struct trace_event_finder, pf);
  914. struct probe_trace_event *tev;
  915. int ret, i;
  916. /* Check number of tevs */
  917. if (tf->ntevs == tf->max_tevs) {
  918. pr_warning("Too many( > %d) probe point found.\n",
  919. tf->max_tevs);
  920. return -ERANGE;
  921. }
  922. tev = &tf->tevs[tf->ntevs++];
  923. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  924. &tev->point);
  925. if (ret < 0)
  926. return ret;
  927. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  928. tev->point.offset);
  929. /* Find each argument */
  930. tev->nargs = pf->pev->nargs;
  931. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  932. if (tev->args == NULL)
  933. return -ENOMEM;
  934. for (i = 0; i < pf->pev->nargs; i++) {
  935. pf->pvar = &pf->pev->args[i];
  936. pf->tvar = &tev->args[i];
  937. ret = find_variable(sp_die, pf);
  938. if (ret != 0)
  939. return ret;
  940. }
  941. return 0;
  942. }
  943. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  944. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  945. struct probe_trace_event **tevs, int max_tevs)
  946. {
  947. struct trace_event_finder tf = {
  948. .pf = {.pev = pev, .callback = add_probe_trace_event},
  949. .max_tevs = max_tevs};
  950. int ret;
  951. /* Allocate result tevs array */
  952. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  953. if (*tevs == NULL)
  954. return -ENOMEM;
  955. tf.tevs = *tevs;
  956. tf.ntevs = 0;
  957. ret = find_probes(fd, &tf.pf);
  958. if (ret < 0) {
  959. free(*tevs);
  960. *tevs = NULL;
  961. return ret;
  962. }
  963. return (ret < 0) ? ret : tf.ntevs;
  964. }
  965. #define MAX_VAR_LEN 64
  966. /* Collect available variables in this scope */
  967. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  968. {
  969. struct available_var_finder *af = data;
  970. struct variable_list *vl;
  971. char buf[MAX_VAR_LEN];
  972. int tag, ret;
  973. vl = &af->vls[af->nvls - 1];
  974. tag = dwarf_tag(die_mem);
  975. if (tag == DW_TAG_formal_parameter ||
  976. tag == DW_TAG_variable) {
  977. ret = convert_variable_location(die_mem, af->pf.addr,
  978. af->pf.fb_ops, NULL);
  979. if (ret == 0) {
  980. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  981. pr_debug2("Add new var: %s\n", buf);
  982. if (ret > 0)
  983. strlist__add(vl->vars, buf);
  984. }
  985. }
  986. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  987. return DIE_FIND_CB_CONTINUE;
  988. else
  989. return DIE_FIND_CB_SIBLING;
  990. }
  991. /* Add a found vars into available variables list */
  992. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  993. {
  994. struct available_var_finder *af =
  995. container_of(pf, struct available_var_finder, pf);
  996. struct variable_list *vl;
  997. Dwarf_Die die_mem, *scopes = NULL;
  998. int ret, nscopes;
  999. /* Check number of tevs */
  1000. if (af->nvls == af->max_vls) {
  1001. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1002. return -ERANGE;
  1003. }
  1004. vl = &af->vls[af->nvls++];
  1005. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1006. &vl->point);
  1007. if (ret < 0)
  1008. return ret;
  1009. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1010. vl->point.offset);
  1011. /* Find local variables */
  1012. vl->vars = strlist__new(true, NULL);
  1013. if (vl->vars == NULL)
  1014. return -ENOMEM;
  1015. af->child = true;
  1016. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1017. /* Find external variables */
  1018. if (!af->externs)
  1019. goto out;
  1020. /* Don't need to search child DIE for externs. */
  1021. af->child = false;
  1022. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1023. while (nscopes-- > 1)
  1024. die_find_child(&scopes[nscopes], collect_variables_cb,
  1025. (void *)af, &die_mem);
  1026. if (scopes)
  1027. free(scopes);
  1028. out:
  1029. if (strlist__empty(vl->vars)) {
  1030. strlist__delete(vl->vars);
  1031. vl->vars = NULL;
  1032. }
  1033. return ret;
  1034. }
  1035. /* Find available variables at given probe point */
  1036. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1037. struct variable_list **vls, int max_vls,
  1038. bool externs)
  1039. {
  1040. struct available_var_finder af = {
  1041. .pf = {.pev = pev, .callback = add_available_vars},
  1042. .max_vls = max_vls, .externs = externs};
  1043. int ret;
  1044. /* Allocate result vls array */
  1045. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1046. if (*vls == NULL)
  1047. return -ENOMEM;
  1048. af.vls = *vls;
  1049. af.nvls = 0;
  1050. ret = find_probes(fd, &af.pf);
  1051. if (ret < 0) {
  1052. /* Free vlist for error */
  1053. while (af.nvls--) {
  1054. if (af.vls[af.nvls].point.symbol)
  1055. free(af.vls[af.nvls].point.symbol);
  1056. if (af.vls[af.nvls].vars)
  1057. strlist__delete(af.vls[af.nvls].vars);
  1058. }
  1059. free(af.vls);
  1060. *vls = NULL;
  1061. return ret;
  1062. }
  1063. return (ret < 0) ? ret : af.nvls;
  1064. }
  1065. /* Reverse search */
  1066. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1067. {
  1068. Dwarf_Die cudie, spdie, indie;
  1069. Dwarf *dbg = NULL;
  1070. Dwfl *dwfl = NULL;
  1071. Dwarf_Addr _addr, baseaddr, bias = 0;
  1072. const char *fname = NULL, *func = NULL, *tmp;
  1073. int baseline = 0, lineno = 0, ret = 0;
  1074. /* Open the live linux kernel */
  1075. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1076. if (!dbg) {
  1077. pr_warning("No debug information found in the vmlinux - "
  1078. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1079. ret = -EINVAL;
  1080. goto end;
  1081. }
  1082. /* Adjust address with bias */
  1083. addr += bias;
  1084. /* Find cu die */
  1085. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1086. pr_warning("Failed to find debug information for address %lx\n",
  1087. addr);
  1088. ret = -EINVAL;
  1089. goto end;
  1090. }
  1091. /* Find a corresponding line (filename and lineno) */
  1092. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1093. /* Don't care whether it failed or not */
  1094. /* Find a corresponding function (name, baseline and baseaddr) */
  1095. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1096. /* Get function entry information */
  1097. tmp = dwarf_diename(&spdie);
  1098. if (!tmp ||
  1099. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1100. dwarf_decl_line(&spdie, &baseline) != 0)
  1101. goto post;
  1102. func = tmp;
  1103. if (addr == (unsigned long)baseaddr)
  1104. /* Function entry - Relative line number is 0 */
  1105. lineno = baseline;
  1106. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1107. &indie)) {
  1108. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1109. _addr == addr)
  1110. /*
  1111. * addr is at an inline function entry.
  1112. * In this case, lineno should be the call-site
  1113. * line number.
  1114. */
  1115. lineno = die_get_call_lineno(&indie);
  1116. else {
  1117. /*
  1118. * addr is in an inline function body.
  1119. * Since lineno points one of the lines
  1120. * of the inline function, baseline should
  1121. * be the entry line of the inline function.
  1122. */
  1123. tmp = dwarf_diename(&indie);
  1124. if (tmp &&
  1125. dwarf_decl_line(&spdie, &baseline) == 0)
  1126. func = tmp;
  1127. }
  1128. }
  1129. }
  1130. post:
  1131. /* Make a relative line number or an offset */
  1132. if (lineno)
  1133. ppt->line = lineno - baseline;
  1134. else if (func)
  1135. ppt->offset = addr - (unsigned long)baseaddr;
  1136. /* Duplicate strings */
  1137. if (func) {
  1138. ppt->function = strdup(func);
  1139. if (ppt->function == NULL) {
  1140. ret = -ENOMEM;
  1141. goto end;
  1142. }
  1143. }
  1144. if (fname) {
  1145. ppt->file = strdup(fname);
  1146. if (ppt->file == NULL) {
  1147. if (ppt->function) {
  1148. free(ppt->function);
  1149. ppt->function = NULL;
  1150. }
  1151. ret = -ENOMEM;
  1152. goto end;
  1153. }
  1154. }
  1155. end:
  1156. if (dwfl)
  1157. dwfl_end(dwfl);
  1158. if (ret == 0 && (fname || func))
  1159. ret = 1; /* Found a point */
  1160. return ret;
  1161. }
  1162. /* Add a line and store the src path */
  1163. static int line_range_add_line(const char *src, unsigned int lineno,
  1164. struct line_range *lr)
  1165. {
  1166. /* Copy source path */
  1167. if (!lr->path) {
  1168. lr->path = strdup(src);
  1169. if (lr->path == NULL)
  1170. return -ENOMEM;
  1171. }
  1172. return line_list__add_line(&lr->line_list, lineno);
  1173. }
  1174. static int line_range_walk_cb(const char *fname, int lineno,
  1175. Dwarf_Addr addr __used,
  1176. void *data)
  1177. {
  1178. struct line_finder *lf = data;
  1179. if ((strtailcmp(fname, lf->fname) != 0) ||
  1180. (lf->lno_s > lineno || lf->lno_e < lineno))
  1181. return 0;
  1182. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1183. return -EINVAL;
  1184. return 0;
  1185. }
  1186. /* Find line range from its line number */
  1187. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1188. {
  1189. int ret;
  1190. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1191. /* Update status */
  1192. if (ret >= 0)
  1193. if (!list_empty(&lf->lr->line_list))
  1194. ret = lf->found = 1;
  1195. else
  1196. ret = 0; /* Lines are not found */
  1197. else {
  1198. free(lf->lr->path);
  1199. lf->lr->path = NULL;
  1200. }
  1201. return ret;
  1202. }
  1203. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1204. {
  1205. struct dwarf_callback_param *param = data;
  1206. param->retval = find_line_range_by_line(in_die, param->data);
  1207. return DWARF_CB_ABORT; /* No need to find other instances */
  1208. }
  1209. /* Search function from function name */
  1210. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1211. {
  1212. struct dwarf_callback_param *param = data;
  1213. struct line_finder *lf = param->data;
  1214. struct line_range *lr = lf->lr;
  1215. /* Check declared file */
  1216. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1217. return DWARF_CB_OK;
  1218. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1219. die_compare_name(sp_die, lr->function)) {
  1220. lf->fname = dwarf_decl_file(sp_die);
  1221. dwarf_decl_line(sp_die, &lr->offset);
  1222. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1223. lf->lno_s = lr->offset + lr->start;
  1224. if (lf->lno_s < 0) /* Overflow */
  1225. lf->lno_s = INT_MAX;
  1226. lf->lno_e = lr->offset + lr->end;
  1227. if (lf->lno_e < 0) /* Overflow */
  1228. lf->lno_e = INT_MAX;
  1229. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1230. lr->start = lf->lno_s;
  1231. lr->end = lf->lno_e;
  1232. if (dwarf_func_inline(sp_die)) {
  1233. struct dwarf_callback_param _param;
  1234. _param.data = (void *)lf;
  1235. _param.retval = 0;
  1236. dwarf_func_inline_instances(sp_die,
  1237. line_range_inline_cb,
  1238. &_param);
  1239. param->retval = _param.retval;
  1240. } else
  1241. param->retval = find_line_range_by_line(sp_die, lf);
  1242. return DWARF_CB_ABORT;
  1243. }
  1244. return DWARF_CB_OK;
  1245. }
  1246. static int find_line_range_by_func(struct line_finder *lf)
  1247. {
  1248. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1249. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1250. return param.retval;
  1251. }
  1252. int find_line_range(int fd, struct line_range *lr)
  1253. {
  1254. struct line_finder lf = {.lr = lr, .found = 0};
  1255. int ret = 0;
  1256. Dwarf_Off off = 0, noff;
  1257. size_t cuhl;
  1258. Dwarf_Die *diep;
  1259. Dwarf *dbg = NULL;
  1260. Dwfl *dwfl;
  1261. Dwarf_Addr bias; /* Currently ignored */
  1262. const char *comp_dir;
  1263. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1264. if (!dbg) {
  1265. pr_warning("No debug information found in the vmlinux - "
  1266. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1267. close(fd); /* Without dwfl_end(), fd isn't closed. */
  1268. return -EBADF;
  1269. }
  1270. /* Fastpath: lookup by function name from .debug_pubnames section */
  1271. if (lr->function) {
  1272. struct pubname_callback_param pubname_param = {
  1273. .function = lr->function, .file = lr->file,
  1274. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1275. struct dwarf_callback_param line_range_param = {
  1276. .data = (void *)&lf, .retval = 0};
  1277. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  1278. if (pubname_param.found) {
  1279. line_range_search_cb(&lf.sp_die, &line_range_param);
  1280. if (lf.found)
  1281. goto found;
  1282. }
  1283. }
  1284. /* Loop on CUs (Compilation Unit) */
  1285. while (!lf.found && ret >= 0) {
  1286. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1287. break;
  1288. /* Get the DIE(Debugging Information Entry) of this CU */
  1289. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1290. if (!diep)
  1291. continue;
  1292. /* Check if target file is included. */
  1293. if (lr->file)
  1294. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1295. else
  1296. lf.fname = 0;
  1297. if (!lr->file || lf.fname) {
  1298. if (lr->function)
  1299. ret = find_line_range_by_func(&lf);
  1300. else {
  1301. lf.lno_s = lr->start;
  1302. lf.lno_e = lr->end;
  1303. ret = find_line_range_by_line(NULL, &lf);
  1304. }
  1305. }
  1306. off = noff;
  1307. }
  1308. found:
  1309. /* Store comp_dir */
  1310. if (lf.found) {
  1311. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1312. if (comp_dir) {
  1313. lr->comp_dir = strdup(comp_dir);
  1314. if (!lr->comp_dir)
  1315. ret = -ENOMEM;
  1316. }
  1317. }
  1318. pr_debug("path: %s\n", lr->path);
  1319. dwfl_end(dwfl);
  1320. return (ret < 0) ? ret : lf.found;
  1321. }