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