Home | History | Annotate | Line # | Download | only in aarch64
db_disasm.c revision 1.2.2.2
      1  1.2.2.2    martin /* $NetBSD: db_disasm.c,v 1.2.2.2 2020/04/13 08:03:27 martin Exp $ */
      2      1.1      matt 
      3      1.2       ryo /*
      4      1.2       ryo  * 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.2       ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.2       ryo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18      1.2       ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19      1.2       ryo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20      1.2       ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21      1.2       ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22      1.2       ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.2       ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24      1.2       ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25      1.2       ryo  * 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.2.2.2    martin __KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.2.2.2 2020/04/13 08:03:27 martin Exp $");
     31      1.2       ryo 
     32      1.2       ryo #include <sys/param.h>
     33      1.2       ryo #include <machine/db_machdep.h>
     34      1.2       ryo #include <ddb/db_interface.h>
     35      1.2       ryo #include <ddb/db_sym.h>
     36      1.2       ryo #include <ddb/db_output.h>
     37      1.2       ryo #include <ddb/db_access.h>
     38  1.2.2.1  christos #include <ddb/db_user.h>
     39      1.2       ryo 
     40      1.2       ryo #include <arch/aarch64/aarch64/disasm.h>
     41      1.2       ryo 
     42      1.2       ryo static uint32_t
     43      1.2       ryo db_disasm_readword(uintptr_t address)
     44      1.2       ryo {
     45      1.2       ryo 	return db_get_value(address, sizeof(uint32_t), false);
     46      1.2       ryo }
     47      1.1      matt 
     48      1.2       ryo static void
     49      1.2       ryo db_disasm_printaddr(uintptr_t address)
     50      1.2       ryo {
     51      1.2       ryo 	db_printf("%lx <", address);
     52      1.2       ryo 	db_printsym((db_addr_t)address, DB_STGY_ANY, db_printf);
     53      1.2       ryo 	db_printf(">");
     54      1.2       ryo }
     55      1.1      matt 
     56      1.2       ryo static const disasm_interface_t db_disasm_interface = {
     57      1.2       ryo 	.di_readword = db_disasm_readword,
     58      1.2       ryo 	.di_printaddr = db_disasm_printaddr,
     59      1.2       ryo 	.di_printf = db_printf
     60      1.2       ryo };
     61      1.1      matt 
     62      1.2       ryo db_addr_t
     63      1.2       ryo db_disasm(db_addr_t loc, bool altfmt)
     64      1.2       ryo {
     65      1.2       ryo 	return disasm(&db_disasm_interface, loc);
     66      1.2       ryo }
     67  1.2.2.1  christos 
     68  1.2.2.1  christos 
     69  1.2.2.1  christos static char *strdisasm_ptr;
     70  1.2.2.1  christos static char strdisasm_buf[256];
     71  1.2.2.1  christos 
     72  1.2.2.1  christos static uint32_t
     73  1.2.2.1  christos strdisasm_readword(uintptr_t address)
     74  1.2.2.1  christos {
     75  1.2.2.1  christos 	return *(uint32_t *)address;
     76  1.2.2.1  christos }
     77  1.2.2.1  christos 
     78  1.2.2.2    martin static void __printflike(1, 2)
     79  1.2.2.1  christos strdisasm_printf(const char *fmt, ...)
     80  1.2.2.1  christos {
     81  1.2.2.1  christos 	va_list ap;
     82  1.2.2.1  christos 	int len;
     83  1.2.2.1  christos 
     84  1.2.2.1  christos 	/* calculation spaces to append a string */
     85  1.2.2.1  christos 	len = strdisasm_buf + sizeof(strdisasm_buf) - strdisasm_ptr;
     86  1.2.2.1  christos 
     87  1.2.2.1  christos 	va_start(ap, fmt);
     88  1.2.2.1  christos 	len = vsnprintf(strdisasm_ptr, len, fmt, ap);
     89  1.2.2.1  christos 	va_end(ap);
     90  1.2.2.1  christos 
     91  1.2.2.1  christos 	strdisasm_ptr += len;
     92  1.2.2.1  christos }
     93  1.2.2.1  christos 
     94  1.2.2.1  christos static void
     95  1.2.2.1  christos strdisasm_printaddr(uintptr_t address)
     96  1.2.2.1  christos {
     97  1.2.2.1  christos 	strdisasm_printf("0x%lx", address);
     98  1.2.2.1  christos }
     99  1.2.2.1  christos 
    100  1.2.2.1  christos static const disasm_interface_t strdisasm_interface = {
    101  1.2.2.1  christos 	.di_readword = strdisasm_readword,
    102  1.2.2.1  christos 	.di_printaddr = strdisasm_printaddr,
    103  1.2.2.1  christos 	.di_printf = strdisasm_printf
    104  1.2.2.1  christos };
    105  1.2.2.1  christos 
    106  1.2.2.1  christos const char *
    107  1.2.2.1  christos strdisasm(vaddr_t pc)
    108  1.2.2.1  christos {
    109  1.2.2.1  christos 	char *p;
    110  1.2.2.1  christos 
    111  1.2.2.1  christos 	strdisasm_ptr = strdisasm_buf;
    112  1.2.2.1  christos 	disasm(&strdisasm_interface, (db_addr_t)pc);
    113  1.2.2.1  christos 
    114  1.2.2.1  christos 	/* replace tab to space, and chomp '\n' */
    115  1.2.2.1  christos 	for (p = strdisasm_buf; *p != '\0'; p++) {
    116  1.2.2.1  christos 		if (*p == '\t')
    117  1.2.2.1  christos 			*p = ' ';
    118  1.2.2.1  christos 	}
    119  1.2.2.1  christos 	if ((p > strdisasm_buf) && (p[-1] == '\n'))
    120  1.2.2.1  christos 		p[-1] = '\0';
    121  1.2.2.1  christos 
    122  1.2.2.1  christos 	return strdisasm_buf;
    123  1.2.2.1  christos }
    124  1.2.2.1  christos 
    125  1.2.2.1  christos /*
    126  1.2.2.1  christos  * disassemble aarch32 insns?
    127  1.2.2.1  christos  */
    128  1.2.2.1  christos const char *
    129  1.2.2.1  christos strdisasm_aarch32(vaddr_t pc)
    130  1.2.2.1  christos {
    131  1.2.2.1  christos 	uint32_t insn = *(uint32_t *)pc;
    132  1.2.2.1  christos 
    133  1.2.2.1  christos 	/* not supported any aarch32 insns yet... */
    134  1.2.2.1  christos 	snprintf(strdisasm_buf, sizeof(strdisasm_buf), ".insn 0x%08x", insn);
    135  1.2.2.1  christos 
    136  1.2.2.1  christos 	return strdisasm_buf;
    137  1.2.2.1  christos }
    138  1.2.2.1  christos 
    139