probe-finder.c 39 KB

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