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