probe-finder.c 45 KB

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