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