probe-finder.c 39 KB

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