probe-finder.c 42 KB

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