probe-finder.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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. }