db_disasm.c revision 1.1.28.2 1 1.1.28.2 pgoyette /* $NetBSD: db_disasm.c,v 1.1.28.2 2018/07/28 04:37:25 pgoyette Exp $ */
2 1.1 matt
3 1.1.28.1 pgoyette /*
4 1.1.28.1 pgoyette * Copyright (c) 2017 Ryo Shimizu <ryo (at) nerv.org>
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * Redistribution and use in source and binary forms, with or without
8 1.1 matt * modification, are permitted provided that the following conditions
9 1.1 matt * are met:
10 1.1 matt * 1. Redistributions of source code must retain the above copyright
11 1.1 matt * notice, this list of conditions and the following disclaimer.
12 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 matt * notice, this list of conditions and the following disclaimer in the
14 1.1 matt * documentation and/or other materials provided with the distribution.
15 1.1 matt *
16 1.1.28.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.28.1 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1.28.1 pgoyette * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1.28.1 pgoyette * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1.28.1 pgoyette * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1.28.1 pgoyette * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1.28.1 pgoyette * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.28.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1.28.1 pgoyette * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1.28.1 pgoyette * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
27 1.1 matt */
28 1.1 matt
29 1.1 matt #include <sys/cdefs.h>
30 1.1.28.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.1.28.2 2018/07/28 04:37:25 pgoyette Exp $");
31 1.1.28.1 pgoyette
32 1.1.28.1 pgoyette #include <sys/param.h>
33 1.1.28.1 pgoyette #include <machine/db_machdep.h>
34 1.1.28.1 pgoyette #include <ddb/db_interface.h>
35 1.1.28.1 pgoyette #include <ddb/db_sym.h>
36 1.1.28.1 pgoyette #include <ddb/db_output.h>
37 1.1.28.1 pgoyette #include <ddb/db_access.h>
38 1.1.28.1 pgoyette
39 1.1.28.1 pgoyette #include <arch/aarch64/aarch64/disasm.h>
40 1.1.28.1 pgoyette
41 1.1.28.1 pgoyette static uint32_t
42 1.1.28.1 pgoyette db_disasm_readword(uintptr_t address)
43 1.1.28.1 pgoyette {
44 1.1.28.1 pgoyette return db_get_value(address, sizeof(uint32_t), false);
45 1.1.28.1 pgoyette }
46 1.1 matt
47 1.1.28.1 pgoyette static void
48 1.1.28.1 pgoyette db_disasm_printaddr(uintptr_t address)
49 1.1.28.1 pgoyette {
50 1.1.28.1 pgoyette db_printf("%lx <", address);
51 1.1.28.1 pgoyette db_printsym((db_addr_t)address, DB_STGY_ANY, db_printf);
52 1.1.28.1 pgoyette db_printf(">");
53 1.1.28.1 pgoyette }
54 1.1 matt
55 1.1.28.1 pgoyette static const disasm_interface_t db_disasm_interface = {
56 1.1.28.1 pgoyette .di_readword = db_disasm_readword,
57 1.1.28.1 pgoyette .di_printaddr = db_disasm_printaddr,
58 1.1.28.1 pgoyette .di_printf = db_printf
59 1.1.28.1 pgoyette };
60 1.1 matt
61 1.1.28.1 pgoyette db_addr_t
62 1.1.28.1 pgoyette db_disasm(db_addr_t loc, bool altfmt)
63 1.1.28.1 pgoyette {
64 1.1.28.1 pgoyette return disasm(&db_disasm_interface, loc);
65 1.1.28.1 pgoyette }
66 1.1.28.2 pgoyette
67 1.1.28.2 pgoyette
68 1.1.28.2 pgoyette static char *strdisasm_ptr;
69 1.1.28.2 pgoyette static char strdisasm_buf[256];
70 1.1.28.2 pgoyette
71 1.1.28.2 pgoyette static uint32_t
72 1.1.28.2 pgoyette strdisasm_readword(uintptr_t address)
73 1.1.28.2 pgoyette {
74 1.1.28.2 pgoyette return *(uint32_t *)address;
75 1.1.28.2 pgoyette }
76 1.1.28.2 pgoyette
77 1.1.28.2 pgoyette static void
78 1.1.28.2 pgoyette strdisasm_printf(const char *fmt, ...)
79 1.1.28.2 pgoyette {
80 1.1.28.2 pgoyette va_list ap;
81 1.1.28.2 pgoyette int len;
82 1.1.28.2 pgoyette
83 1.1.28.2 pgoyette /* calculation spaces to append a string */
84 1.1.28.2 pgoyette len = strdisasm_buf + sizeof(strdisasm_buf) - strdisasm_ptr;
85 1.1.28.2 pgoyette
86 1.1.28.2 pgoyette va_start(ap, fmt);
87 1.1.28.2 pgoyette len = vsnprintf(strdisasm_ptr, len, fmt, ap);
88 1.1.28.2 pgoyette va_end(ap);
89 1.1.28.2 pgoyette
90 1.1.28.2 pgoyette strdisasm_ptr += len;
91 1.1.28.2 pgoyette }
92 1.1.28.2 pgoyette
93 1.1.28.2 pgoyette static void
94 1.1.28.2 pgoyette strdisasm_printaddr(uintptr_t address)
95 1.1.28.2 pgoyette {
96 1.1.28.2 pgoyette strdisasm_printf("0x%lx", address);
97 1.1.28.2 pgoyette }
98 1.1.28.2 pgoyette
99 1.1.28.2 pgoyette static const disasm_interface_t strdisasm_interface = {
100 1.1.28.2 pgoyette .di_readword = strdisasm_readword,
101 1.1.28.2 pgoyette .di_printaddr = strdisasm_printaddr,
102 1.1.28.2 pgoyette .di_printf = strdisasm_printf
103 1.1.28.2 pgoyette };
104 1.1.28.2 pgoyette
105 1.1.28.2 pgoyette const char *
106 1.1.28.2 pgoyette strdisasm(vaddr_t pc)
107 1.1.28.2 pgoyette {
108 1.1.28.2 pgoyette char *p;
109 1.1.28.2 pgoyette
110 1.1.28.2 pgoyette strdisasm_ptr = strdisasm_buf;
111 1.1.28.2 pgoyette disasm(&strdisasm_interface, (db_addr_t)pc);
112 1.1.28.2 pgoyette
113 1.1.28.2 pgoyette /* replace tab to space, and chomp '\n' */
114 1.1.28.2 pgoyette for (p = strdisasm_buf; *p != '\0'; p++) {
115 1.1.28.2 pgoyette if (*p == '\t')
116 1.1.28.2 pgoyette *p = ' ';
117 1.1.28.2 pgoyette }
118 1.1.28.2 pgoyette if ((p > strdisasm_buf) && (p[-1] == '\n'))
119 1.1.28.2 pgoyette p[-1] = '\0';
120 1.1.28.2 pgoyette
121 1.1.28.2 pgoyette return strdisasm_buf;
122 1.1.28.2 pgoyette }
123