probe-finder.c 47 KB

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