probe-finder.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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 scope DIE */
  544. static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
  545. {
  546. Dwarf_Die vr_die;
  547. char buf[32], *ptr;
  548. int ret = 0;
  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", pf->pvar->var);
  581. /* Search child die for local variables and parameters. */
  582. if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
  583. /* Search again in global variables */
  584. if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, 0, &vr_die))
  585. ret = -ENOENT;
  586. }
  587. if (ret >= 0)
  588. ret = convert_variable(&vr_die, pf);
  589. if (ret < 0)
  590. pr_warning("Failed to find '%s' in this function.\n",
  591. pf->pvar->var);
  592. return ret;
  593. }
  594. /* Convert subprogram DIE to trace point */
  595. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  596. bool retprobe, struct probe_trace_point *tp)
  597. {
  598. Dwarf_Addr eaddr;
  599. const char *name;
  600. /* Copy the name of probe point */
  601. name = dwarf_diename(sp_die);
  602. if (name) {
  603. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  604. pr_warning("Failed to get entry address of %s\n",
  605. dwarf_diename(sp_die));
  606. return -ENOENT;
  607. }
  608. tp->symbol = strdup(name);
  609. if (tp->symbol == NULL)
  610. return -ENOMEM;
  611. tp->offset = (unsigned long)(paddr - eaddr);
  612. } else
  613. /* This function has no name. */
  614. tp->offset = (unsigned long)paddr;
  615. /* Return probe must be on the head of a subprogram */
  616. if (retprobe) {
  617. if (eaddr != paddr) {
  618. pr_warning("Return probe must be on the head of"
  619. " a real function.\n");
  620. return -EINVAL;
  621. }
  622. tp->retprobe = true;
  623. }
  624. return 0;
  625. }
  626. /* Call probe_finder callback with scope DIE */
  627. static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
  628. {
  629. Dwarf_Attribute fb_attr;
  630. size_t nops;
  631. int ret;
  632. if (!sc_die) {
  633. pr_err("Caller must pass a scope DIE. Program error.\n");
  634. return -EINVAL;
  635. }
  636. /* If not a real subprogram, find a real one */
  637. if (dwarf_tag(sc_die) != DW_TAG_subprogram) {
  638. if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
  639. pr_warning("Failed to find probe point in any "
  640. "functions.\n");
  641. return -ENOENT;
  642. }
  643. } else
  644. memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
  645. /* Get the frame base attribute/ops from subprogram */
  646. dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
  647. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  648. if (ret <= 0 || nops == 0) {
  649. pf->fb_ops = NULL;
  650. #if _ELFUTILS_PREREQ(0, 142)
  651. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  652. pf->cfi != NULL) {
  653. Dwarf_Frame *frame;
  654. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  655. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  656. pr_warning("Failed to get call frame on 0x%jx\n",
  657. (uintmax_t)pf->addr);
  658. return -ENOENT;
  659. }
  660. #endif
  661. }
  662. /* Call finder's callback handler */
  663. ret = pf->callback(sc_die, pf);
  664. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  665. pf->fb_ops = NULL;
  666. return ret;
  667. }
  668. struct find_scope_param {
  669. const char *function;
  670. const char *file;
  671. int line;
  672. int diff;
  673. Dwarf_Die *die_mem;
  674. bool found;
  675. };
  676. static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
  677. {
  678. struct find_scope_param *fsp = data;
  679. const char *file;
  680. int lno;
  681. /* Skip if declared file name does not match */
  682. if (fsp->file) {
  683. file = dwarf_decl_file(fn_die);
  684. if (!file || strcmp(fsp->file, file) != 0)
  685. return 0;
  686. }
  687. /* If the function name is given, that's what user expects */
  688. if (fsp->function) {
  689. if (die_compare_name(fn_die, fsp->function)) {
  690. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  691. fsp->found = true;
  692. return 1;
  693. }
  694. } else {
  695. /* With the line number, find the nearest declared DIE */
  696. dwarf_decl_line(fn_die, &lno);
  697. if (lno < fsp->line && fsp->diff > fsp->line - lno) {
  698. /* Keep a candidate and continue */
  699. fsp->diff = fsp->line - lno;
  700. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  701. fsp->found = true;
  702. }
  703. }
  704. return 0;
  705. }
  706. /* Find an appropriate scope fits to given conditions */
  707. static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
  708. {
  709. struct find_scope_param fsp = {
  710. .function = pf->pev->point.function,
  711. .file = pf->fname,
  712. .line = pf->lno,
  713. .diff = INT_MAX,
  714. .die_mem = die_mem,
  715. .found = false,
  716. };
  717. cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
  718. return fsp.found ? die_mem : NULL;
  719. }
  720. static int probe_point_line_walker(const char *fname, int lineno,
  721. Dwarf_Addr addr, void *data)
  722. {
  723. struct probe_finder *pf = data;
  724. Dwarf_Die *sc_die, die_mem;
  725. int ret;
  726. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  727. return 0;
  728. pf->addr = addr;
  729. sc_die = find_best_scope(pf, &die_mem);
  730. if (!sc_die) {
  731. pr_warning("Failed to find scope of probe point.\n");
  732. return -ENOENT;
  733. }
  734. ret = call_probe_finder(sc_die, pf);
  735. /* Continue if no error, because the line will be in inline function */
  736. return ret < 0 ? ret : 0;
  737. }
  738. /* Find probe point from its line number */
  739. static int find_probe_point_by_line(struct probe_finder *pf)
  740. {
  741. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  742. }
  743. /* Find lines which match lazy pattern */
  744. static int find_lazy_match_lines(struct list_head *head,
  745. const char *fname, const char *pat)
  746. {
  747. FILE *fp;
  748. char *line = NULL;
  749. size_t line_len;
  750. ssize_t len;
  751. int count = 0, linenum = 1;
  752. fp = fopen(fname, "r");
  753. if (!fp) {
  754. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  755. return -errno;
  756. }
  757. while ((len = getline(&line, &line_len, fp)) > 0) {
  758. if (line[len - 1] == '\n')
  759. line[len - 1] = '\0';
  760. if (strlazymatch(line, pat)) {
  761. line_list__add_line(head, linenum);
  762. count++;
  763. }
  764. linenum++;
  765. }
  766. if (ferror(fp))
  767. count = -errno;
  768. free(line);
  769. fclose(fp);
  770. if (count == 0)
  771. pr_debug("No matched lines found in %s.\n", fname);
  772. return count;
  773. }
  774. static int probe_point_lazy_walker(const char *fname, int lineno,
  775. Dwarf_Addr addr, void *data)
  776. {
  777. struct probe_finder *pf = data;
  778. Dwarf_Die *sc_die, die_mem;
  779. int ret;
  780. if (!line_list__has_line(&pf->lcache, lineno) ||
  781. strtailcmp(fname, pf->fname) != 0)
  782. return 0;
  783. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  784. lineno, (unsigned long long)addr);
  785. pf->addr = addr;
  786. pf->lno = lineno;
  787. sc_die = find_best_scope(pf, &die_mem);
  788. if (!sc_die) {
  789. pr_warning("Failed to find scope of probe point.\n");
  790. return -ENOENT;
  791. }
  792. ret = call_probe_finder(sc_die, pf);
  793. /*
  794. * Continue if no error, because the lazy pattern will match
  795. * to other lines
  796. */
  797. return ret < 0 ? ret : 0;
  798. }
  799. /* Find probe points from lazy pattern */
  800. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  801. {
  802. int ret = 0;
  803. if (list_empty(&pf->lcache)) {
  804. /* Matching lazy line pattern */
  805. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  806. pf->pev->point.lazy_line);
  807. if (ret <= 0)
  808. return ret;
  809. }
  810. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  811. }
  812. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  813. {
  814. struct probe_finder *pf = data;
  815. struct perf_probe_point *pp = &pf->pev->point;
  816. Dwarf_Addr addr;
  817. int ret;
  818. if (pp->lazy_line)
  819. ret = find_probe_point_lazy(in_die, pf);
  820. else {
  821. /* Get probe address */
  822. if (dwarf_entrypc(in_die, &addr) != 0) {
  823. pr_warning("Failed to get entry address of %s.\n",
  824. dwarf_diename(in_die));
  825. return -ENOENT;
  826. }
  827. pf->addr = addr;
  828. pf->addr += pp->offset;
  829. pr_debug("found inline addr: 0x%jx\n",
  830. (uintmax_t)pf->addr);
  831. ret = call_probe_finder(in_die, pf);
  832. }
  833. return ret;
  834. }
  835. /* Callback parameter with return value for libdw */
  836. struct dwarf_callback_param {
  837. void *data;
  838. int retval;
  839. };
  840. /* Search function from function name */
  841. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  842. {
  843. struct dwarf_callback_param *param = data;
  844. struct probe_finder *pf = param->data;
  845. struct perf_probe_point *pp = &pf->pev->point;
  846. /* Check tag and diename */
  847. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  848. !die_compare_name(sp_die, pp->function))
  849. return DWARF_CB_OK;
  850. /* Check declared file */
  851. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  852. return DWARF_CB_OK;
  853. pf->fname = dwarf_decl_file(sp_die);
  854. if (pp->line) { /* Function relative line */
  855. dwarf_decl_line(sp_die, &pf->lno);
  856. pf->lno += pp->line;
  857. param->retval = find_probe_point_by_line(pf);
  858. } else if (!dwarf_func_inline(sp_die)) {
  859. /* Real function */
  860. if (pp->lazy_line)
  861. param->retval = find_probe_point_lazy(sp_die, pf);
  862. else {
  863. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  864. pr_warning("Failed to get entry address of "
  865. "%s.\n", dwarf_diename(sp_die));
  866. param->retval = -ENOENT;
  867. return DWARF_CB_ABORT;
  868. }
  869. pf->addr += pp->offset;
  870. /* TODO: Check the address in this function */
  871. param->retval = call_probe_finder(sp_die, pf);
  872. }
  873. } else
  874. /* Inlined function: search instances */
  875. param->retval = die_walk_instances(sp_die,
  876. probe_point_inline_cb, (void *)pf);
  877. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  878. }
  879. static int find_probe_point_by_func(struct probe_finder *pf)
  880. {
  881. struct dwarf_callback_param _param = {.data = (void *)pf,
  882. .retval = 0};
  883. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  884. return _param.retval;
  885. }
  886. struct pubname_callback_param {
  887. char *function;
  888. char *file;
  889. Dwarf_Die *cu_die;
  890. Dwarf_Die *sp_die;
  891. int found;
  892. };
  893. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  894. {
  895. struct pubname_callback_param *param = data;
  896. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  897. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  898. return DWARF_CB_OK;
  899. if (die_compare_name(param->sp_die, param->function)) {
  900. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  901. return DWARF_CB_OK;
  902. if (param->file &&
  903. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  904. return DWARF_CB_OK;
  905. param->found = 1;
  906. return DWARF_CB_ABORT;
  907. }
  908. }
  909. return DWARF_CB_OK;
  910. }
  911. /* Find probe points from debuginfo */
  912. static int debuginfo__find_probes(struct debuginfo *self,
  913. struct probe_finder *pf)
  914. {
  915. struct perf_probe_point *pp = &pf->pev->point;
  916. Dwarf_Off off, noff;
  917. size_t cuhl;
  918. Dwarf_Die *diep;
  919. int ret = 0;
  920. #if _ELFUTILS_PREREQ(0, 142)
  921. /* Get the call frame information from this dwarf */
  922. pf->cfi = dwarf_getcfi(self->dbg);
  923. #endif
  924. off = 0;
  925. line_list__init(&pf->lcache);
  926. /* Fastpath: lookup by function name from .debug_pubnames section */
  927. if (pp->function) {
  928. struct pubname_callback_param pubname_param = {
  929. .function = pp->function,
  930. .file = pp->file,
  931. .cu_die = &pf->cu_die,
  932. .sp_die = &pf->sp_die,
  933. .found = 0,
  934. };
  935. struct dwarf_callback_param probe_param = {
  936. .data = pf,
  937. };
  938. dwarf_getpubnames(self->dbg, pubname_search_cb,
  939. &pubname_param, 0);
  940. if (pubname_param.found) {
  941. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  942. if (ret)
  943. goto found;
  944. }
  945. }
  946. /* Loop on CUs (Compilation Unit) */
  947. while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  948. /* Get the DIE(Debugging Information Entry) of this CU */
  949. diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die);
  950. if (!diep)
  951. continue;
  952. /* Check if target file is included. */
  953. if (pp->file)
  954. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  955. else
  956. pf->fname = NULL;
  957. if (!pp->file || pf->fname) {
  958. if (pp->function)
  959. ret = find_probe_point_by_func(pf);
  960. else if (pp->lazy_line)
  961. ret = find_probe_point_lazy(NULL, pf);
  962. else {
  963. pf->lno = pp->line;
  964. ret = find_probe_point_by_line(pf);
  965. }
  966. if (ret < 0)
  967. break;
  968. }
  969. off = noff;
  970. }
  971. found:
  972. line_list__free(&pf->lcache);
  973. return ret;
  974. }
  975. /* Add a found probe point into trace event list */
  976. static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
  977. {
  978. struct trace_event_finder *tf =
  979. container_of(pf, struct trace_event_finder, pf);
  980. struct probe_trace_event *tev;
  981. int ret, i;
  982. /* Check number of tevs */
  983. if (tf->ntevs == tf->max_tevs) {
  984. pr_warning("Too many( > %d) probe point found.\n",
  985. tf->max_tevs);
  986. return -ERANGE;
  987. }
  988. tev = &tf->tevs[tf->ntevs++];
  989. /* Trace point should be converted from subprogram DIE */
  990. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  991. pf->pev->point.retprobe, &tev->point);
  992. if (ret < 0)
  993. return ret;
  994. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  995. tev->point.offset);
  996. /* Find each argument */
  997. tev->nargs = pf->pev->nargs;
  998. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  999. if (tev->args == NULL)
  1000. return -ENOMEM;
  1001. for (i = 0; i < pf->pev->nargs; i++) {
  1002. pf->pvar = &pf->pev->args[i];
  1003. pf->tvar = &tev->args[i];
  1004. /* Variable should be found from scope DIE */
  1005. ret = find_variable(sc_die, pf);
  1006. if (ret != 0)
  1007. return ret;
  1008. }
  1009. return 0;
  1010. }
  1011. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1012. int debuginfo__find_trace_events(struct debuginfo *self,
  1013. struct perf_probe_event *pev,
  1014. struct probe_trace_event **tevs, int max_tevs)
  1015. {
  1016. struct trace_event_finder tf = {
  1017. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1018. .max_tevs = max_tevs};
  1019. int ret;
  1020. /* Allocate result tevs array */
  1021. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1022. if (*tevs == NULL)
  1023. return -ENOMEM;
  1024. tf.tevs = *tevs;
  1025. tf.ntevs = 0;
  1026. ret = debuginfo__find_probes(self, &tf.pf);
  1027. if (ret < 0) {
  1028. free(*tevs);
  1029. *tevs = NULL;
  1030. return ret;
  1031. }
  1032. return (ret < 0) ? ret : tf.ntevs;
  1033. }
  1034. #define MAX_VAR_LEN 64
  1035. /* Collect available variables in this scope */
  1036. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1037. {
  1038. struct available_var_finder *af = data;
  1039. struct variable_list *vl;
  1040. char buf[MAX_VAR_LEN];
  1041. int tag, ret;
  1042. vl = &af->vls[af->nvls - 1];
  1043. tag = dwarf_tag(die_mem);
  1044. if (tag == DW_TAG_formal_parameter ||
  1045. tag == DW_TAG_variable) {
  1046. ret = convert_variable_location(die_mem, af->pf.addr,
  1047. af->pf.fb_ops, NULL);
  1048. if (ret == 0) {
  1049. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1050. pr_debug2("Add new var: %s\n", buf);
  1051. if (ret > 0)
  1052. strlist__add(vl->vars, buf);
  1053. }
  1054. }
  1055. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1056. return DIE_FIND_CB_CONTINUE;
  1057. else
  1058. return DIE_FIND_CB_SIBLING;
  1059. }
  1060. /* Add a found vars into available variables list */
  1061. static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
  1062. {
  1063. struct available_var_finder *af =
  1064. container_of(pf, struct available_var_finder, pf);
  1065. struct variable_list *vl;
  1066. Dwarf_Die die_mem;
  1067. int ret;
  1068. /* Check number of tevs */
  1069. if (af->nvls == af->max_vls) {
  1070. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1071. return -ERANGE;
  1072. }
  1073. vl = &af->vls[af->nvls++];
  1074. /* Trace point should be converted from subprogram DIE */
  1075. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  1076. pf->pev->point.retprobe, &vl->point);
  1077. if (ret < 0)
  1078. return ret;
  1079. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1080. vl->point.offset);
  1081. /* Find local variables */
  1082. vl->vars = strlist__new(true, NULL);
  1083. if (vl->vars == NULL)
  1084. return -ENOMEM;
  1085. af->child = true;
  1086. die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
  1087. /* Find external variables */
  1088. if (!af->externs)
  1089. goto out;
  1090. /* Don't need to search child DIE for externs. */
  1091. af->child = false;
  1092. die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
  1093. out:
  1094. if (strlist__empty(vl->vars)) {
  1095. strlist__delete(vl->vars);
  1096. vl->vars = NULL;
  1097. }
  1098. return ret;
  1099. }
  1100. /* Find available variables at given probe point */
  1101. int debuginfo__find_available_vars_at(struct debuginfo *self,
  1102. struct perf_probe_event *pev,
  1103. struct variable_list **vls,
  1104. int max_vls, bool externs)
  1105. {
  1106. struct available_var_finder af = {
  1107. .pf = {.pev = pev, .callback = add_available_vars},
  1108. .max_vls = max_vls, .externs = externs};
  1109. int ret;
  1110. /* Allocate result vls array */
  1111. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1112. if (*vls == NULL)
  1113. return -ENOMEM;
  1114. af.vls = *vls;
  1115. af.nvls = 0;
  1116. ret = debuginfo__find_probes(self, &af.pf);
  1117. if (ret < 0) {
  1118. /* Free vlist for error */
  1119. while (af.nvls--) {
  1120. if (af.vls[af.nvls].point.symbol)
  1121. free(af.vls[af.nvls].point.symbol);
  1122. if (af.vls[af.nvls].vars)
  1123. strlist__delete(af.vls[af.nvls].vars);
  1124. }
  1125. free(af.vls);
  1126. *vls = NULL;
  1127. return ret;
  1128. }
  1129. return (ret < 0) ? ret : af.nvls;
  1130. }
  1131. /* Reverse search */
  1132. int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr,
  1133. struct perf_probe_point *ppt)
  1134. {
  1135. Dwarf_Die cudie, spdie, indie;
  1136. Dwarf_Addr _addr, baseaddr;
  1137. const char *fname = NULL, *func = NULL, *tmp;
  1138. int baseline = 0, lineno = 0, ret = 0;
  1139. /* Adjust address with bias */
  1140. addr += self->bias;
  1141. /* Find cu die */
  1142. if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) {
  1143. pr_warning("Failed to find debug information for address %lx\n",
  1144. addr);
  1145. ret = -EINVAL;
  1146. goto end;
  1147. }
  1148. /* Find a corresponding line (filename and lineno) */
  1149. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1150. /* Don't care whether it failed or not */
  1151. /* Find a corresponding function (name, baseline and baseaddr) */
  1152. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1153. /* Get function entry information */
  1154. tmp = dwarf_diename(&spdie);
  1155. if (!tmp ||
  1156. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1157. dwarf_decl_line(&spdie, &baseline) != 0)
  1158. goto post;
  1159. func = tmp;
  1160. if (addr == (unsigned long)baseaddr)
  1161. /* Function entry - Relative line number is 0 */
  1162. lineno = baseline;
  1163. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1164. &indie)) {
  1165. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1166. _addr == addr)
  1167. /*
  1168. * addr is at an inline function entry.
  1169. * In this case, lineno should be the call-site
  1170. * line number.
  1171. */
  1172. lineno = die_get_call_lineno(&indie);
  1173. else {
  1174. /*
  1175. * addr is in an inline function body.
  1176. * Since lineno points one of the lines
  1177. * of the inline function, baseline should
  1178. * be the entry line of the inline function.
  1179. */
  1180. tmp = dwarf_diename(&indie);
  1181. if (tmp &&
  1182. dwarf_decl_line(&spdie, &baseline) == 0)
  1183. func = tmp;
  1184. }
  1185. }
  1186. }
  1187. post:
  1188. /* Make a relative line number or an offset */
  1189. if (lineno)
  1190. ppt->line = lineno - baseline;
  1191. else if (func)
  1192. ppt->offset = addr - (unsigned long)baseaddr;
  1193. /* Duplicate strings */
  1194. if (func) {
  1195. ppt->function = strdup(func);
  1196. if (ppt->function == NULL) {
  1197. ret = -ENOMEM;
  1198. goto end;
  1199. }
  1200. }
  1201. if (fname) {
  1202. ppt->file = strdup(fname);
  1203. if (ppt->file == NULL) {
  1204. if (ppt->function) {
  1205. free(ppt->function);
  1206. ppt->function = NULL;
  1207. }
  1208. ret = -ENOMEM;
  1209. goto end;
  1210. }
  1211. }
  1212. end:
  1213. if (ret == 0 && (fname || func))
  1214. ret = 1; /* Found a point */
  1215. return ret;
  1216. }
  1217. /* Add a line and store the src path */
  1218. static int line_range_add_line(const char *src, unsigned int lineno,
  1219. struct line_range *lr)
  1220. {
  1221. /* Copy source path */
  1222. if (!lr->path) {
  1223. lr->path = strdup(src);
  1224. if (lr->path == NULL)
  1225. return -ENOMEM;
  1226. }
  1227. return line_list__add_line(&lr->line_list, lineno);
  1228. }
  1229. static int line_range_walk_cb(const char *fname, int lineno,
  1230. Dwarf_Addr addr __used,
  1231. void *data)
  1232. {
  1233. struct line_finder *lf = data;
  1234. if ((strtailcmp(fname, lf->fname) != 0) ||
  1235. (lf->lno_s > lineno || lf->lno_e < lineno))
  1236. return 0;
  1237. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1238. return -EINVAL;
  1239. return 0;
  1240. }
  1241. /* Find line range from its line number */
  1242. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1243. {
  1244. int ret;
  1245. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1246. /* Update status */
  1247. if (ret >= 0)
  1248. if (!list_empty(&lf->lr->line_list))
  1249. ret = lf->found = 1;
  1250. else
  1251. ret = 0; /* Lines are not found */
  1252. else {
  1253. free(lf->lr->path);
  1254. lf->lr->path = NULL;
  1255. }
  1256. return ret;
  1257. }
  1258. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1259. {
  1260. find_line_range_by_line(in_die, data);
  1261. /*
  1262. * We have to check all instances of inlined function, because
  1263. * some execution paths can be optimized out depends on the
  1264. * function argument of instances
  1265. */
  1266. return 0;
  1267. }
  1268. /* Search function from function name */
  1269. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1270. {
  1271. struct dwarf_callback_param *param = data;
  1272. struct line_finder *lf = param->data;
  1273. struct line_range *lr = lf->lr;
  1274. /* Check declared file */
  1275. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1276. return DWARF_CB_OK;
  1277. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1278. die_compare_name(sp_die, lr->function)) {
  1279. lf->fname = dwarf_decl_file(sp_die);
  1280. dwarf_decl_line(sp_die, &lr->offset);
  1281. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1282. lf->lno_s = lr->offset + lr->start;
  1283. if (lf->lno_s < 0) /* Overflow */
  1284. lf->lno_s = INT_MAX;
  1285. lf->lno_e = lr->offset + lr->end;
  1286. if (lf->lno_e < 0) /* Overflow */
  1287. lf->lno_e = INT_MAX;
  1288. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1289. lr->start = lf->lno_s;
  1290. lr->end = lf->lno_e;
  1291. if (dwarf_func_inline(sp_die))
  1292. param->retval = die_walk_instances(sp_die,
  1293. line_range_inline_cb, lf);
  1294. else
  1295. param->retval = find_line_range_by_line(sp_die, lf);
  1296. return DWARF_CB_ABORT;
  1297. }
  1298. return DWARF_CB_OK;
  1299. }
  1300. static int find_line_range_by_func(struct line_finder *lf)
  1301. {
  1302. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1303. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1304. return param.retval;
  1305. }
  1306. int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
  1307. {
  1308. struct line_finder lf = {.lr = lr, .found = 0};
  1309. int ret = 0;
  1310. Dwarf_Off off = 0, noff;
  1311. size_t cuhl;
  1312. Dwarf_Die *diep;
  1313. const char *comp_dir;
  1314. /* Fastpath: lookup by function name from .debug_pubnames section */
  1315. if (lr->function) {
  1316. struct pubname_callback_param pubname_param = {
  1317. .function = lr->function, .file = lr->file,
  1318. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1319. struct dwarf_callback_param line_range_param = {
  1320. .data = (void *)&lf, .retval = 0};
  1321. dwarf_getpubnames(self->dbg, pubname_search_cb,
  1322. &pubname_param, 0);
  1323. if (pubname_param.found) {
  1324. line_range_search_cb(&lf.sp_die, &line_range_param);
  1325. if (lf.found)
  1326. goto found;
  1327. }
  1328. }
  1329. /* Loop on CUs (Compilation Unit) */
  1330. while (!lf.found && ret >= 0) {
  1331. if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
  1332. NULL, NULL, NULL) != 0)
  1333. break;
  1334. /* Get the DIE(Debugging Information Entry) of this CU */
  1335. diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
  1336. if (!diep)
  1337. continue;
  1338. /* Check if target file is included. */
  1339. if (lr->file)
  1340. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1341. else
  1342. lf.fname = 0;
  1343. if (!lr->file || lf.fname) {
  1344. if (lr->function)
  1345. ret = find_line_range_by_func(&lf);
  1346. else {
  1347. lf.lno_s = lr->start;
  1348. lf.lno_e = lr->end;
  1349. ret = find_line_range_by_line(NULL, &lf);
  1350. }
  1351. }
  1352. off = noff;
  1353. }
  1354. found:
  1355. /* Store comp_dir */
  1356. if (lf.found) {
  1357. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1358. if (comp_dir) {
  1359. lr->comp_dir = strdup(comp_dir);
  1360. if (!lr->comp_dir)
  1361. ret = -ENOMEM;
  1362. }
  1363. }
  1364. pr_debug("path: %s\n", lr->path);
  1365. return (ret < 0) ? ret : lf.found;
  1366. }