probe-finder.c 37 KB

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