probe-finder.c 39 KB

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