probe-finder.c 42 KB

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