probe-finder.c 51 KB

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