probe-finder.c 39 KB

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