probe-finder.c 46 KB

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