1 /*
2  * Copyright (c) 2024 Meta Platforms
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <stdbool.h>
9 
10 #include <zephyr/debug/symtab.h>
11 #include <zephyr/shell/shell.h>
12 
symtab_get(void)13 const struct symtab_info *symtab_get(void)
14 {
15 	extern const struct symtab_info z_symtab;
16 
17 	return &z_symtab;
18 }
19 
symtab_find_symbol_name(uintptr_t addr,uint32_t * offset)20 const char *symtab_find_symbol_name(uintptr_t addr, uint32_t *offset)
21 {
22 	const struct symtab_info *const symtab = symtab_get();
23 	const uint32_t symbol_offset = addr - symtab->first_addr;
24 	uint32_t left = 0, right = symtab->length;
25 	uint32_t ret_offset = 0;
26 	const char *ret_name = "?";
27 
28 	/* No need to search if the address is out-of-bound */
29 	if (symbol_offset < symtab->entries[symtab->length].offset) {
30 		while (left <= right) {
31 			uint32_t mid = left + (right - left) / 2;
32 
33 			if ((symbol_offset >= symtab->entries[mid].offset) &&
34 			(symbol_offset < symtab->entries[mid + 1].offset)) {
35 				ret_offset = symbol_offset - symtab->entries[mid].offset;
36 				ret_name = symtab->entries[mid].name;
37 				break;
38 			} else if (symbol_offset < symtab->entries[mid].offset) {
39 				right = mid - 1;
40 			} else {
41 				left = mid + 1;
42 			}
43 		}
44 	}
45 
46 	if (offset != NULL) {
47 		*offset = ret_offset;
48 	}
49 
50 	return ret_name;
51 }
52 
53 #ifdef CONFIG_SYMTAB_SHELL
cmd_symtab_list(const struct shell * sh,size_t argc,char * argv[])54 static int cmd_symtab_list(const struct shell *sh, size_t argc, char *argv[])
55 {
56 	ARG_UNUSED(argc);
57 	ARG_UNUSED(argv);
58 
59 	const struct symtab_info *const symtab = symtab_get();
60 
61 	for (uint32_t i = 0; i < symtab->length; i++) {
62 		const struct z_symtab_entry *const entry = &symtab->entries[i];
63 
64 		shell_print(sh, "%d\t%p  %s", i + 1, (void *)(entry->offset + symtab->first_addr),
65 			    entry->name);
66 	}
67 
68 	return 0;
69 }
70 
71 SHELL_STATIC_SUBCMD_SET_CREATE(symtab_cmds,
72 	SHELL_CMD(list, NULL, "Show symbol list.", cmd_symtab_list),
73 	SHELL_SUBCMD_SET_END
74 );
75 
76 SHELL_CMD_REGISTER(symtab, &symtab_cmds, "Symbol table shell commands", NULL);
77 #endif /* CONFIG_SYMTAB_SHELL */
78