probe-finder.c 40 KB

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