probe-finder.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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, baseaddr;
  1158. const char *fname = NULL, *func = 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. tmp = dwarf_diename(&spdie);
  1176. if (!tmp ||
  1177. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1178. dwarf_decl_line(&spdie, &baseline) != 0)
  1179. goto post;
  1180. func = tmp;
  1181. if (addr == (unsigned long)baseaddr)
  1182. /* Function entry - Relative line number is 0 */
  1183. lineno = baseline;
  1184. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1185. &indie)) {
  1186. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1187. _addr == addr)
  1188. /*
  1189. * addr is at an inline function entry.
  1190. * In this case, lineno should be the call-site
  1191. * line number.
  1192. */
  1193. lineno = die_get_call_lineno(&indie);
  1194. else {
  1195. /*
  1196. * addr is in an inline function body.
  1197. * Since lineno points one of the lines
  1198. * of the inline function, baseline should
  1199. * be the entry line of the inline function.
  1200. */
  1201. tmp = dwarf_diename(&indie);
  1202. if (tmp &&
  1203. dwarf_decl_line(&spdie, &baseline) == 0)
  1204. func = tmp;
  1205. }
  1206. }
  1207. }
  1208. post:
  1209. /* Make a relative line number or an offset */
  1210. if (lineno)
  1211. ppt->line = lineno - baseline;
  1212. else if (func)
  1213. ppt->offset = addr - (unsigned long)baseaddr;
  1214. /* Duplicate strings */
  1215. if (func) {
  1216. ppt->function = strdup(func);
  1217. if (ppt->function == NULL) {
  1218. ret = -ENOMEM;
  1219. goto end;
  1220. }
  1221. }
  1222. if (fname) {
  1223. ppt->file = strdup(fname);
  1224. if (ppt->file == NULL) {
  1225. if (ppt->function) {
  1226. free(ppt->function);
  1227. ppt->function = NULL;
  1228. }
  1229. ret = -ENOMEM;
  1230. goto end;
  1231. }
  1232. }
  1233. end:
  1234. if (ret == 0 && (fname || func))
  1235. ret = 1; /* Found a point */
  1236. return ret;
  1237. }
  1238. /* Add a line and store the src path */
  1239. static int line_range_add_line(const char *src, unsigned int lineno,
  1240. struct line_range *lr)
  1241. {
  1242. /* Copy source path */
  1243. if (!lr->path) {
  1244. lr->path = strdup(src);
  1245. if (lr->path == NULL)
  1246. return -ENOMEM;
  1247. }
  1248. return line_list__add_line(&lr->line_list, lineno);
  1249. }
  1250. static int line_range_walk_cb(const char *fname, int lineno,
  1251. Dwarf_Addr addr __maybe_unused,
  1252. void *data)
  1253. {
  1254. struct line_finder *lf = data;
  1255. if ((strtailcmp(fname, lf->fname) != 0) ||
  1256. (lf->lno_s > lineno || lf->lno_e < lineno))
  1257. return 0;
  1258. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1259. return -EINVAL;
  1260. return 0;
  1261. }
  1262. /* Find line range from its line number */
  1263. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1264. {
  1265. int ret;
  1266. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1267. /* Update status */
  1268. if (ret >= 0)
  1269. if (!list_empty(&lf->lr->line_list))
  1270. ret = lf->found = 1;
  1271. else
  1272. ret = 0; /* Lines are not found */
  1273. else {
  1274. free(lf->lr->path);
  1275. lf->lr->path = NULL;
  1276. }
  1277. return ret;
  1278. }
  1279. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1280. {
  1281. find_line_range_by_line(in_die, data);
  1282. /*
  1283. * We have to check all instances of inlined function, because
  1284. * some execution paths can be optimized out depends on the
  1285. * function argument of instances
  1286. */
  1287. return 0;
  1288. }
  1289. /* Search function definition from function name */
  1290. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1291. {
  1292. struct dwarf_callback_param *param = data;
  1293. struct line_finder *lf = param->data;
  1294. struct line_range *lr = lf->lr;
  1295. /* Check declared file */
  1296. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1297. return DWARF_CB_OK;
  1298. if (die_is_func_def(sp_die) &&
  1299. die_compare_name(sp_die, lr->function)) {
  1300. lf->fname = dwarf_decl_file(sp_die);
  1301. dwarf_decl_line(sp_die, &lr->offset);
  1302. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1303. lf->lno_s = lr->offset + lr->start;
  1304. if (lf->lno_s < 0) /* Overflow */
  1305. lf->lno_s = INT_MAX;
  1306. lf->lno_e = lr->offset + lr->end;
  1307. if (lf->lno_e < 0) /* Overflow */
  1308. lf->lno_e = INT_MAX;
  1309. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1310. lr->start = lf->lno_s;
  1311. lr->end = lf->lno_e;
  1312. if (dwarf_func_inline(sp_die))
  1313. param->retval = die_walk_instances(sp_die,
  1314. line_range_inline_cb, lf);
  1315. else
  1316. param->retval = find_line_range_by_line(sp_die, lf);
  1317. return DWARF_CB_ABORT;
  1318. }
  1319. return DWARF_CB_OK;
  1320. }
  1321. static int find_line_range_by_func(struct line_finder *lf)
  1322. {
  1323. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1324. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1325. return param.retval;
  1326. }
  1327. int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
  1328. {
  1329. struct line_finder lf = {.lr = lr, .found = 0};
  1330. int ret = 0;
  1331. Dwarf_Off off = 0, noff;
  1332. size_t cuhl;
  1333. Dwarf_Die *diep;
  1334. const char *comp_dir;
  1335. /* Fastpath: lookup by function name from .debug_pubnames section */
  1336. if (lr->function) {
  1337. struct pubname_callback_param pubname_param = {
  1338. .function = lr->function, .file = lr->file,
  1339. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1340. struct dwarf_callback_param line_range_param = {
  1341. .data = (void *)&lf, .retval = 0};
  1342. dwarf_getpubnames(self->dbg, pubname_search_cb,
  1343. &pubname_param, 0);
  1344. if (pubname_param.found) {
  1345. line_range_search_cb(&lf.sp_die, &line_range_param);
  1346. if (lf.found)
  1347. goto found;
  1348. }
  1349. }
  1350. /* Loop on CUs (Compilation Unit) */
  1351. while (!lf.found && ret >= 0) {
  1352. if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
  1353. NULL, NULL, NULL) != 0)
  1354. break;
  1355. /* Get the DIE(Debugging Information Entry) of this CU */
  1356. diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
  1357. if (!diep)
  1358. continue;
  1359. /* Check if target file is included. */
  1360. if (lr->file)
  1361. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1362. else
  1363. lf.fname = 0;
  1364. if (!lr->file || lf.fname) {
  1365. if (lr->function)
  1366. ret = find_line_range_by_func(&lf);
  1367. else {
  1368. lf.lno_s = lr->start;
  1369. lf.lno_e = lr->end;
  1370. ret = find_line_range_by_line(NULL, &lf);
  1371. }
  1372. }
  1373. off = noff;
  1374. }
  1375. found:
  1376. /* Store comp_dir */
  1377. if (lf.found) {
  1378. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1379. if (comp_dir) {
  1380. lr->comp_dir = strdup(comp_dir);
  1381. if (!lr->comp_dir)
  1382. ret = -ENOMEM;
  1383. }
  1384. }
  1385. pr_debug("path: %s\n", lr->path);
  1386. return (ret < 0) ? ret : lf.found;
  1387. }