probe-finder.c 39 KB

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