probe-finder.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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 subprogram die */
  544. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  545. {
  546. Dwarf_Die vr_die, *scopes;
  547. char buf[32], *ptr;
  548. int ret, nscopes;
  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",
  581. pf->pvar->var);
  582. /* Search child die for local variables and parameters. */
  583. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  584. ret = convert_variable(&vr_die, pf);
  585. else {
  586. /* Search upper class */
  587. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  588. while (nscopes-- > 1) {
  589. pr_debug("Searching variables in %s\n",
  590. dwarf_diename(&scopes[nscopes]));
  591. /* We should check this scope, so give dummy address */
  592. if (die_find_variable_at(&scopes[nscopes],
  593. pf->pvar->var, 0,
  594. &vr_die)) {
  595. ret = convert_variable(&vr_die, pf);
  596. goto found;
  597. }
  598. }
  599. if (scopes)
  600. free(scopes);
  601. ret = -ENOENT;
  602. }
  603. found:
  604. if (ret < 0)
  605. pr_warning("Failed to find '%s' in this function.\n",
  606. pf->pvar->var);
  607. return ret;
  608. }
  609. /* Convert subprogram DIE to trace point */
  610. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  611. bool retprobe, struct probe_trace_point *tp)
  612. {
  613. Dwarf_Addr eaddr;
  614. const char *name;
  615. /* Copy the name of probe point */
  616. name = dwarf_diename(sp_die);
  617. if (name) {
  618. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  619. pr_warning("Failed to get entry address of %s\n",
  620. dwarf_diename(sp_die));
  621. return -ENOENT;
  622. }
  623. tp->symbol = strdup(name);
  624. if (tp->symbol == NULL)
  625. return -ENOMEM;
  626. tp->offset = (unsigned long)(paddr - eaddr);
  627. } else
  628. /* This function has no name. */
  629. tp->offset = (unsigned long)paddr;
  630. /* Return probe must be on the head of a subprogram */
  631. if (retprobe) {
  632. if (eaddr != paddr) {
  633. pr_warning("Return probe must be on the head of"
  634. " a real function.\n");
  635. return -EINVAL;
  636. }
  637. tp->retprobe = true;
  638. }
  639. return 0;
  640. }
  641. /* Call probe_finder callback with real subprogram DIE */
  642. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  643. {
  644. Dwarf_Die die_mem;
  645. Dwarf_Attribute fb_attr;
  646. size_t nops;
  647. int ret;
  648. /* If no real subprogram, find a real one */
  649. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  650. sp_die = die_find_realfunc(&pf->cu_die, pf->addr, &die_mem);
  651. if (!sp_die) {
  652. pr_warning("Failed to find probe point in any "
  653. "functions.\n");
  654. return -ENOENT;
  655. }
  656. }
  657. /* Get the frame base attribute/ops */
  658. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  659. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  660. if (ret <= 0 || nops == 0) {
  661. pf->fb_ops = NULL;
  662. #if _ELFUTILS_PREREQ(0, 142)
  663. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  664. pf->cfi != NULL) {
  665. Dwarf_Frame *frame;
  666. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  667. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  668. pr_warning("Failed to get call frame on 0x%jx\n",
  669. (uintmax_t)pf->addr);
  670. return -ENOENT;
  671. }
  672. #endif
  673. }
  674. /* Call finder's callback handler */
  675. ret = pf->callback(sp_die, pf);
  676. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  677. pf->fb_ops = NULL;
  678. return ret;
  679. }
  680. static int probe_point_line_walker(const char *fname, int lineno,
  681. Dwarf_Addr addr, void *data)
  682. {
  683. struct probe_finder *pf = data;
  684. int ret;
  685. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  686. return 0;
  687. pf->addr = addr;
  688. ret = call_probe_finder(NULL, pf);
  689. /* Continue if no error, because the line will be in inline function */
  690. return ret < 0 ? ret : 0;
  691. }
  692. /* Find probe point from its line number */
  693. static int find_probe_point_by_line(struct probe_finder *pf)
  694. {
  695. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  696. }
  697. /* Find lines which match lazy pattern */
  698. static int find_lazy_match_lines(struct list_head *head,
  699. const char *fname, const char *pat)
  700. {
  701. FILE *fp;
  702. char *line = NULL;
  703. size_t line_len;
  704. ssize_t len;
  705. int count = 0, linenum = 1;
  706. fp = fopen(fname, "r");
  707. if (!fp) {
  708. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  709. return -errno;
  710. }
  711. while ((len = getline(&line, &line_len, fp)) > 0) {
  712. if (line[len - 1] == '\n')
  713. line[len - 1] = '\0';
  714. if (strlazymatch(line, pat)) {
  715. line_list__add_line(head, linenum);
  716. count++;
  717. }
  718. linenum++;
  719. }
  720. if (ferror(fp))
  721. count = -errno;
  722. free(line);
  723. fclose(fp);
  724. if (count == 0)
  725. pr_debug("No matched lines found in %s.\n", fname);
  726. return count;
  727. }
  728. static int probe_point_lazy_walker(const char *fname, int lineno,
  729. Dwarf_Addr addr, void *data)
  730. {
  731. struct probe_finder *pf = data;
  732. int ret;
  733. if (!line_list__has_line(&pf->lcache, lineno) ||
  734. strtailcmp(fname, pf->fname) != 0)
  735. return 0;
  736. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  737. lineno, (unsigned long long)addr);
  738. pf->addr = addr;
  739. ret = call_probe_finder(NULL, pf);
  740. /*
  741. * Continue if no error, because the lazy pattern will match
  742. * to other lines
  743. */
  744. return ret < 0 ? ret : 0;
  745. }
  746. /* Find probe points from lazy pattern */
  747. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  748. {
  749. int ret = 0;
  750. if (list_empty(&pf->lcache)) {
  751. /* Matching lazy line pattern */
  752. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  753. pf->pev->point.lazy_line);
  754. if (ret <= 0)
  755. return ret;
  756. }
  757. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  758. }
  759. /* Callback parameter with return value */
  760. struct dwarf_callback_param {
  761. void *data;
  762. int retval;
  763. };
  764. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  765. {
  766. struct dwarf_callback_param *param = data;
  767. struct probe_finder *pf = param->data;
  768. struct perf_probe_point *pp = &pf->pev->point;
  769. Dwarf_Addr addr;
  770. if (pp->lazy_line)
  771. param->retval = find_probe_point_lazy(in_die, pf);
  772. else {
  773. /* Get probe address */
  774. if (dwarf_entrypc(in_die, &addr) != 0) {
  775. pr_warning("Failed to get entry address of %s.\n",
  776. dwarf_diename(in_die));
  777. param->retval = -ENOENT;
  778. return DWARF_CB_ABORT;
  779. }
  780. pf->addr = addr;
  781. pf->addr += pp->offset;
  782. pr_debug("found inline addr: 0x%jx\n",
  783. (uintmax_t)pf->addr);
  784. param->retval = call_probe_finder(in_die, pf);
  785. if (param->retval < 0)
  786. return DWARF_CB_ABORT;
  787. }
  788. return DWARF_CB_OK;
  789. }
  790. /* Search function from function name */
  791. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  792. {
  793. struct dwarf_callback_param *param = data;
  794. struct probe_finder *pf = param->data;
  795. struct perf_probe_point *pp = &pf->pev->point;
  796. /* Check tag and diename */
  797. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  798. !die_compare_name(sp_die, pp->function))
  799. return DWARF_CB_OK;
  800. /* Check declared file */
  801. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  802. return DWARF_CB_OK;
  803. pf->fname = dwarf_decl_file(sp_die);
  804. if (pp->line) { /* Function relative line */
  805. dwarf_decl_line(sp_die, &pf->lno);
  806. pf->lno += pp->line;
  807. param->retval = find_probe_point_by_line(pf);
  808. } else if (!dwarf_func_inline(sp_die)) {
  809. /* Real function */
  810. if (pp->lazy_line)
  811. param->retval = find_probe_point_lazy(sp_die, pf);
  812. else {
  813. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  814. pr_warning("Failed to get entry address of "
  815. "%s.\n", dwarf_diename(sp_die));
  816. param->retval = -ENOENT;
  817. return DWARF_CB_ABORT;
  818. }
  819. pf->addr += pp->offset;
  820. /* TODO: Check the address in this function */
  821. param->retval = call_probe_finder(sp_die, pf);
  822. }
  823. } else {
  824. struct dwarf_callback_param _param = {.data = (void *)pf,
  825. .retval = 0};
  826. /* Inlined function: search instances */
  827. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  828. &_param);
  829. param->retval = _param.retval;
  830. }
  831. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  832. }
  833. static int find_probe_point_by_func(struct probe_finder *pf)
  834. {
  835. struct dwarf_callback_param _param = {.data = (void *)pf,
  836. .retval = 0};
  837. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  838. return _param.retval;
  839. }
  840. struct pubname_callback_param {
  841. char *function;
  842. char *file;
  843. Dwarf_Die *cu_die;
  844. Dwarf_Die *sp_die;
  845. int found;
  846. };
  847. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  848. {
  849. struct pubname_callback_param *param = data;
  850. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  851. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  852. return DWARF_CB_OK;
  853. if (die_compare_name(param->sp_die, param->function)) {
  854. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  855. return DWARF_CB_OK;
  856. if (param->file &&
  857. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  858. return DWARF_CB_OK;
  859. param->found = 1;
  860. return DWARF_CB_ABORT;
  861. }
  862. }
  863. return DWARF_CB_OK;
  864. }
  865. /* Find probe points from debuginfo */
  866. static int debuginfo__find_probes(struct debuginfo *self,
  867. struct probe_finder *pf)
  868. {
  869. struct perf_probe_point *pp = &pf->pev->point;
  870. Dwarf_Off off, noff;
  871. size_t cuhl;
  872. Dwarf_Die *diep;
  873. int ret = 0;
  874. #if _ELFUTILS_PREREQ(0, 142)
  875. /* Get the call frame information from this dwarf */
  876. pf->cfi = dwarf_getcfi(self->dbg);
  877. #endif
  878. off = 0;
  879. line_list__init(&pf->lcache);
  880. /* Fastpath: lookup by function name from .debug_pubnames section */
  881. if (pp->function) {
  882. struct pubname_callback_param pubname_param = {
  883. .function = pp->function,
  884. .file = pp->file,
  885. .cu_die = &pf->cu_die,
  886. .sp_die = &pf->sp_die,
  887. .found = 0,
  888. };
  889. struct dwarf_callback_param probe_param = {
  890. .data = pf,
  891. };
  892. dwarf_getpubnames(self->dbg, pubname_search_cb,
  893. &pubname_param, 0);
  894. if (pubname_param.found) {
  895. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  896. if (ret)
  897. goto found;
  898. }
  899. }
  900. /* Loop on CUs (Compilation Unit) */
  901. while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  902. /* Get the DIE(Debugging Information Entry) of this CU */
  903. diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die);
  904. if (!diep)
  905. continue;
  906. /* Check if target file is included. */
  907. if (pp->file)
  908. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  909. else
  910. pf->fname = NULL;
  911. if (!pp->file || pf->fname) {
  912. if (pp->function)
  913. ret = find_probe_point_by_func(pf);
  914. else if (pp->lazy_line)
  915. ret = find_probe_point_lazy(NULL, pf);
  916. else {
  917. pf->lno = pp->line;
  918. ret = find_probe_point_by_line(pf);
  919. }
  920. if (ret < 0)
  921. break;
  922. }
  923. off = noff;
  924. }
  925. found:
  926. line_list__free(&pf->lcache);
  927. return ret;
  928. }
  929. /* Add a found probe point into trace event list */
  930. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  931. {
  932. struct trace_event_finder *tf =
  933. container_of(pf, struct trace_event_finder, pf);
  934. struct probe_trace_event *tev;
  935. int ret, i;
  936. /* Check number of tevs */
  937. if (tf->ntevs == tf->max_tevs) {
  938. pr_warning("Too many( > %d) probe point found.\n",
  939. tf->max_tevs);
  940. return -ERANGE;
  941. }
  942. tev = &tf->tevs[tf->ntevs++];
  943. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  944. &tev->point);
  945. if (ret < 0)
  946. return ret;
  947. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  948. tev->point.offset);
  949. /* Find each argument */
  950. tev->nargs = pf->pev->nargs;
  951. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  952. if (tev->args == NULL)
  953. return -ENOMEM;
  954. for (i = 0; i < pf->pev->nargs; i++) {
  955. pf->pvar = &pf->pev->args[i];
  956. pf->tvar = &tev->args[i];
  957. ret = find_variable(sp_die, pf);
  958. if (ret != 0)
  959. return ret;
  960. }
  961. return 0;
  962. }
  963. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  964. int debuginfo__find_trace_events(struct debuginfo *self,
  965. struct perf_probe_event *pev,
  966. struct probe_trace_event **tevs, int max_tevs)
  967. {
  968. struct trace_event_finder tf = {
  969. .pf = {.pev = pev, .callback = add_probe_trace_event},
  970. .max_tevs = max_tevs};
  971. int ret;
  972. /* Allocate result tevs array */
  973. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  974. if (*tevs == NULL)
  975. return -ENOMEM;
  976. tf.tevs = *tevs;
  977. tf.ntevs = 0;
  978. ret = debuginfo__find_probes(self, &tf.pf);
  979. if (ret < 0) {
  980. free(*tevs);
  981. *tevs = NULL;
  982. return ret;
  983. }
  984. return (ret < 0) ? ret : tf.ntevs;
  985. }
  986. #define MAX_VAR_LEN 64
  987. /* Collect available variables in this scope */
  988. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  989. {
  990. struct available_var_finder *af = data;
  991. struct variable_list *vl;
  992. char buf[MAX_VAR_LEN];
  993. int tag, ret;
  994. vl = &af->vls[af->nvls - 1];
  995. tag = dwarf_tag(die_mem);
  996. if (tag == DW_TAG_formal_parameter ||
  997. tag == DW_TAG_variable) {
  998. ret = convert_variable_location(die_mem, af->pf.addr,
  999. af->pf.fb_ops, NULL);
  1000. if (ret == 0) {
  1001. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1002. pr_debug2("Add new var: %s\n", buf);
  1003. if (ret > 0)
  1004. strlist__add(vl->vars, buf);
  1005. }
  1006. }
  1007. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1008. return DIE_FIND_CB_CONTINUE;
  1009. else
  1010. return DIE_FIND_CB_SIBLING;
  1011. }
  1012. /* Add a found vars into available variables list */
  1013. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1014. {
  1015. struct available_var_finder *af =
  1016. container_of(pf, struct available_var_finder, pf);
  1017. struct variable_list *vl;
  1018. Dwarf_Die die_mem, *scopes = NULL;
  1019. int ret, nscopes;
  1020. /* Check number of tevs */
  1021. if (af->nvls == af->max_vls) {
  1022. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1023. return -ERANGE;
  1024. }
  1025. vl = &af->vls[af->nvls++];
  1026. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1027. &vl->point);
  1028. if (ret < 0)
  1029. return ret;
  1030. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1031. vl->point.offset);
  1032. /* Find local variables */
  1033. vl->vars = strlist__new(true, NULL);
  1034. if (vl->vars == NULL)
  1035. return -ENOMEM;
  1036. af->child = true;
  1037. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1038. /* Find external variables */
  1039. if (!af->externs)
  1040. goto out;
  1041. /* Don't need to search child DIE for externs. */
  1042. af->child = false;
  1043. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1044. while (nscopes-- > 1)
  1045. die_find_child(&scopes[nscopes], collect_variables_cb,
  1046. (void *)af, &die_mem);
  1047. if (scopes)
  1048. free(scopes);
  1049. out:
  1050. if (strlist__empty(vl->vars)) {
  1051. strlist__delete(vl->vars);
  1052. vl->vars = NULL;
  1053. }
  1054. return ret;
  1055. }
  1056. /* Find available variables at given probe point */
  1057. int debuginfo__find_available_vars_at(struct debuginfo *self,
  1058. struct perf_probe_event *pev,
  1059. struct variable_list **vls,
  1060. int max_vls, bool externs)
  1061. {
  1062. struct available_var_finder af = {
  1063. .pf = {.pev = pev, .callback = add_available_vars},
  1064. .max_vls = max_vls, .externs = externs};
  1065. int ret;
  1066. /* Allocate result vls array */
  1067. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1068. if (*vls == NULL)
  1069. return -ENOMEM;
  1070. af.vls = *vls;
  1071. af.nvls = 0;
  1072. ret = debuginfo__find_probes(self, &af.pf);
  1073. if (ret < 0) {
  1074. /* Free vlist for error */
  1075. while (af.nvls--) {
  1076. if (af.vls[af.nvls].point.symbol)
  1077. free(af.vls[af.nvls].point.symbol);
  1078. if (af.vls[af.nvls].vars)
  1079. strlist__delete(af.vls[af.nvls].vars);
  1080. }
  1081. free(af.vls);
  1082. *vls = NULL;
  1083. return ret;
  1084. }
  1085. return (ret < 0) ? ret : af.nvls;
  1086. }
  1087. /* Reverse search */
  1088. int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr,
  1089. struct perf_probe_point *ppt)
  1090. {
  1091. Dwarf_Die cudie, spdie, indie;
  1092. Dwarf_Addr _addr, baseaddr;
  1093. const char *fname = NULL, *func = NULL, *tmp;
  1094. int baseline = 0, lineno = 0, ret = 0;
  1095. /* Adjust address with bias */
  1096. addr += self->bias;
  1097. /* Find cu die */
  1098. if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) {
  1099. pr_warning("Failed to find debug information for address %lx\n",
  1100. addr);
  1101. ret = -EINVAL;
  1102. goto end;
  1103. }
  1104. /* Find a corresponding line (filename and lineno) */
  1105. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1106. /* Don't care whether it failed or not */
  1107. /* Find a corresponding function (name, baseline and baseaddr) */
  1108. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1109. /* Get function entry information */
  1110. tmp = dwarf_diename(&spdie);
  1111. if (!tmp ||
  1112. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1113. dwarf_decl_line(&spdie, &baseline) != 0)
  1114. goto post;
  1115. func = tmp;
  1116. if (addr == (unsigned long)baseaddr)
  1117. /* Function entry - Relative line number is 0 */
  1118. lineno = baseline;
  1119. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1120. &indie)) {
  1121. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1122. _addr == addr)
  1123. /*
  1124. * addr is at an inline function entry.
  1125. * In this case, lineno should be the call-site
  1126. * line number.
  1127. */
  1128. lineno = die_get_call_lineno(&indie);
  1129. else {
  1130. /*
  1131. * addr is in an inline function body.
  1132. * Since lineno points one of the lines
  1133. * of the inline function, baseline should
  1134. * be the entry line of the inline function.
  1135. */
  1136. tmp = dwarf_diename(&indie);
  1137. if (tmp &&
  1138. dwarf_decl_line(&spdie, &baseline) == 0)
  1139. func = tmp;
  1140. }
  1141. }
  1142. }
  1143. post:
  1144. /* Make a relative line number or an offset */
  1145. if (lineno)
  1146. ppt->line = lineno - baseline;
  1147. else if (func)
  1148. ppt->offset = addr - (unsigned long)baseaddr;
  1149. /* Duplicate strings */
  1150. if (func) {
  1151. ppt->function = strdup(func);
  1152. if (ppt->function == NULL) {
  1153. ret = -ENOMEM;
  1154. goto end;
  1155. }
  1156. }
  1157. if (fname) {
  1158. ppt->file = strdup(fname);
  1159. if (ppt->file == NULL) {
  1160. if (ppt->function) {
  1161. free(ppt->function);
  1162. ppt->function = NULL;
  1163. }
  1164. ret = -ENOMEM;
  1165. goto end;
  1166. }
  1167. }
  1168. end:
  1169. if (ret == 0 && (fname || func))
  1170. ret = 1; /* Found a point */
  1171. return ret;
  1172. }
  1173. /* Add a line and store the src path */
  1174. static int line_range_add_line(const char *src, unsigned int lineno,
  1175. struct line_range *lr)
  1176. {
  1177. /* Copy source path */
  1178. if (!lr->path) {
  1179. lr->path = strdup(src);
  1180. if (lr->path == NULL)
  1181. return -ENOMEM;
  1182. }
  1183. return line_list__add_line(&lr->line_list, lineno);
  1184. }
  1185. static int line_range_walk_cb(const char *fname, int lineno,
  1186. Dwarf_Addr addr __used,
  1187. void *data)
  1188. {
  1189. struct line_finder *lf = data;
  1190. if ((strtailcmp(fname, lf->fname) != 0) ||
  1191. (lf->lno_s > lineno || lf->lno_e < lineno))
  1192. return 0;
  1193. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1194. return -EINVAL;
  1195. return 0;
  1196. }
  1197. /* Find line range from its line number */
  1198. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1199. {
  1200. int ret;
  1201. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1202. /* Update status */
  1203. if (ret >= 0)
  1204. if (!list_empty(&lf->lr->line_list))
  1205. ret = lf->found = 1;
  1206. else
  1207. ret = 0; /* Lines are not found */
  1208. else {
  1209. free(lf->lr->path);
  1210. lf->lr->path = NULL;
  1211. }
  1212. return ret;
  1213. }
  1214. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1215. {
  1216. struct dwarf_callback_param *param = data;
  1217. param->retval = find_line_range_by_line(in_die, param->data);
  1218. return DWARF_CB_ABORT; /* No need to find other instances */
  1219. }
  1220. /* Search function from function name */
  1221. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1222. {
  1223. struct dwarf_callback_param *param = data;
  1224. struct line_finder *lf = param->data;
  1225. struct line_range *lr = lf->lr;
  1226. /* Check declared file */
  1227. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1228. return DWARF_CB_OK;
  1229. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1230. die_compare_name(sp_die, lr->function)) {
  1231. lf->fname = dwarf_decl_file(sp_die);
  1232. dwarf_decl_line(sp_die, &lr->offset);
  1233. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1234. lf->lno_s = lr->offset + lr->start;
  1235. if (lf->lno_s < 0) /* Overflow */
  1236. lf->lno_s = INT_MAX;
  1237. lf->lno_e = lr->offset + lr->end;
  1238. if (lf->lno_e < 0) /* Overflow */
  1239. lf->lno_e = INT_MAX;
  1240. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1241. lr->start = lf->lno_s;
  1242. lr->end = lf->lno_e;
  1243. if (dwarf_func_inline(sp_die)) {
  1244. struct dwarf_callback_param _param;
  1245. _param.data = (void *)lf;
  1246. _param.retval = 0;
  1247. dwarf_func_inline_instances(sp_die,
  1248. line_range_inline_cb,
  1249. &_param);
  1250. param->retval = _param.retval;
  1251. } else
  1252. param->retval = find_line_range_by_line(sp_die, lf);
  1253. return DWARF_CB_ABORT;
  1254. }
  1255. return DWARF_CB_OK;
  1256. }
  1257. static int find_line_range_by_func(struct line_finder *lf)
  1258. {
  1259. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1260. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1261. return param.retval;
  1262. }
  1263. int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
  1264. {
  1265. struct line_finder lf = {.lr = lr, .found = 0};
  1266. int ret = 0;
  1267. Dwarf_Off off = 0, noff;
  1268. size_t cuhl;
  1269. Dwarf_Die *diep;
  1270. const char *comp_dir;
  1271. /* Fastpath: lookup by function name from .debug_pubnames section */
  1272. if (lr->function) {
  1273. struct pubname_callback_param pubname_param = {
  1274. .function = lr->function, .file = lr->file,
  1275. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1276. struct dwarf_callback_param line_range_param = {
  1277. .data = (void *)&lf, .retval = 0};
  1278. dwarf_getpubnames(self->dbg, pubname_search_cb,
  1279. &pubname_param, 0);
  1280. if (pubname_param.found) {
  1281. line_range_search_cb(&lf.sp_die, &line_range_param);
  1282. if (lf.found)
  1283. goto found;
  1284. }
  1285. }
  1286. /* Loop on CUs (Compilation Unit) */
  1287. while (!lf.found && ret >= 0) {
  1288. if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
  1289. NULL, NULL, NULL) != 0)
  1290. break;
  1291. /* Get the DIE(Debugging Information Entry) of this CU */
  1292. diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
  1293. if (!diep)
  1294. continue;
  1295. /* Check if target file is included. */
  1296. if (lr->file)
  1297. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1298. else
  1299. lf.fname = 0;
  1300. if (!lr->file || lf.fname) {
  1301. if (lr->function)
  1302. ret = find_line_range_by_func(&lf);
  1303. else {
  1304. lf.lno_s = lr->start;
  1305. lf.lno_e = lr->end;
  1306. ret = find_line_range_by_line(NULL, &lf);
  1307. }
  1308. }
  1309. off = noff;
  1310. }
  1311. found:
  1312. /* Store comp_dir */
  1313. if (lf.found) {
  1314. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1315. if (comp_dir) {
  1316. lr->comp_dir = strdup(comp_dir);
  1317. if (!lr->comp_dir)
  1318. ret = -ENOMEM;
  1319. }
  1320. }
  1321. pr_debug("path: %s\n", lr->path);
  1322. return (ret < 0) ? ret : lf.found;
  1323. }