xref: /openbmc/linux/tools/perf/util/dso.c (revision c6fddb28)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <bpf/libbpf.h>
15 #include "bpf-event.h"
16 #include "compress.h"
17 #include "env.h"
18 #include "namespaces.h"
19 #include "path.h"
20 #include "map.h"
21 #include "symbol.h"
22 #include "srcline.h"
23 #include "dso.h"
24 #include "dsos.h"
25 #include "machine.h"
26 #include "auxtrace.h"
27 #include "util.h" /* O_CLOEXEC for older systems */
28 #include "debug.h"
29 #include "string2.h"
30 #include "vdso.h"
31 
32 static const char * const debuglink_paths[] = {
33 	"%.0s%s",
34 	"%s/%s",
35 	"%s/.debug/%s",
36 	"/usr/lib/debug%s/%s"
37 };
38 
39 char dso__symtab_origin(const struct dso *dso)
40 {
41 	static const char origin[] = {
42 		[DSO_BINARY_TYPE__KALLSYMS]			= 'k',
43 		[DSO_BINARY_TYPE__VMLINUX]			= 'v',
44 		[DSO_BINARY_TYPE__JAVA_JIT]			= 'j',
45 		[DSO_BINARY_TYPE__DEBUGLINK]			= 'l',
46 		[DSO_BINARY_TYPE__BUILD_ID_CACHE]		= 'B',
47 		[DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO]	= 'D',
48 		[DSO_BINARY_TYPE__FEDORA_DEBUGINFO]		= 'f',
49 		[DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]		= 'u',
50 		[DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]	= 'o',
51 		[DSO_BINARY_TYPE__BUILDID_DEBUGINFO]		= 'b',
52 		[DSO_BINARY_TYPE__SYSTEM_PATH_DSO]		= 'd',
53 		[DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]		= 'K',
54 		[DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP]	= 'm',
55 		[DSO_BINARY_TYPE__GUEST_KALLSYMS]		= 'g',
56 		[DSO_BINARY_TYPE__GUEST_KMODULE]		= 'G',
57 		[DSO_BINARY_TYPE__GUEST_KMODULE_COMP]		= 'M',
58 		[DSO_BINARY_TYPE__GUEST_VMLINUX]		= 'V',
59 	};
60 
61 	if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
62 		return '!';
63 	return origin[dso->symtab_type];
64 }
65 
66 int dso__read_binary_type_filename(const struct dso *dso,
67 				   enum dso_binary_type type,
68 				   char *root_dir, char *filename, size_t size)
69 {
70 	char build_id_hex[SBUILD_ID_SIZE];
71 	int ret = 0;
72 	size_t len;
73 
74 	switch (type) {
75 	case DSO_BINARY_TYPE__DEBUGLINK:
76 	{
77 		const char *last_slash;
78 		char dso_dir[PATH_MAX];
79 		char symfile[PATH_MAX];
80 		unsigned int i;
81 
82 		len = __symbol__join_symfs(filename, size, dso->long_name);
83 		last_slash = filename + len;
84 		while (last_slash != filename && *last_slash != '/')
85 			last_slash--;
86 
87 		strncpy(dso_dir, filename, last_slash - filename);
88 		dso_dir[last_slash-filename] = '\0';
89 
90 		if (!is_regular_file(filename)) {
91 			ret = -1;
92 			break;
93 		}
94 
95 		ret = filename__read_debuglink(filename, symfile, PATH_MAX);
96 		if (ret)
97 			break;
98 
99 		/* Check predefined locations where debug file might reside */
100 		ret = -1;
101 		for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
102 			snprintf(filename, size,
103 					debuglink_paths[i], dso_dir, symfile);
104 			if (is_regular_file(filename)) {
105 				ret = 0;
106 				break;
107 			}
108 		}
109 
110 		break;
111 	}
112 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
113 		if (dso__build_id_filename(dso, filename, size, false) == NULL)
114 			ret = -1;
115 		break;
116 
117 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
118 		if (dso__build_id_filename(dso, filename, size, true) == NULL)
119 			ret = -1;
120 		break;
121 
122 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
123 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
124 		snprintf(filename + len, size - len, "%s.debug", dso->long_name);
125 		break;
126 
127 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
128 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
129 		snprintf(filename + len, size - len, "%s", dso->long_name);
130 		break;
131 
132 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
133 	{
134 		const char *last_slash;
135 		size_t dir_size;
136 
137 		last_slash = dso->long_name + dso->long_name_len;
138 		while (last_slash != dso->long_name && *last_slash != '/')
139 			last_slash--;
140 
141 		len = __symbol__join_symfs(filename, size, "");
142 		dir_size = last_slash - dso->long_name + 2;
143 		if (dir_size > (size - len)) {
144 			ret = -1;
145 			break;
146 		}
147 		len += scnprintf(filename + len, dir_size, "%s",  dso->long_name);
148 		len += scnprintf(filename + len , size - len, ".debug%s",
149 								last_slash);
150 		break;
151 	}
152 
153 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
154 		if (!dso->has_build_id) {
155 			ret = -1;
156 			break;
157 		}
158 
159 		build_id__sprintf(dso->build_id,
160 				  sizeof(dso->build_id),
161 				  build_id_hex);
162 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
163 		snprintf(filename + len, size - len, "%.2s/%s.debug",
164 			 build_id_hex, build_id_hex + 2);
165 		break;
166 
167 	case DSO_BINARY_TYPE__VMLINUX:
168 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
169 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
170 		__symbol__join_symfs(filename, size, dso->long_name);
171 		break;
172 
173 	case DSO_BINARY_TYPE__GUEST_KMODULE:
174 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
175 		path__join3(filename, size, symbol_conf.symfs,
176 			    root_dir, dso->long_name);
177 		break;
178 
179 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
180 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
181 		__symbol__join_symfs(filename, size, dso->long_name);
182 		break;
183 
184 	case DSO_BINARY_TYPE__KCORE:
185 	case DSO_BINARY_TYPE__GUEST_KCORE:
186 		snprintf(filename, size, "%s", dso->long_name);
187 		break;
188 
189 	default:
190 	case DSO_BINARY_TYPE__KALLSYMS:
191 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
192 	case DSO_BINARY_TYPE__JAVA_JIT:
193 	case DSO_BINARY_TYPE__BPF_PROG_INFO:
194 	case DSO_BINARY_TYPE__BPF_IMAGE:
195 	case DSO_BINARY_TYPE__NOT_FOUND:
196 		ret = -1;
197 		break;
198 	}
199 
200 	return ret;
201 }
202 
203 enum {
204 	COMP_ID__NONE = 0,
205 };
206 
207 static const struct {
208 	const char *fmt;
209 	int (*decompress)(const char *input, int output);
210 	bool (*is_compressed)(const char *input);
211 } compressions[] = {
212 	[COMP_ID__NONE] = { .fmt = NULL, },
213 #ifdef HAVE_ZLIB_SUPPORT
214 	{ "gz", gzip_decompress_to_file, gzip_is_compressed },
215 #endif
216 #ifdef HAVE_LZMA_SUPPORT
217 	{ "xz", lzma_decompress_to_file, lzma_is_compressed },
218 #endif
219 	{ NULL, NULL, NULL },
220 };
221 
222 static int is_supported_compression(const char *ext)
223 {
224 	unsigned i;
225 
226 	for (i = 1; compressions[i].fmt; i++) {
227 		if (!strcmp(ext, compressions[i].fmt))
228 			return i;
229 	}
230 	return COMP_ID__NONE;
231 }
232 
233 bool is_kernel_module(const char *pathname, int cpumode)
234 {
235 	struct kmod_path m;
236 	int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
237 
238 	WARN_ONCE(mode != cpumode,
239 		  "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
240 		  cpumode);
241 
242 	switch (mode) {
243 	case PERF_RECORD_MISC_USER:
244 	case PERF_RECORD_MISC_HYPERVISOR:
245 	case PERF_RECORD_MISC_GUEST_USER:
246 		return false;
247 	/* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
248 	default:
249 		if (kmod_path__parse(&m, pathname)) {
250 			pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
251 					pathname);
252 			return true;
253 		}
254 	}
255 
256 	return m.kmod;
257 }
258 
259 bool dso__needs_decompress(struct dso *dso)
260 {
261 	return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
262 		dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
263 }
264 
265 static int decompress_kmodule(struct dso *dso, const char *name,
266 			      char *pathname, size_t len)
267 {
268 	char tmpbuf[] = KMOD_DECOMP_NAME;
269 	int fd = -1;
270 
271 	if (!dso__needs_decompress(dso))
272 		return -1;
273 
274 	if (dso->comp == COMP_ID__NONE)
275 		return -1;
276 
277 	/*
278 	 * We have proper compression id for DSO and yet the file
279 	 * behind the 'name' can still be plain uncompressed object.
280 	 *
281 	 * The reason is behind the logic we open the DSO object files,
282 	 * when we try all possible 'debug' objects until we find the
283 	 * data. So even if the DSO is represented by 'krava.xz' module,
284 	 * we can end up here opening ~/.debug/....23432432/debug' file
285 	 * which is not compressed.
286 	 *
287 	 * To keep this transparent, we detect this and return the file
288 	 * descriptor to the uncompressed file.
289 	 */
290 	if (!compressions[dso->comp].is_compressed(name))
291 		return open(name, O_RDONLY);
292 
293 	fd = mkstemp(tmpbuf);
294 	if (fd < 0) {
295 		dso->load_errno = errno;
296 		return -1;
297 	}
298 
299 	if (compressions[dso->comp].decompress(name, fd)) {
300 		dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
301 		close(fd);
302 		fd = -1;
303 	}
304 
305 	if (!pathname || (fd < 0))
306 		unlink(tmpbuf);
307 
308 	if (pathname && (fd >= 0))
309 		strlcpy(pathname, tmpbuf, len);
310 
311 	return fd;
312 }
313 
314 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
315 {
316 	return decompress_kmodule(dso, name, NULL, 0);
317 }
318 
319 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
320 				 char *pathname, size_t len)
321 {
322 	int fd = decompress_kmodule(dso, name, pathname, len);
323 
324 	close(fd);
325 	return fd >= 0 ? 0 : -1;
326 }
327 
328 /*
329  * Parses kernel module specified in @path and updates
330  * @m argument like:
331  *
332  *    @comp - true if @path contains supported compression suffix,
333  *            false otherwise
334  *    @kmod - true if @path contains '.ko' suffix in right position,
335  *            false otherwise
336  *    @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
337  *            of the kernel module without suffixes, otherwise strudup-ed
338  *            base name of @path
339  *    @ext  - if (@alloc_ext && @comp) is true, it contains strdup-ed string
340  *            the compression suffix
341  *
342  * Returns 0 if there's no strdup error, -ENOMEM otherwise.
343  */
344 int __kmod_path__parse(struct kmod_path *m, const char *path,
345 		       bool alloc_name)
346 {
347 	const char *name = strrchr(path, '/');
348 	const char *ext  = strrchr(path, '.');
349 	bool is_simple_name = false;
350 
351 	memset(m, 0x0, sizeof(*m));
352 	name = name ? name + 1 : path;
353 
354 	/*
355 	 * '.' is also a valid character for module name. For example:
356 	 * [aaa.bbb] is a valid module name. '[' should have higher
357 	 * priority than '.ko' suffix.
358 	 *
359 	 * The kernel names are from machine__mmap_name. Such
360 	 * name should belong to kernel itself, not kernel module.
361 	 */
362 	if (name[0] == '[') {
363 		is_simple_name = true;
364 		if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
365 		    (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
366 		    (strncmp(name, "[vdso]", 6) == 0) ||
367 		    (strncmp(name, "[vdso32]", 8) == 0) ||
368 		    (strncmp(name, "[vdsox32]", 9) == 0) ||
369 		    (strncmp(name, "[vsyscall]", 10) == 0)) {
370 			m->kmod = false;
371 
372 		} else
373 			m->kmod = true;
374 	}
375 
376 	/* No extension, just return name. */
377 	if ((ext == NULL) || is_simple_name) {
378 		if (alloc_name) {
379 			m->name = strdup(name);
380 			return m->name ? 0 : -ENOMEM;
381 		}
382 		return 0;
383 	}
384 
385 	m->comp = is_supported_compression(ext + 1);
386 	if (m->comp > COMP_ID__NONE)
387 		ext -= 3;
388 
389 	/* Check .ko extension only if there's enough name left. */
390 	if (ext > name)
391 		m->kmod = !strncmp(ext, ".ko", 3);
392 
393 	if (alloc_name) {
394 		if (m->kmod) {
395 			if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
396 				return -ENOMEM;
397 		} else {
398 			if (asprintf(&m->name, "%s", name) == -1)
399 				return -ENOMEM;
400 		}
401 
402 		strreplace(m->name, '-', '_');
403 	}
404 
405 	return 0;
406 }
407 
408 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
409 			  struct machine *machine)
410 {
411 	if (machine__is_host(machine))
412 		dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
413 	else
414 		dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
415 
416 	/* _KMODULE_COMP should be next to _KMODULE */
417 	if (m->kmod && m->comp) {
418 		dso->symtab_type++;
419 		dso->comp = m->comp;
420 	}
421 
422 	dso__set_short_name(dso, strdup(m->name), true);
423 }
424 
425 /*
426  * Global list of open DSOs and the counter.
427  */
428 static LIST_HEAD(dso__data_open);
429 static long dso__data_open_cnt;
430 static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
431 
432 static void dso__list_add(struct dso *dso)
433 {
434 	list_add_tail(&dso->data.open_entry, &dso__data_open);
435 	dso__data_open_cnt++;
436 }
437 
438 static void dso__list_del(struct dso *dso)
439 {
440 	list_del_init(&dso->data.open_entry);
441 	WARN_ONCE(dso__data_open_cnt <= 0,
442 		  "DSO data fd counter out of bounds.");
443 	dso__data_open_cnt--;
444 }
445 
446 static void close_first_dso(void);
447 
448 static int do_open(char *name)
449 {
450 	int fd;
451 	char sbuf[STRERR_BUFSIZE];
452 
453 	do {
454 		fd = open(name, O_RDONLY|O_CLOEXEC);
455 		if (fd >= 0)
456 			return fd;
457 
458 		pr_debug("dso open failed: %s\n",
459 			 str_error_r(errno, sbuf, sizeof(sbuf)));
460 		if (!dso__data_open_cnt || errno != EMFILE)
461 			break;
462 
463 		close_first_dso();
464 	} while (1);
465 
466 	return -1;
467 }
468 
469 static int __open_dso(struct dso *dso, struct machine *machine)
470 {
471 	int fd = -EINVAL;
472 	char *root_dir = (char *)"";
473 	char *name = malloc(PATH_MAX);
474 	bool decomp = false;
475 
476 	if (!name)
477 		return -ENOMEM;
478 
479 	if (machine)
480 		root_dir = machine->root_dir;
481 
482 	if (dso__read_binary_type_filename(dso, dso->binary_type,
483 					    root_dir, name, PATH_MAX))
484 		goto out;
485 
486 	if (!is_regular_file(name))
487 		goto out;
488 
489 	if (dso__needs_decompress(dso)) {
490 		char newpath[KMOD_DECOMP_LEN];
491 		size_t len = sizeof(newpath);
492 
493 		if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
494 			fd = -dso->load_errno;
495 			goto out;
496 		}
497 
498 		decomp = true;
499 		strcpy(name, newpath);
500 	}
501 
502 	fd = do_open(name);
503 
504 	if (decomp)
505 		unlink(name);
506 
507 out:
508 	free(name);
509 	return fd;
510 }
511 
512 static void check_data_close(void);
513 
514 /**
515  * dso_close - Open DSO data file
516  * @dso: dso object
517  *
518  * Open @dso's data file descriptor and updates
519  * list/count of open DSO objects.
520  */
521 static int open_dso(struct dso *dso, struct machine *machine)
522 {
523 	int fd;
524 	struct nscookie nsc;
525 
526 	if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
527 		nsinfo__mountns_enter(dso->nsinfo, &nsc);
528 	fd = __open_dso(dso, machine);
529 	if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
530 		nsinfo__mountns_exit(&nsc);
531 
532 	if (fd >= 0) {
533 		dso__list_add(dso);
534 		/*
535 		 * Check if we crossed the allowed number
536 		 * of opened DSOs and close one if needed.
537 		 */
538 		check_data_close();
539 	}
540 
541 	return fd;
542 }
543 
544 static void close_data_fd(struct dso *dso)
545 {
546 	if (dso->data.fd >= 0) {
547 		close(dso->data.fd);
548 		dso->data.fd = -1;
549 		dso->data.file_size = 0;
550 		dso__list_del(dso);
551 	}
552 }
553 
554 /**
555  * dso_close - Close DSO data file
556  * @dso: dso object
557  *
558  * Close @dso's data file descriptor and updates
559  * list/count of open DSO objects.
560  */
561 static void close_dso(struct dso *dso)
562 {
563 	close_data_fd(dso);
564 }
565 
566 static void close_first_dso(void)
567 {
568 	struct dso *dso;
569 
570 	dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
571 	close_dso(dso);
572 }
573 
574 static rlim_t get_fd_limit(void)
575 {
576 	struct rlimit l;
577 	rlim_t limit = 0;
578 
579 	/* Allow half of the current open fd limit. */
580 	if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
581 		if (l.rlim_cur == RLIM_INFINITY)
582 			limit = l.rlim_cur;
583 		else
584 			limit = l.rlim_cur / 2;
585 	} else {
586 		pr_err("failed to get fd limit\n");
587 		limit = 1;
588 	}
589 
590 	return limit;
591 }
592 
593 static rlim_t fd_limit;
594 
595 /*
596  * Used only by tests/dso-data.c to reset the environment
597  * for tests. I dont expect we should change this during
598  * standard runtime.
599  */
600 void reset_fd_limit(void)
601 {
602 	fd_limit = 0;
603 }
604 
605 static bool may_cache_fd(void)
606 {
607 	if (!fd_limit)
608 		fd_limit = get_fd_limit();
609 
610 	if (fd_limit == RLIM_INFINITY)
611 		return true;
612 
613 	return fd_limit > (rlim_t) dso__data_open_cnt;
614 }
615 
616 /*
617  * Check and close LRU dso if we crossed allowed limit
618  * for opened dso file descriptors. The limit is half
619  * of the RLIMIT_NOFILE files opened.
620 */
621 static void check_data_close(void)
622 {
623 	bool cache_fd = may_cache_fd();
624 
625 	if (!cache_fd)
626 		close_first_dso();
627 }
628 
629 /**
630  * dso__data_close - Close DSO data file
631  * @dso: dso object
632  *
633  * External interface to close @dso's data file descriptor.
634  */
635 void dso__data_close(struct dso *dso)
636 {
637 	pthread_mutex_lock(&dso__data_open_lock);
638 	close_dso(dso);
639 	pthread_mutex_unlock(&dso__data_open_lock);
640 }
641 
642 static void try_to_open_dso(struct dso *dso, struct machine *machine)
643 {
644 	enum dso_binary_type binary_type_data[] = {
645 		DSO_BINARY_TYPE__BUILD_ID_CACHE,
646 		DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
647 		DSO_BINARY_TYPE__NOT_FOUND,
648 	};
649 	int i = 0;
650 
651 	if (dso->data.fd >= 0)
652 		return;
653 
654 	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
655 		dso->data.fd = open_dso(dso, machine);
656 		goto out;
657 	}
658 
659 	do {
660 		dso->binary_type = binary_type_data[i++];
661 
662 		dso->data.fd = open_dso(dso, machine);
663 		if (dso->data.fd >= 0)
664 			goto out;
665 
666 	} while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
667 out:
668 	if (dso->data.fd >= 0)
669 		dso->data.status = DSO_DATA_STATUS_OK;
670 	else
671 		dso->data.status = DSO_DATA_STATUS_ERROR;
672 }
673 
674 /**
675  * dso__data_get_fd - Get dso's data file descriptor
676  * @dso: dso object
677  * @machine: machine object
678  *
679  * External interface to find dso's file, open it and
680  * returns file descriptor.  It should be paired with
681  * dso__data_put_fd() if it returns non-negative value.
682  */
683 int dso__data_get_fd(struct dso *dso, struct machine *machine)
684 {
685 	if (dso->data.status == DSO_DATA_STATUS_ERROR)
686 		return -1;
687 
688 	if (pthread_mutex_lock(&dso__data_open_lock) < 0)
689 		return -1;
690 
691 	try_to_open_dso(dso, machine);
692 
693 	if (dso->data.fd < 0)
694 		pthread_mutex_unlock(&dso__data_open_lock);
695 
696 	return dso->data.fd;
697 }
698 
699 void dso__data_put_fd(struct dso *dso __maybe_unused)
700 {
701 	pthread_mutex_unlock(&dso__data_open_lock);
702 }
703 
704 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
705 {
706 	u32 flag = 1 << by;
707 
708 	if (dso->data.status_seen & flag)
709 		return true;
710 
711 	dso->data.status_seen |= flag;
712 
713 	return false;
714 }
715 
716 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
717 {
718 	struct bpf_prog_info_node *node;
719 	ssize_t size = DSO__DATA_CACHE_SIZE;
720 	u64 len;
721 	u8 *buf;
722 
723 	node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
724 	if (!node || !node->info_linear) {
725 		dso->data.status = DSO_DATA_STATUS_ERROR;
726 		return -1;
727 	}
728 
729 	len = node->info_linear->info.jited_prog_len;
730 	buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
731 
732 	if (offset >= len)
733 		return -1;
734 
735 	size = (ssize_t)min(len - offset, (u64)size);
736 	memcpy(data, buf + offset, size);
737 	return size;
738 }
739 
740 static int bpf_size(struct dso *dso)
741 {
742 	struct bpf_prog_info_node *node;
743 
744 	node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
745 	if (!node || !node->info_linear) {
746 		dso->data.status = DSO_DATA_STATUS_ERROR;
747 		return -1;
748 	}
749 
750 	dso->data.file_size = node->info_linear->info.jited_prog_len;
751 	return 0;
752 }
753 
754 static void
755 dso_cache__free(struct dso *dso)
756 {
757 	struct rb_root *root = &dso->data.cache;
758 	struct rb_node *next = rb_first(root);
759 
760 	pthread_mutex_lock(&dso->lock);
761 	while (next) {
762 		struct dso_cache *cache;
763 
764 		cache = rb_entry(next, struct dso_cache, rb_node);
765 		next = rb_next(&cache->rb_node);
766 		rb_erase(&cache->rb_node, root);
767 		free(cache);
768 	}
769 	pthread_mutex_unlock(&dso->lock);
770 }
771 
772 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
773 {
774 	const struct rb_root *root = &dso->data.cache;
775 	struct rb_node * const *p = &root->rb_node;
776 	const struct rb_node *parent = NULL;
777 	struct dso_cache *cache;
778 
779 	while (*p != NULL) {
780 		u64 end;
781 
782 		parent = *p;
783 		cache = rb_entry(parent, struct dso_cache, rb_node);
784 		end = cache->offset + DSO__DATA_CACHE_SIZE;
785 
786 		if (offset < cache->offset)
787 			p = &(*p)->rb_left;
788 		else if (offset >= end)
789 			p = &(*p)->rb_right;
790 		else
791 			return cache;
792 	}
793 
794 	return NULL;
795 }
796 
797 static struct dso_cache *
798 dso_cache__insert(struct dso *dso, struct dso_cache *new)
799 {
800 	struct rb_root *root = &dso->data.cache;
801 	struct rb_node **p = &root->rb_node;
802 	struct rb_node *parent = NULL;
803 	struct dso_cache *cache;
804 	u64 offset = new->offset;
805 
806 	pthread_mutex_lock(&dso->lock);
807 	while (*p != NULL) {
808 		u64 end;
809 
810 		parent = *p;
811 		cache = rb_entry(parent, struct dso_cache, rb_node);
812 		end = cache->offset + DSO__DATA_CACHE_SIZE;
813 
814 		if (offset < cache->offset)
815 			p = &(*p)->rb_left;
816 		else if (offset >= end)
817 			p = &(*p)->rb_right;
818 		else
819 			goto out;
820 	}
821 
822 	rb_link_node(&new->rb_node, parent, p);
823 	rb_insert_color(&new->rb_node, root);
824 
825 	cache = NULL;
826 out:
827 	pthread_mutex_unlock(&dso->lock);
828 	return cache;
829 }
830 
831 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
832 				 u64 size, bool out)
833 {
834 	u64 cache_offset = offset - cache->offset;
835 	u64 cache_size   = min(cache->size - cache_offset, size);
836 
837 	if (out)
838 		memcpy(data, cache->data + cache_offset, cache_size);
839 	else
840 		memcpy(cache->data + cache_offset, data, cache_size);
841 	return cache_size;
842 }
843 
844 static ssize_t file_read(struct dso *dso, struct machine *machine,
845 			 u64 offset, char *data)
846 {
847 	ssize_t ret;
848 
849 	pthread_mutex_lock(&dso__data_open_lock);
850 
851 	/*
852 	 * dso->data.fd might be closed if other thread opened another
853 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
854 	 */
855 	try_to_open_dso(dso, machine);
856 
857 	if (dso->data.fd < 0) {
858 		dso->data.status = DSO_DATA_STATUS_ERROR;
859 		ret = -errno;
860 		goto out;
861 	}
862 
863 	ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
864 out:
865 	pthread_mutex_unlock(&dso__data_open_lock);
866 	return ret;
867 }
868 
869 static struct dso_cache *dso_cache__populate(struct dso *dso,
870 					     struct machine *machine,
871 					     u64 offset, ssize_t *ret)
872 {
873 	u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
874 	struct dso_cache *cache;
875 	struct dso_cache *old;
876 
877 	cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
878 	if (!cache) {
879 		*ret = -ENOMEM;
880 		return NULL;
881 	}
882 
883 	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
884 		*ret = bpf_read(dso, cache_offset, cache->data);
885 	else
886 		*ret = file_read(dso, machine, cache_offset, cache->data);
887 
888 	if (*ret <= 0) {
889 		free(cache);
890 		return NULL;
891 	}
892 
893 	cache->offset = cache_offset;
894 	cache->size   = *ret;
895 
896 	old = dso_cache__insert(dso, cache);
897 	if (old) {
898 		/* we lose the race */
899 		free(cache);
900 		cache = old;
901 	}
902 
903 	return cache;
904 }
905 
906 static struct dso_cache *dso_cache__find(struct dso *dso,
907 					 struct machine *machine,
908 					 u64 offset,
909 					 ssize_t *ret)
910 {
911 	struct dso_cache *cache = __dso_cache__find(dso, offset);
912 
913 	return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
914 }
915 
916 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
917 			    u64 offset, u8 *data, ssize_t size, bool out)
918 {
919 	struct dso_cache *cache;
920 	ssize_t ret = 0;
921 
922 	cache = dso_cache__find(dso, machine, offset, &ret);
923 	if (!cache)
924 		return ret;
925 
926 	return dso_cache__memcpy(cache, offset, data, size, out);
927 }
928 
929 /*
930  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
931  * in the rb_tree. Any read to already cached data is served
932  * by cached data. Writes update the cache only, not the backing file.
933  */
934 static ssize_t cached_io(struct dso *dso, struct machine *machine,
935 			 u64 offset, u8 *data, ssize_t size, bool out)
936 {
937 	ssize_t r = 0;
938 	u8 *p = data;
939 
940 	do {
941 		ssize_t ret;
942 
943 		ret = dso_cache_io(dso, machine, offset, p, size, out);
944 		if (ret < 0)
945 			return ret;
946 
947 		/* Reached EOF, return what we have. */
948 		if (!ret)
949 			break;
950 
951 		BUG_ON(ret > size);
952 
953 		r      += ret;
954 		p      += ret;
955 		offset += ret;
956 		size   -= ret;
957 
958 	} while (size);
959 
960 	return r;
961 }
962 
963 static int file_size(struct dso *dso, struct machine *machine)
964 {
965 	int ret = 0;
966 	struct stat st;
967 	char sbuf[STRERR_BUFSIZE];
968 
969 	pthread_mutex_lock(&dso__data_open_lock);
970 
971 	/*
972 	 * dso->data.fd might be closed if other thread opened another
973 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
974 	 */
975 	try_to_open_dso(dso, machine);
976 
977 	if (dso->data.fd < 0) {
978 		ret = -errno;
979 		dso->data.status = DSO_DATA_STATUS_ERROR;
980 		goto out;
981 	}
982 
983 	if (fstat(dso->data.fd, &st) < 0) {
984 		ret = -errno;
985 		pr_err("dso cache fstat failed: %s\n",
986 		       str_error_r(errno, sbuf, sizeof(sbuf)));
987 		dso->data.status = DSO_DATA_STATUS_ERROR;
988 		goto out;
989 	}
990 	dso->data.file_size = st.st_size;
991 
992 out:
993 	pthread_mutex_unlock(&dso__data_open_lock);
994 	return ret;
995 }
996 
997 int dso__data_file_size(struct dso *dso, struct machine *machine)
998 {
999 	if (dso->data.file_size)
1000 		return 0;
1001 
1002 	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1003 		return -1;
1004 
1005 	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1006 		return bpf_size(dso);
1007 
1008 	return file_size(dso, machine);
1009 }
1010 
1011 /**
1012  * dso__data_size - Return dso data size
1013  * @dso: dso object
1014  * @machine: machine object
1015  *
1016  * Return: dso data size
1017  */
1018 off_t dso__data_size(struct dso *dso, struct machine *machine)
1019 {
1020 	if (dso__data_file_size(dso, machine))
1021 		return -1;
1022 
1023 	/* For now just estimate dso data size is close to file size */
1024 	return dso->data.file_size;
1025 }
1026 
1027 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1028 				      u64 offset, u8 *data, ssize_t size,
1029 				      bool out)
1030 {
1031 	if (dso__data_file_size(dso, machine))
1032 		return -1;
1033 
1034 	/* Check the offset sanity. */
1035 	if (offset > dso->data.file_size)
1036 		return -1;
1037 
1038 	if (offset + size < offset)
1039 		return -1;
1040 
1041 	return cached_io(dso, machine, offset, data, size, out);
1042 }
1043 
1044 /**
1045  * dso__data_read_offset - Read data from dso file offset
1046  * @dso: dso object
1047  * @machine: machine object
1048  * @offset: file offset
1049  * @data: buffer to store data
1050  * @size: size of the @data buffer
1051  *
1052  * External interface to read data from dso file offset. Open
1053  * dso data file and use cached_read to get the data.
1054  */
1055 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1056 			      u64 offset, u8 *data, ssize_t size)
1057 {
1058 	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1059 		return -1;
1060 
1061 	return data_read_write_offset(dso, machine, offset, data, size, true);
1062 }
1063 
1064 /**
1065  * dso__data_read_addr - Read data from dso address
1066  * @dso: dso object
1067  * @machine: machine object
1068  * @add: virtual memory address
1069  * @data: buffer to store data
1070  * @size: size of the @data buffer
1071  *
1072  * External interface to read data from dso address.
1073  */
1074 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1075 			    struct machine *machine, u64 addr,
1076 			    u8 *data, ssize_t size)
1077 {
1078 	u64 offset = map->map_ip(map, addr);
1079 	return dso__data_read_offset(dso, machine, offset, data, size);
1080 }
1081 
1082 /**
1083  * dso__data_write_cache_offs - Write data to dso data cache at file offset
1084  * @dso: dso object
1085  * @machine: machine object
1086  * @offset: file offset
1087  * @data: buffer to write
1088  * @size: size of the @data buffer
1089  *
1090  * Write into the dso file data cache, but do not change the file itself.
1091  */
1092 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1093 				   u64 offset, const u8 *data_in, ssize_t size)
1094 {
1095 	u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1096 
1097 	if (dso->data.status == DSO_DATA_STATUS_ERROR)
1098 		return -1;
1099 
1100 	return data_read_write_offset(dso, machine, offset, data, size, false);
1101 }
1102 
1103 /**
1104  * dso__data_write_cache_addr - Write data to dso data cache at dso address
1105  * @dso: dso object
1106  * @machine: machine object
1107  * @add: virtual memory address
1108  * @data: buffer to write
1109  * @size: size of the @data buffer
1110  *
1111  * External interface to write into the dso file data cache, but do not change
1112  * the file itself.
1113  */
1114 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1115 				   struct machine *machine, u64 addr,
1116 				   const u8 *data, ssize_t size)
1117 {
1118 	u64 offset = map->map_ip(map, addr);
1119 	return dso__data_write_cache_offs(dso, machine, offset, data, size);
1120 }
1121 
1122 struct map *dso__new_map(const char *name)
1123 {
1124 	struct map *map = NULL;
1125 	struct dso *dso = dso__new(name);
1126 
1127 	if (dso)
1128 		map = map__new2(0, dso);
1129 
1130 	return map;
1131 }
1132 
1133 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1134 				    const char *short_name, int dso_type)
1135 {
1136 	/*
1137 	 * The kernel dso could be created by build_id processing.
1138 	 */
1139 	struct dso *dso = machine__findnew_dso(machine, name);
1140 
1141 	/*
1142 	 * We need to run this in all cases, since during the build_id
1143 	 * processing we had no idea this was the kernel dso.
1144 	 */
1145 	if (dso != NULL) {
1146 		dso__set_short_name(dso, short_name, false);
1147 		dso->kernel = dso_type;
1148 	}
1149 
1150 	return dso;
1151 }
1152 
1153 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1154 {
1155 	struct rb_root *root = dso->root;
1156 
1157 	if (name == NULL)
1158 		return;
1159 
1160 	if (dso->long_name_allocated)
1161 		free((char *)dso->long_name);
1162 
1163 	if (root) {
1164 		rb_erase(&dso->rb_node, root);
1165 		/*
1166 		 * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1167 		 * add it back, so a clean removal is required here.
1168 		 */
1169 		RB_CLEAR_NODE(&dso->rb_node);
1170 		dso->root = NULL;
1171 	}
1172 
1173 	dso->long_name		 = name;
1174 	dso->long_name_len	 = strlen(name);
1175 	dso->long_name_allocated = name_allocated;
1176 
1177 	if (root)
1178 		__dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1179 }
1180 
1181 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1182 {
1183 	dso__set_long_name_id(dso, name, NULL, name_allocated);
1184 }
1185 
1186 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1187 {
1188 	if (name == NULL)
1189 		return;
1190 
1191 	if (dso->short_name_allocated)
1192 		free((char *)dso->short_name);
1193 
1194 	dso->short_name		  = name;
1195 	dso->short_name_len	  = strlen(name);
1196 	dso->short_name_allocated = name_allocated;
1197 }
1198 
1199 int dso__name_len(const struct dso *dso)
1200 {
1201 	if (!dso)
1202 		return strlen("[unknown]");
1203 	if (verbose > 0)
1204 		return dso->long_name_len;
1205 
1206 	return dso->short_name_len;
1207 }
1208 
1209 bool dso__loaded(const struct dso *dso)
1210 {
1211 	return dso->loaded;
1212 }
1213 
1214 bool dso__sorted_by_name(const struct dso *dso)
1215 {
1216 	return dso->sorted_by_name;
1217 }
1218 
1219 void dso__set_sorted_by_name(struct dso *dso)
1220 {
1221 	dso->sorted_by_name = true;
1222 }
1223 
1224 struct dso *dso__new_id(const char *name, struct dso_id *id)
1225 {
1226 	struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1227 
1228 	if (dso != NULL) {
1229 		strcpy(dso->name, name);
1230 		if (id)
1231 			dso->id = *id;
1232 		dso__set_long_name_id(dso, dso->name, id, false);
1233 		dso__set_short_name(dso, dso->name, false);
1234 		dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1235 		dso->data.cache = RB_ROOT;
1236 		dso->inlined_nodes = RB_ROOT_CACHED;
1237 		dso->srclines = RB_ROOT_CACHED;
1238 		dso->data.fd = -1;
1239 		dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1240 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1241 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1242 		dso->is_64_bit = (sizeof(void *) == 8);
1243 		dso->loaded = 0;
1244 		dso->rel = 0;
1245 		dso->sorted_by_name = 0;
1246 		dso->has_build_id = 0;
1247 		dso->has_srcline = 1;
1248 		dso->a2l_fails = 1;
1249 		dso->kernel = DSO_TYPE_USER;
1250 		dso->needs_swap = DSO_SWAP__UNSET;
1251 		dso->comp = COMP_ID__NONE;
1252 		RB_CLEAR_NODE(&dso->rb_node);
1253 		dso->root = NULL;
1254 		INIT_LIST_HEAD(&dso->node);
1255 		INIT_LIST_HEAD(&dso->data.open_entry);
1256 		pthread_mutex_init(&dso->lock, NULL);
1257 		refcount_set(&dso->refcnt, 1);
1258 	}
1259 
1260 	return dso;
1261 }
1262 
1263 struct dso *dso__new(const char *name)
1264 {
1265 	return dso__new_id(name, NULL);
1266 }
1267 
1268 void dso__delete(struct dso *dso)
1269 {
1270 	if (!RB_EMPTY_NODE(&dso->rb_node))
1271 		pr_err("DSO %s is still in rbtree when being deleted!\n",
1272 		       dso->long_name);
1273 
1274 	/* free inlines first, as they reference symbols */
1275 	inlines__tree_delete(&dso->inlined_nodes);
1276 	srcline__tree_delete(&dso->srclines);
1277 	symbols__delete(&dso->symbols);
1278 
1279 	if (dso->short_name_allocated) {
1280 		zfree((char **)&dso->short_name);
1281 		dso->short_name_allocated = false;
1282 	}
1283 
1284 	if (dso->long_name_allocated) {
1285 		zfree((char **)&dso->long_name);
1286 		dso->long_name_allocated = false;
1287 	}
1288 
1289 	dso__data_close(dso);
1290 	auxtrace_cache__free(dso->auxtrace_cache);
1291 	dso_cache__free(dso);
1292 	dso__free_a2l(dso);
1293 	zfree(&dso->symsrc_filename);
1294 	nsinfo__zput(dso->nsinfo);
1295 	pthread_mutex_destroy(&dso->lock);
1296 	free(dso);
1297 }
1298 
1299 struct dso *dso__get(struct dso *dso)
1300 {
1301 	if (dso)
1302 		refcount_inc(&dso->refcnt);
1303 	return dso;
1304 }
1305 
1306 void dso__put(struct dso *dso)
1307 {
1308 	if (dso && refcount_dec_and_test(&dso->refcnt))
1309 		dso__delete(dso);
1310 }
1311 
1312 void dso__set_build_id(struct dso *dso, void *build_id)
1313 {
1314 	memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1315 	dso->has_build_id = 1;
1316 }
1317 
1318 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1319 {
1320 	return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1321 }
1322 
1323 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1324 {
1325 	char path[PATH_MAX];
1326 
1327 	if (machine__is_default_guest(machine))
1328 		return;
1329 	sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1330 	if (sysfs__read_build_id(path, dso->build_id,
1331 				 sizeof(dso->build_id)) == 0)
1332 		dso->has_build_id = true;
1333 }
1334 
1335 int dso__kernel_module_get_build_id(struct dso *dso,
1336 				    const char *root_dir)
1337 {
1338 	char filename[PATH_MAX];
1339 	/*
1340 	 * kernel module short names are of the form "[module]" and
1341 	 * we need just "module" here.
1342 	 */
1343 	const char *name = dso->short_name + 1;
1344 
1345 	snprintf(filename, sizeof(filename),
1346 		 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1347 		 root_dir, (int)strlen(name) - 1, name);
1348 
1349 	if (sysfs__read_build_id(filename, dso->build_id,
1350 				 sizeof(dso->build_id)) == 0)
1351 		dso->has_build_id = true;
1352 
1353 	return 0;
1354 }
1355 
1356 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1357 {
1358 	char sbuild_id[SBUILD_ID_SIZE];
1359 
1360 	build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1361 	return fprintf(fp, "%s", sbuild_id);
1362 }
1363 
1364 size_t dso__fprintf(struct dso *dso, FILE *fp)
1365 {
1366 	struct rb_node *nd;
1367 	size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1368 
1369 	if (dso->short_name != dso->long_name)
1370 		ret += fprintf(fp, "%s, ", dso->long_name);
1371 	ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1372 	ret += dso__fprintf_buildid(dso, fp);
1373 	ret += fprintf(fp, ")\n");
1374 	for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1375 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1376 		ret += symbol__fprintf(pos, fp);
1377 	}
1378 
1379 	return ret;
1380 }
1381 
1382 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1383 {
1384 	int fd;
1385 	enum dso_type type = DSO__TYPE_UNKNOWN;
1386 
1387 	fd = dso__data_get_fd(dso, machine);
1388 	if (fd >= 0) {
1389 		type = dso__type_fd(fd);
1390 		dso__data_put_fd(dso);
1391 	}
1392 
1393 	return type;
1394 }
1395 
1396 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1397 {
1398 	int idx, errnum = dso->load_errno;
1399 	/*
1400 	 * This must have a same ordering as the enum dso_load_errno.
1401 	 */
1402 	static const char *dso_load__error_str[] = {
1403 	"Internal tools/perf/ library error",
1404 	"Invalid ELF file",
1405 	"Can not read build id",
1406 	"Mismatching build id",
1407 	"Decompression failure",
1408 	};
1409 
1410 	BUG_ON(buflen == 0);
1411 
1412 	if (errnum >= 0) {
1413 		const char *err = str_error_r(errnum, buf, buflen);
1414 
1415 		if (err != buf)
1416 			scnprintf(buf, buflen, "%s", err);
1417 
1418 		return 0;
1419 	}
1420 
1421 	if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1422 		return -1;
1423 
1424 	idx = errnum - __DSO_LOAD_ERRNO__START;
1425 	scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1426 	return 0;
1427 }
1428