probe-finder.c 42 KB

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