probe-finder.c 39 KB

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