probe-finder.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /*
  2. * probe-finder.c : C expression to kprobe event converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <dwarf-regs.h>
  33. #include <linux/bitops.h>
  34. #include "event.h"
  35. #include "dso.h"
  36. #include "debug.h"
  37. #include "intlist.h"
  38. #include "util.h"
  39. #include "symbol.h"
  40. #include "probe-finder.h"
  41. /* Kprobe tracer basic type is up to u64 */
  42. #define MAX_BASIC_TYPE_BITS 64
  43. /* Dwarf FL wrappers */
  44. static char *debuginfo_path; /* Currently dummy */
  45. static const Dwfl_Callbacks offline_callbacks = {
  46. .find_debuginfo = dwfl_standard_find_debuginfo,
  47. .debuginfo_path = &debuginfo_path,
  48. .section_address = dwfl_offline_section_address,
  49. /* We use this table for core files too. */
  50. .find_elf = dwfl_build_id_find_elf,
  51. };
  52. /* Get a Dwarf from offline image */
  53. static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
  54. const char *path)
  55. {
  56. int fd;
  57. fd = open(path, O_RDONLY);
  58. if (fd < 0)
  59. return fd;
  60. dbg->dwfl = dwfl_begin(&offline_callbacks);
  61. if (!dbg->dwfl)
  62. goto error;
  63. dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
  64. if (!dbg->mod)
  65. goto error;
  66. dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
  67. if (!dbg->dbg)
  68. goto error;
  69. return 0;
  70. error:
  71. if (dbg->dwfl)
  72. dwfl_end(dbg->dwfl);
  73. else
  74. close(fd);
  75. memset(dbg, 0, sizeof(*dbg));
  76. return -ENOENT;
  77. }
  78. static struct debuginfo *__debuginfo__new(const char *path)
  79. {
  80. struct debuginfo *dbg = zalloc(sizeof(*dbg));
  81. if (!dbg)
  82. return NULL;
  83. if (debuginfo__init_offline_dwarf(dbg, path) < 0)
  84. zfree(&dbg);
  85. if (dbg)
  86. pr_debug("Open Debuginfo file: %s\n", path);
  87. return dbg;
  88. }
  89. enum dso_binary_type distro_dwarf_types[] = {
  90. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  91. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  92. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  93. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  94. DSO_BINARY_TYPE__NOT_FOUND,
  95. };
  96. struct debuginfo *debuginfo__new(const char *path)
  97. {
  98. enum dso_binary_type *type;
  99. char buf[PATH_MAX], nil = '\0';
  100. struct dso *dso;
  101. struct debuginfo *dinfo = NULL;
  102. /* Try to open distro debuginfo files */
  103. dso = dso__new(path);
  104. if (!dso)
  105. goto out;
  106. for (type = distro_dwarf_types;
  107. !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND;
  108. type++) {
  109. if (dso__read_binary_type_filename(dso, *type, &nil,
  110. buf, PATH_MAX) < 0)
  111. continue;
  112. dinfo = __debuginfo__new(buf);
  113. }
  114. dso__delete(dso);
  115. out:
  116. /* if failed to open all distro debuginfo, open given binary */
  117. return dinfo ? : __debuginfo__new(path);
  118. }
  119. void debuginfo__delete(struct debuginfo *dbg)
  120. {
  121. if (dbg) {
  122. if (dbg->dwfl)
  123. dwfl_end(dbg->dwfl);
  124. free(dbg);
  125. }
  126. }
  127. /*
  128. * Probe finder related functions
  129. */
  130. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  131. {
  132. struct probe_trace_arg_ref *ref;
  133. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  134. if (ref != NULL)
  135. ref->offset = offs;
  136. return ref;
  137. }
  138. /*
  139. * Convert a location into trace_arg.
  140. * If tvar == NULL, this just checks variable can be converted.
  141. * If fentry == true and vr_die is a parameter, do huristic search
  142. * for the location fuzzed by function entry mcount.
  143. */
  144. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  145. Dwarf_Op *fb_ops, Dwarf_Die *sp_die,
  146. struct probe_trace_arg *tvar)
  147. {
  148. Dwarf_Attribute attr;
  149. Dwarf_Addr tmp = 0;
  150. Dwarf_Op *op;
  151. size_t nops;
  152. unsigned int regn;
  153. Dwarf_Word offs = 0;
  154. bool ref = false;
  155. const char *regs;
  156. int ret;
  157. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  158. goto static_var;
  159. /* TODO: handle more than 1 exprs */
  160. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  161. return -EINVAL; /* Broken DIE ? */
  162. if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) {
  163. ret = dwarf_entrypc(sp_die, &tmp);
  164. if (ret || addr != tmp ||
  165. dwarf_tag(vr_die) != DW_TAG_formal_parameter ||
  166. dwarf_highpc(sp_die, &tmp))
  167. return -ENOENT;
  168. /*
  169. * This is fuzzed by fentry mcount. We try to find the
  170. * parameter location at the earliest address.
  171. */
  172. for (addr += 1; addr <= tmp; addr++) {
  173. if (dwarf_getlocation_addr(&attr, addr, &op,
  174. &nops, 1) > 0)
  175. goto found;
  176. }
  177. return -ENOENT;
  178. }
  179. found:
  180. if (nops == 0)
  181. /* TODO: Support const_value */
  182. return -ENOENT;
  183. if (op->atom == DW_OP_addr) {
  184. static_var:
  185. if (!tvar)
  186. return 0;
  187. /* Static variables on memory (not stack), make @varname */
  188. ret = strlen(dwarf_diename(vr_die));
  189. tvar->value = zalloc(ret + 2);
  190. if (tvar->value == NULL)
  191. return -ENOMEM;
  192. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  193. tvar->ref = alloc_trace_arg_ref((long)offs);
  194. if (tvar->ref == NULL)
  195. return -ENOMEM;
  196. return 0;
  197. }
  198. /* If this is based on frame buffer, set the offset */
  199. if (op->atom == DW_OP_fbreg) {
  200. if (fb_ops == NULL)
  201. return -ENOTSUP;
  202. ref = true;
  203. offs = op->number;
  204. op = &fb_ops[0];
  205. }
  206. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  207. regn = op->atom - DW_OP_breg0;
  208. offs += op->number;
  209. ref = true;
  210. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  211. regn = op->atom - DW_OP_reg0;
  212. } else if (op->atom == DW_OP_bregx) {
  213. regn = op->number;
  214. offs += op->number2;
  215. ref = true;
  216. } else if (op->atom == DW_OP_regx) {
  217. regn = op->number;
  218. } else {
  219. pr_debug("DW_OP %x is not supported.\n", op->atom);
  220. return -ENOTSUP;
  221. }
  222. if (!tvar)
  223. return 0;
  224. regs = get_arch_regstr(regn);
  225. if (!regs) {
  226. /* This should be a bug in DWARF or this tool */
  227. pr_warning("Mapping for the register number %u "
  228. "missing on this architecture.\n", regn);
  229. return -ERANGE;
  230. }
  231. tvar->value = strdup(regs);
  232. if (tvar->value == NULL)
  233. return -ENOMEM;
  234. if (ref) {
  235. tvar->ref = alloc_trace_arg_ref((long)offs);
  236. if (tvar->ref == NULL)
  237. return -ENOMEM;
  238. }
  239. return 0;
  240. }
  241. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  242. static int convert_variable_type(Dwarf_Die *vr_die,
  243. struct probe_trace_arg *tvar,
  244. const char *cast)
  245. {
  246. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  247. Dwarf_Die type;
  248. char buf[16];
  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(-ret));
  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. /* Get an appropriate symbol from symtab */
  551. symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL);
  552. if (!symbol) {
  553. pr_warning("Failed to find symbol at 0x%lx\n",
  554. (unsigned long)paddr);
  555. return -ENOENT;
  556. }
  557. tp->offset = (unsigned long)(paddr - sym.st_value);
  558. tp->address = (unsigned long)paddr;
  559. tp->symbol = strdup(symbol);
  560. if (!tp->symbol)
  561. return -ENOMEM;
  562. /* Return probe must be on the head of a subprogram */
  563. if (retprobe) {
  564. if (eaddr != paddr) {
  565. pr_warning("Return probe must be on the head of"
  566. " a real function.\n");
  567. return -EINVAL;
  568. }
  569. tp->retprobe = true;
  570. }
  571. return 0;
  572. }
  573. /* Call probe_finder callback with scope DIE */
  574. static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
  575. {
  576. Dwarf_Attribute fb_attr;
  577. size_t nops;
  578. int ret;
  579. if (!sc_die) {
  580. pr_err("Caller must pass a scope DIE. Program error.\n");
  581. return -EINVAL;
  582. }
  583. /* If not a real subprogram, find a real one */
  584. if (!die_is_func_def(sc_die)) {
  585. if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
  586. pr_warning("Failed to find probe point in any "
  587. "functions.\n");
  588. return -ENOENT;
  589. }
  590. } else
  591. memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
  592. /* Get the frame base attribute/ops from subprogram */
  593. dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
  594. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  595. if (ret <= 0 || nops == 0) {
  596. pf->fb_ops = NULL;
  597. #if _ELFUTILS_PREREQ(0, 142)
  598. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  599. pf->cfi != NULL) {
  600. Dwarf_Frame *frame;
  601. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  602. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  603. pr_warning("Failed to get call frame on 0x%jx\n",
  604. (uintmax_t)pf->addr);
  605. return -ENOENT;
  606. }
  607. #endif
  608. }
  609. /* Call finder's callback handler */
  610. ret = pf->callback(sc_die, pf);
  611. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  612. pf->fb_ops = NULL;
  613. return ret;
  614. }
  615. struct find_scope_param {
  616. const char *function;
  617. const char *file;
  618. int line;
  619. int diff;
  620. Dwarf_Die *die_mem;
  621. bool found;
  622. };
  623. static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
  624. {
  625. struct find_scope_param *fsp = data;
  626. const char *file;
  627. int lno;
  628. /* Skip if declared file name does not match */
  629. if (fsp->file) {
  630. file = dwarf_decl_file(fn_die);
  631. if (!file || strcmp(fsp->file, file) != 0)
  632. return 0;
  633. }
  634. /* If the function name is given, that's what user expects */
  635. if (fsp->function) {
  636. if (die_compare_name(fn_die, fsp->function)) {
  637. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  638. fsp->found = true;
  639. return 1;
  640. }
  641. } else {
  642. /* With the line number, find the nearest declared DIE */
  643. dwarf_decl_line(fn_die, &lno);
  644. if (lno < fsp->line && fsp->diff > fsp->line - lno) {
  645. /* Keep a candidate and continue */
  646. fsp->diff = fsp->line - lno;
  647. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  648. fsp->found = true;
  649. }
  650. }
  651. return 0;
  652. }
  653. /* Find an appropriate scope fits to given conditions */
  654. static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
  655. {
  656. struct find_scope_param fsp = {
  657. .function = pf->pev->point.function,
  658. .file = pf->fname,
  659. .line = pf->lno,
  660. .diff = INT_MAX,
  661. .die_mem = die_mem,
  662. .found = false,
  663. };
  664. cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
  665. return fsp.found ? die_mem : NULL;
  666. }
  667. static int probe_point_line_walker(const char *fname, int lineno,
  668. Dwarf_Addr addr, void *data)
  669. {
  670. struct probe_finder *pf = data;
  671. Dwarf_Die *sc_die, die_mem;
  672. int ret;
  673. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  674. return 0;
  675. pf->addr = addr;
  676. sc_die = find_best_scope(pf, &die_mem);
  677. if (!sc_die) {
  678. pr_warning("Failed to find scope of probe point.\n");
  679. return -ENOENT;
  680. }
  681. ret = call_probe_finder(sc_die, pf);
  682. /* Continue if no error, because the line will be in inline function */
  683. return ret < 0 ? ret : 0;
  684. }
  685. /* Find probe point from its line number */
  686. static int find_probe_point_by_line(struct probe_finder *pf)
  687. {
  688. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  689. }
  690. /* Find lines which match lazy pattern */
  691. static int find_lazy_match_lines(struct intlist *list,
  692. const char *fname, const char *pat)
  693. {
  694. FILE *fp;
  695. char *line = NULL;
  696. size_t line_len;
  697. ssize_t len;
  698. int count = 0, linenum = 1;
  699. fp = fopen(fname, "r");
  700. if (!fp) {
  701. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  702. return -errno;
  703. }
  704. while ((len = getline(&line, &line_len, fp)) > 0) {
  705. if (line[len - 1] == '\n')
  706. line[len - 1] = '\0';
  707. if (strlazymatch(line, pat)) {
  708. intlist__add(list, linenum);
  709. count++;
  710. }
  711. linenum++;
  712. }
  713. if (ferror(fp))
  714. count = -errno;
  715. free(line);
  716. fclose(fp);
  717. if (count == 0)
  718. pr_debug("No matched lines found in %s.\n", fname);
  719. return count;
  720. }
  721. static int probe_point_lazy_walker(const char *fname, int lineno,
  722. Dwarf_Addr addr, void *data)
  723. {
  724. struct probe_finder *pf = data;
  725. Dwarf_Die *sc_die, die_mem;
  726. int ret;
  727. if (!intlist__has_entry(pf->lcache, lineno) ||
  728. strtailcmp(fname, pf->fname) != 0)
  729. return 0;
  730. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  731. lineno, (unsigned long long)addr);
  732. pf->addr = addr;
  733. pf->lno = lineno;
  734. sc_die = find_best_scope(pf, &die_mem);
  735. if (!sc_die) {
  736. pr_warning("Failed to find scope of probe point.\n");
  737. return -ENOENT;
  738. }
  739. ret = call_probe_finder(sc_die, 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 (intlist__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. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  760. {
  761. struct probe_finder *pf = data;
  762. struct perf_probe_point *pp = &pf->pev->point;
  763. Dwarf_Addr addr;
  764. int ret;
  765. if (pp->lazy_line)
  766. ret = find_probe_point_lazy(in_die, pf);
  767. else {
  768. /* Get probe address */
  769. if (dwarf_entrypc(in_die, &addr) != 0) {
  770. pr_warning("Failed to get entry address of %s.\n",
  771. dwarf_diename(in_die));
  772. return -ENOENT;
  773. }
  774. pf->addr = addr;
  775. pf->addr += pp->offset;
  776. pr_debug("found inline addr: 0x%jx\n",
  777. (uintmax_t)pf->addr);
  778. ret = call_probe_finder(in_die, pf);
  779. }
  780. return ret;
  781. }
  782. /* Callback parameter with return value for libdw */
  783. struct dwarf_callback_param {
  784. void *data;
  785. int retval;
  786. };
  787. /* Search function from function name */
  788. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  789. {
  790. struct dwarf_callback_param *param = data;
  791. struct probe_finder *pf = param->data;
  792. struct perf_probe_point *pp = &pf->pev->point;
  793. /* Check tag and diename */
  794. if (!die_is_func_def(sp_die) ||
  795. !die_compare_name(sp_die, pp->function))
  796. return DWARF_CB_OK;
  797. /* Check declared file */
  798. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  799. return DWARF_CB_OK;
  800. pf->fname = dwarf_decl_file(sp_die);
  801. if (pp->line) { /* Function relative line */
  802. dwarf_decl_line(sp_die, &pf->lno);
  803. pf->lno += pp->line;
  804. param->retval = find_probe_point_by_line(pf);
  805. } else if (!dwarf_func_inline(sp_die)) {
  806. /* Real function */
  807. if (pp->lazy_line)
  808. param->retval = find_probe_point_lazy(sp_die, pf);
  809. else {
  810. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  811. pr_warning("Failed to get entry address of "
  812. "%s.\n", dwarf_diename(sp_die));
  813. param->retval = -ENOENT;
  814. return DWARF_CB_ABORT;
  815. }
  816. pf->addr += pp->offset;
  817. /* TODO: Check the address in this function */
  818. param->retval = call_probe_finder(sp_die, pf);
  819. }
  820. } else
  821. /* Inlined function: search instances */
  822. param->retval = die_walk_instances(sp_die,
  823. probe_point_inline_cb, (void *)pf);
  824. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  825. }
  826. static int find_probe_point_by_func(struct probe_finder *pf)
  827. {
  828. struct dwarf_callback_param _param = {.data = (void *)pf,
  829. .retval = 0};
  830. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  831. return _param.retval;
  832. }
  833. struct pubname_callback_param {
  834. char *function;
  835. char *file;
  836. Dwarf_Die *cu_die;
  837. Dwarf_Die *sp_die;
  838. int found;
  839. };
  840. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  841. {
  842. struct pubname_callback_param *param = data;
  843. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  844. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  845. return DWARF_CB_OK;
  846. if (die_compare_name(param->sp_die, param->function)) {
  847. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  848. return DWARF_CB_OK;
  849. if (param->file &&
  850. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  851. return DWARF_CB_OK;
  852. param->found = 1;
  853. return DWARF_CB_ABORT;
  854. }
  855. }
  856. return DWARF_CB_OK;
  857. }
  858. /* Find probe points from debuginfo */
  859. static int debuginfo__find_probes(struct debuginfo *dbg,
  860. struct probe_finder *pf)
  861. {
  862. struct perf_probe_point *pp = &pf->pev->point;
  863. Dwarf_Off off, noff;
  864. size_t cuhl;
  865. Dwarf_Die *diep;
  866. int ret = 0;
  867. #if _ELFUTILS_PREREQ(0, 142)
  868. /* Get the call frame information from this dwarf */
  869. pf->cfi = dwarf_getcfi_elf(dwarf_getelf(dbg->dbg));
  870. #endif
  871. off = 0;
  872. pf->lcache = intlist__new(NULL);
  873. if (!pf->lcache)
  874. return -ENOMEM;
  875. /* Fastpath: lookup by function name from .debug_pubnames section */
  876. if (pp->function) {
  877. struct pubname_callback_param pubname_param = {
  878. .function = pp->function,
  879. .file = pp->file,
  880. .cu_die = &pf->cu_die,
  881. .sp_die = &pf->sp_die,
  882. .found = 0,
  883. };
  884. struct dwarf_callback_param probe_param = {
  885. .data = pf,
  886. };
  887. dwarf_getpubnames(dbg->dbg, pubname_search_cb,
  888. &pubname_param, 0);
  889. if (pubname_param.found) {
  890. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  891. if (ret)
  892. goto found;
  893. }
  894. }
  895. /* Loop on CUs (Compilation Unit) */
  896. while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  897. /* Get the DIE(Debugging Information Entry) of this CU */
  898. diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
  899. if (!diep)
  900. continue;
  901. /* Check if target file is included. */
  902. if (pp->file)
  903. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  904. else
  905. pf->fname = NULL;
  906. if (!pp->file || pf->fname) {
  907. if (pp->function)
  908. ret = find_probe_point_by_func(pf);
  909. else if (pp->lazy_line)
  910. ret = find_probe_point_lazy(NULL, pf);
  911. else {
  912. pf->lno = pp->line;
  913. ret = find_probe_point_by_line(pf);
  914. }
  915. if (ret < 0)
  916. break;
  917. }
  918. off = noff;
  919. }
  920. found:
  921. intlist__delete(pf->lcache);
  922. pf->lcache = NULL;
  923. return ret;
  924. }
  925. struct local_vars_finder {
  926. struct probe_finder *pf;
  927. struct perf_probe_arg *args;
  928. int max_args;
  929. int nargs;
  930. int ret;
  931. };
  932. /* Collect available variables in this scope */
  933. static int copy_variables_cb(Dwarf_Die *die_mem, void *data)
  934. {
  935. struct local_vars_finder *vf = data;
  936. struct probe_finder *pf = vf->pf;
  937. int tag;
  938. tag = dwarf_tag(die_mem);
  939. if (tag == DW_TAG_formal_parameter ||
  940. tag == DW_TAG_variable) {
  941. if (convert_variable_location(die_mem, vf->pf->addr,
  942. vf->pf->fb_ops, &pf->sp_die,
  943. NULL) == 0) {
  944. vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem);
  945. if (vf->args[vf->nargs].var == NULL) {
  946. vf->ret = -ENOMEM;
  947. return DIE_FIND_CB_END;
  948. }
  949. pr_debug(" %s", vf->args[vf->nargs].var);
  950. vf->nargs++;
  951. }
  952. }
  953. if (dwarf_haspc(die_mem, vf->pf->addr))
  954. return DIE_FIND_CB_CONTINUE;
  955. else
  956. return DIE_FIND_CB_SIBLING;
  957. }
  958. static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf,
  959. struct perf_probe_arg *args)
  960. {
  961. Dwarf_Die die_mem;
  962. int i;
  963. int n = 0;
  964. struct local_vars_finder vf = {.pf = pf, .args = args,
  965. .max_args = MAX_PROBE_ARGS, .ret = 0};
  966. for (i = 0; i < pf->pev->nargs; i++) {
  967. /* var never be NULL */
  968. if (strcmp(pf->pev->args[i].var, "$vars") == 0) {
  969. pr_debug("Expanding $vars into:");
  970. vf.nargs = n;
  971. /* Special local variables */
  972. die_find_child(sc_die, copy_variables_cb, (void *)&vf,
  973. &die_mem);
  974. pr_debug(" (%d)\n", vf.nargs - n);
  975. if (vf.ret < 0)
  976. return vf.ret;
  977. n = vf.nargs;
  978. } else {
  979. /* Copy normal argument */
  980. args[n] = pf->pev->args[i];
  981. n++;
  982. }
  983. }
  984. return n;
  985. }
  986. /* Add a found probe point into trace event list */
  987. static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
  988. {
  989. struct trace_event_finder *tf =
  990. container_of(pf, struct trace_event_finder, pf);
  991. struct probe_trace_event *tev;
  992. struct perf_probe_arg *args;
  993. int ret, i;
  994. /* Check number of tevs */
  995. if (tf->ntevs == tf->max_tevs) {
  996. pr_warning("Too many( > %d) probe point found.\n",
  997. tf->max_tevs);
  998. return -ERANGE;
  999. }
  1000. tev = &tf->tevs[tf->ntevs++];
  1001. /* Trace point should be converted from subprogram DIE */
  1002. ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr,
  1003. pf->pev->point.retprobe, &tev->point);
  1004. if (ret < 0)
  1005. return ret;
  1006. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1007. tev->point.offset);
  1008. /* Expand special probe argument if exist */
  1009. args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS);
  1010. if (args == NULL)
  1011. return -ENOMEM;
  1012. ret = expand_probe_args(sc_die, pf, args);
  1013. if (ret < 0)
  1014. goto end;
  1015. tev->nargs = ret;
  1016. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1017. if (tev->args == NULL) {
  1018. ret = -ENOMEM;
  1019. goto end;
  1020. }
  1021. /* Find each argument */
  1022. for (i = 0; i < tev->nargs; i++) {
  1023. pf->pvar = &args[i];
  1024. pf->tvar = &tev->args[i];
  1025. /* Variable should be found from scope DIE */
  1026. ret = find_variable(sc_die, pf);
  1027. if (ret != 0)
  1028. break;
  1029. }
  1030. end:
  1031. free(args);
  1032. return ret;
  1033. }
  1034. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1035. int debuginfo__find_trace_events(struct debuginfo *dbg,
  1036. struct perf_probe_event *pev,
  1037. struct probe_trace_event **tevs, int max_tevs)
  1038. {
  1039. struct trace_event_finder tf = {
  1040. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1041. .mod = dbg->mod, .max_tevs = max_tevs};
  1042. int ret;
  1043. /* Allocate result tevs array */
  1044. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1045. if (*tevs == NULL)
  1046. return -ENOMEM;
  1047. tf.tevs = *tevs;
  1048. tf.ntevs = 0;
  1049. ret = debuginfo__find_probes(dbg, &tf.pf);
  1050. if (ret < 0) {
  1051. zfree(tevs);
  1052. return ret;
  1053. }
  1054. return (ret < 0) ? ret : tf.ntevs;
  1055. }
  1056. #define MAX_VAR_LEN 64
  1057. /* Collect available variables in this scope */
  1058. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1059. {
  1060. struct available_var_finder *af = data;
  1061. struct variable_list *vl;
  1062. char buf[MAX_VAR_LEN];
  1063. int tag, ret;
  1064. vl = &af->vls[af->nvls - 1];
  1065. tag = dwarf_tag(die_mem);
  1066. if (tag == DW_TAG_formal_parameter ||
  1067. tag == DW_TAG_variable) {
  1068. ret = convert_variable_location(die_mem, af->pf.addr,
  1069. af->pf.fb_ops, &af->pf.sp_die,
  1070. NULL);
  1071. if (ret == 0) {
  1072. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1073. pr_debug2("Add new var: %s\n", buf);
  1074. if (ret > 0)
  1075. strlist__add(vl->vars, buf);
  1076. }
  1077. }
  1078. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1079. return DIE_FIND_CB_CONTINUE;
  1080. else
  1081. return DIE_FIND_CB_SIBLING;
  1082. }
  1083. /* Add a found vars into available variables list */
  1084. static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
  1085. {
  1086. struct available_var_finder *af =
  1087. container_of(pf, struct available_var_finder, pf);
  1088. struct variable_list *vl;
  1089. Dwarf_Die die_mem;
  1090. int ret;
  1091. /* Check number of tevs */
  1092. if (af->nvls == af->max_vls) {
  1093. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1094. return -ERANGE;
  1095. }
  1096. vl = &af->vls[af->nvls++];
  1097. /* Trace point should be converted from subprogram DIE */
  1098. ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr,
  1099. pf->pev->point.retprobe, &vl->point);
  1100. if (ret < 0)
  1101. return ret;
  1102. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1103. vl->point.offset);
  1104. /* Find local variables */
  1105. vl->vars = strlist__new(true, NULL);
  1106. if (vl->vars == NULL)
  1107. return -ENOMEM;
  1108. af->child = true;
  1109. die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
  1110. /* Find external variables */
  1111. if (!af->externs)
  1112. goto out;
  1113. /* Don't need to search child DIE for externs. */
  1114. af->child = false;
  1115. die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
  1116. out:
  1117. if (strlist__empty(vl->vars)) {
  1118. strlist__delete(vl->vars);
  1119. vl->vars = NULL;
  1120. }
  1121. return ret;
  1122. }
  1123. /*
  1124. * Find available variables at given probe point
  1125. * Return the number of found probe points. Return 0 if there is no
  1126. * matched probe point. Return <0 if an error occurs.
  1127. */
  1128. int debuginfo__find_available_vars_at(struct debuginfo *dbg,
  1129. struct perf_probe_event *pev,
  1130. struct variable_list **vls,
  1131. int max_vls, bool externs)
  1132. {
  1133. struct available_var_finder af = {
  1134. .pf = {.pev = pev, .callback = add_available_vars},
  1135. .mod = dbg->mod,
  1136. .max_vls = max_vls, .externs = externs};
  1137. int ret;
  1138. /* Allocate result vls array */
  1139. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1140. if (*vls == NULL)
  1141. return -ENOMEM;
  1142. af.vls = *vls;
  1143. af.nvls = 0;
  1144. ret = debuginfo__find_probes(dbg, &af.pf);
  1145. if (ret < 0) {
  1146. /* Free vlist for error */
  1147. while (af.nvls--) {
  1148. zfree(&af.vls[af.nvls].point.symbol);
  1149. strlist__delete(af.vls[af.nvls].vars);
  1150. }
  1151. zfree(vls);
  1152. return ret;
  1153. }
  1154. return (ret < 0) ? ret : af.nvls;
  1155. }
  1156. /* Reverse search */
  1157. int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
  1158. struct perf_probe_point *ppt)
  1159. {
  1160. Dwarf_Die cudie, spdie, indie;
  1161. Dwarf_Addr _addr = 0, baseaddr = 0;
  1162. const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
  1163. int baseline = 0, lineno = 0, ret = 0;
  1164. /* Adjust address with bias */
  1165. addr += dbg->bias;
  1166. /* Find cu die */
  1167. if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr - dbg->bias, &cudie)) {
  1168. pr_warning("Failed to find debug information for address %lx\n",
  1169. addr);
  1170. ret = -EINVAL;
  1171. goto end;
  1172. }
  1173. /* Find a corresponding line (filename and lineno) */
  1174. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1175. /* Don't care whether it failed or not */
  1176. /* Find a corresponding function (name, baseline and baseaddr) */
  1177. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1178. /* Get function entry information */
  1179. func = basefunc = dwarf_diename(&spdie);
  1180. if (!func ||
  1181. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1182. dwarf_decl_line(&spdie, &baseline) != 0) {
  1183. lineno = 0;
  1184. goto post;
  1185. }
  1186. fname = dwarf_decl_file(&spdie);
  1187. if (addr == (unsigned long)baseaddr) {
  1188. /* Function entry - Relative line number is 0 */
  1189. lineno = baseline;
  1190. goto post;
  1191. }
  1192. /* Track down the inline functions step by step */
  1193. while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1194. &indie)) {
  1195. /* There is an inline function */
  1196. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1197. _addr == addr) {
  1198. /*
  1199. * addr is at an inline function entry.
  1200. * In this case, lineno should be the call-site
  1201. * line number. (overwrite lineinfo)
  1202. */
  1203. lineno = die_get_call_lineno(&indie);
  1204. fname = die_get_call_file(&indie);
  1205. break;
  1206. } else {
  1207. /*
  1208. * addr is in an inline function body.
  1209. * Since lineno points one of the lines
  1210. * of the inline function, baseline should
  1211. * be the entry line of the inline function.
  1212. */
  1213. tmp = dwarf_diename(&indie);
  1214. if (!tmp ||
  1215. dwarf_decl_line(&indie, &baseline) != 0)
  1216. break;
  1217. func = tmp;
  1218. spdie = indie;
  1219. }
  1220. }
  1221. /* Verify the lineno and baseline are in a same file */
  1222. tmp = dwarf_decl_file(&spdie);
  1223. if (!tmp || strcmp(tmp, fname) != 0)
  1224. lineno = 0;
  1225. }
  1226. post:
  1227. /* Make a relative line number or an offset */
  1228. if (lineno)
  1229. ppt->line = lineno - baseline;
  1230. else if (basefunc) {
  1231. ppt->offset = addr - (unsigned long)baseaddr;
  1232. func = basefunc;
  1233. }
  1234. /* Duplicate strings */
  1235. if (func) {
  1236. ppt->function = strdup(func);
  1237. if (ppt->function == NULL) {
  1238. ret = -ENOMEM;
  1239. goto end;
  1240. }
  1241. }
  1242. if (fname) {
  1243. ppt->file = strdup(fname);
  1244. if (ppt->file == NULL) {
  1245. zfree(&ppt->function);
  1246. ret = -ENOMEM;
  1247. goto end;
  1248. }
  1249. }
  1250. end:
  1251. if (ret == 0 && (fname || func))
  1252. ret = 1; /* Found a point */
  1253. return ret;
  1254. }
  1255. /* Add a line and store the src path */
  1256. static int line_range_add_line(const char *src, unsigned int lineno,
  1257. struct line_range *lr)
  1258. {
  1259. /* Copy source path */
  1260. if (!lr->path) {
  1261. lr->path = strdup(src);
  1262. if (lr->path == NULL)
  1263. return -ENOMEM;
  1264. }
  1265. return intlist__add(lr->line_list, lineno);
  1266. }
  1267. static int line_range_walk_cb(const char *fname, int lineno,
  1268. Dwarf_Addr addr __maybe_unused,
  1269. void *data)
  1270. {
  1271. struct line_finder *lf = data;
  1272. int err;
  1273. if ((strtailcmp(fname, lf->fname) != 0) ||
  1274. (lf->lno_s > lineno || lf->lno_e < lineno))
  1275. return 0;
  1276. err = line_range_add_line(fname, lineno, lf->lr);
  1277. if (err < 0 && err != -EEXIST)
  1278. return err;
  1279. return 0;
  1280. }
  1281. /* Find line range from its line number */
  1282. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1283. {
  1284. int ret;
  1285. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1286. /* Update status */
  1287. if (ret >= 0)
  1288. if (!intlist__empty(lf->lr->line_list))
  1289. ret = lf->found = 1;
  1290. else
  1291. ret = 0; /* Lines are not found */
  1292. else {
  1293. zfree(&lf->lr->path);
  1294. }
  1295. return ret;
  1296. }
  1297. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1298. {
  1299. int ret = find_line_range_by_line(in_die, data);
  1300. /*
  1301. * We have to check all instances of inlined function, because
  1302. * some execution paths can be optimized out depends on the
  1303. * function argument of instances. However, if an error occurs,
  1304. * it should be handled by the caller.
  1305. */
  1306. return ret < 0 ? ret : 0;
  1307. }
  1308. /* Search function definition from function name */
  1309. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1310. {
  1311. struct dwarf_callback_param *param = data;
  1312. struct line_finder *lf = param->data;
  1313. struct line_range *lr = lf->lr;
  1314. /* Check declared file */
  1315. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1316. return DWARF_CB_OK;
  1317. if (die_is_func_def(sp_die) &&
  1318. die_compare_name(sp_die, lr->function)) {
  1319. lf->fname = dwarf_decl_file(sp_die);
  1320. dwarf_decl_line(sp_die, &lr->offset);
  1321. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1322. lf->lno_s = lr->offset + lr->start;
  1323. if (lf->lno_s < 0) /* Overflow */
  1324. lf->lno_s = INT_MAX;
  1325. lf->lno_e = lr->offset + lr->end;
  1326. if (lf->lno_e < 0) /* Overflow */
  1327. lf->lno_e = INT_MAX;
  1328. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1329. lr->start = lf->lno_s;
  1330. lr->end = lf->lno_e;
  1331. if (dwarf_func_inline(sp_die))
  1332. param->retval = die_walk_instances(sp_die,
  1333. line_range_inline_cb, lf);
  1334. else
  1335. param->retval = find_line_range_by_line(sp_die, lf);
  1336. return DWARF_CB_ABORT;
  1337. }
  1338. return DWARF_CB_OK;
  1339. }
  1340. static int find_line_range_by_func(struct line_finder *lf)
  1341. {
  1342. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1343. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1344. return param.retval;
  1345. }
  1346. int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
  1347. {
  1348. struct line_finder lf = {.lr = lr, .found = 0};
  1349. int ret = 0;
  1350. Dwarf_Off off = 0, noff;
  1351. size_t cuhl;
  1352. Dwarf_Die *diep;
  1353. const char *comp_dir;
  1354. /* Fastpath: lookup by function name from .debug_pubnames section */
  1355. if (lr->function) {
  1356. struct pubname_callback_param pubname_param = {
  1357. .function = lr->function, .file = lr->file,
  1358. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1359. struct dwarf_callback_param line_range_param = {
  1360. .data = (void *)&lf, .retval = 0};
  1361. dwarf_getpubnames(dbg->dbg, pubname_search_cb,
  1362. &pubname_param, 0);
  1363. if (pubname_param.found) {
  1364. line_range_search_cb(&lf.sp_die, &line_range_param);
  1365. if (lf.found)
  1366. goto found;
  1367. }
  1368. }
  1369. /* Loop on CUs (Compilation Unit) */
  1370. while (!lf.found && ret >= 0) {
  1371. if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
  1372. NULL, NULL, NULL) != 0)
  1373. break;
  1374. /* Get the DIE(Debugging Information Entry) of this CU */
  1375. diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die);
  1376. if (!diep)
  1377. continue;
  1378. /* Check if target file is included. */
  1379. if (lr->file)
  1380. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1381. else
  1382. lf.fname = 0;
  1383. if (!lr->file || lf.fname) {
  1384. if (lr->function)
  1385. ret = find_line_range_by_func(&lf);
  1386. else {
  1387. lf.lno_s = lr->start;
  1388. lf.lno_e = lr->end;
  1389. ret = find_line_range_by_line(NULL, &lf);
  1390. }
  1391. }
  1392. off = noff;
  1393. }
  1394. found:
  1395. /* Store comp_dir */
  1396. if (lf.found) {
  1397. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1398. if (comp_dir) {
  1399. lr->comp_dir = strdup(comp_dir);
  1400. if (!lr->comp_dir)
  1401. ret = -ENOMEM;
  1402. }
  1403. }
  1404. pr_debug("path: %s\n", lr->path);
  1405. return (ret < 0) ? ret : lf.found;
  1406. }