1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <mdb/mdb_disasm_impl.h> 29 #include <mdb/mdb_modapi.h> 30 #include <mdb/mdb_string.h> 31 #include <mdb/mdb_debug.h> 32 #include <mdb/mdb_err.h> 33 #include <mdb/mdb_nv.h> 34 #include <mdb/mdb.h> 35 36 #include <libdisasm.h> 37 38 int 39 mdb_dis_select(const char *name) 40 { 41 mdb_var_t *v = mdb_nv_lookup(&mdb.m_disasms, name); 42 43 if (v != NULL) { 44 mdb.m_disasm = mdb_nv_get_cookie(v); 45 return (0); 46 } 47 48 if (mdb.m_target == NULL) { 49 if (mdb.m_defdisasm != NULL) 50 strfree(mdb.m_defdisasm); 51 mdb.m_defdisasm = strdup(name); 52 return (0); 53 } 54 55 return (set_errno(EMDB_NODIS)); 56 } 57 58 mdb_disasm_t * 59 mdb_dis_create(mdb_dis_ctor_f *ctor) 60 { 61 mdb_disasm_t *dp = mdb_zalloc(sizeof (mdb_disasm_t), UM_SLEEP); 62 63 if ((dp->dis_module = mdb.m_lmod) == NULL) 64 dp->dis_module = &mdb.m_rmod; 65 66 if (ctor(dp) == 0) { 67 mdb_var_t *v = mdb_nv_lookup(&mdb.m_disasms, dp->dis_name); 68 69 if (v != NULL) { 70 dp->dis_ops->dis_destroy(dp); 71 mdb_free(dp, sizeof (mdb_disasm_t)); 72 (void) set_errno(EMDB_DISEXISTS); 73 return (NULL); 74 } 75 76 (void) mdb_nv_insert(&mdb.m_disasms, dp->dis_name, NULL, 77 (uintptr_t)dp, MDB_NV_RDONLY | MDB_NV_SILENT); 78 79 if (mdb.m_disasm == NULL) { 80 mdb.m_disasm = dp; 81 } else if (mdb.m_defdisasm != NULL && 82 strcmp(mdb.m_defdisasm, dp->dis_name) == 0) { 83 mdb.m_disasm = dp; 84 strfree(mdb.m_defdisasm); 85 mdb.m_defdisasm = NULL; 86 } 87 88 return (dp); 89 } 90 91 mdb_free(dp, sizeof (mdb_disasm_t)); 92 return (NULL); 93 } 94 95 void 96 mdb_dis_destroy(mdb_disasm_t *dp) 97 { 98 mdb_var_t *v = mdb_nv_lookup(&mdb.m_disasms, dp->dis_name); 99 100 ASSERT(v != NULL); 101 mdb_nv_remove(&mdb.m_disasms, v); 102 dp->dis_ops->dis_destroy(dp); 103 mdb_free(dp, sizeof (mdb_disasm_t)); 104 105 if (mdb.m_disasm == dp) 106 (void) mdb_dis_select("default"); 107 } 108 109 mdb_tgt_addr_t 110 mdb_dis_ins2str(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 111 char *buf, size_t len, mdb_tgt_addr_t addr) 112 { 113 return (dp->dis_ops->dis_ins2str(dp, t, as, buf, len, addr)); 114 } 115 116 mdb_tgt_addr_t 117 mdb_dis_previns(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 118 mdb_tgt_addr_t addr, uint_t n) 119 { 120 return (dp->dis_ops->dis_previns(dp, t, as, addr, n)); 121 } 122 123 mdb_tgt_addr_t 124 mdb_dis_nextins(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 125 mdb_tgt_addr_t addr) 126 { 127 return (dp->dis_ops->dis_nextins(dp, t, as, addr)); 128 } 129 130 /*ARGSUSED*/ 131 int 132 cmd_dismode(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 133 { 134 if ((flags & DCMD_ADDRSPEC) || argc > 1) 135 return (DCMD_USAGE); 136 137 if (argc != 0) { 138 const char *name; 139 140 if (argv->a_type == MDB_TYPE_STRING) 141 name = argv->a_un.a_str; 142 else 143 name = numtostr(argv->a_un.a_val, 10, NTOS_UNSIGNED); 144 145 if (mdb_dis_select(name) == -1) { 146 warn("failed to set disassembly mode"); 147 return (DCMD_ERR); 148 } 149 } 150 151 mdb_printf("disassembly mode is %s (%s)\n", 152 mdb.m_disasm->dis_name, mdb.m_disasm->dis_desc); 153 154 return (DCMD_OK); 155 } 156 157 /*ARGSUSED*/ 158 static int 159 print_dis(mdb_var_t *v, void *ignore) 160 { 161 mdb_disasm_t *dp = mdb_nv_get_cookie(v); 162 163 mdb_printf("%-24s - %s\n", dp->dis_name, dp->dis_desc); 164 return (0); 165 } 166 167 /*ARGSUSED*/ 168 int 169 cmd_disasms(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 170 { 171 if ((flags & DCMD_ADDRSPEC) || argc != 0) 172 return (DCMD_USAGE); 173 174 mdb_nv_sort_iter(&mdb.m_disasms, print_dis, NULL, UM_SLEEP | UM_GC); 175 return (DCMD_OK); 176 } 177 178 /* 179 * Generic libdisasm disassembler interfaces. 180 */ 181 182 #define DISBUFSZ 64 183 184 /* 185 * Internal structure used by the read and lookup routines. 186 */ 187 typedef struct dis_buf { 188 mdb_tgt_t *db_tgt; 189 mdb_tgt_as_t db_as; 190 mdb_tgt_addr_t db_addr; 191 mdb_tgt_addr_t db_nextaddr; 192 uchar_t db_buf[DISBUFSZ]; 193 ssize_t db_bufsize; 194 } dis_buf_t; 195 196 /* 197 * Disassembler support routine for lookup up an address. Rely on mdb's "%a" 198 * qualifier to convert the address to a symbol. 199 */ 200 /*ARGSUSED*/ 201 static int 202 libdisasm_lookup(void *data, uint64_t addr, char *buf, size_t buflen, 203 uint64_t *start, size_t *len) 204 { 205 if (buf != NULL) { 206 GElf_Sym sym; 207 208 #ifdef __sparc 209 uint32_t instr[3]; 210 uint32_t dtrace_id; 211 212 /* 213 * On SPARC, DTrace FBT trampoline entries have a sethi/or pair 214 * that indicates the dtrace probe id; this may appear as the 215 * first two instructions or one instruction into the 216 * trampoline. 217 */ 218 if (mdb_vread(instr, sizeof (instr), (uintptr_t)addr) == 219 sizeof (instr)) { 220 if ((instr[0] & 0xfffc0000) == 0x11000000 && 221 (instr[1] & 0xffffe000) == 0x90122000) { 222 dtrace_id = (instr[0] << 10) | 223 (instr[1] & 0x1fff); 224 (void) mdb_snprintf(buf, sizeof (buf), "dt=%#x", 225 dtrace_id); 226 goto out; 227 } else if ((instr[1] & 0xfffc0000) == 0x11000000 && 228 (instr[2] & 0xffffe000) == 0x90122000) { 229 dtrace_id = (instr[1] << 10) | 230 (instr[2] & 0x1fff); 231 (void) mdb_snprintf(buf, sizeof (buf), "dt=%#x", 232 dtrace_id); 233 goto out; 234 } 235 } 236 #endif 237 if (mdb_lookup_by_addr((uintptr_t)addr, MDB_SYM_FUZZY, 238 buf, buflen, &sym) < 0) { 239 if (buflen > 0) 240 *buf = '\0'; 241 return (-1); 242 } 243 } 244 245 #ifdef __sparc 246 out: 247 #endif 248 if (start != NULL || len != NULL) { 249 GElf_Sym sym; 250 char c; 251 252 if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, &c, 1, &sym) < 0) 253 return (-1); 254 255 if (start != NULL) 256 *start = sym.st_value; 257 if (len != NULL) 258 *len = sym.st_size; 259 } 260 261 return (0); 262 } 263 264 /* 265 * Disassembler support routine for reading from the target. Rather than having 266 * to read one byte at a time, we read from the address space in chunks. If the 267 * current address doesn't lie within our buffer range, we read in the chunk 268 * starting from the given address. 269 */ 270 static int 271 libdisasm_read(void *data, uint64_t pc, void *buf, size_t buflen) 272 { 273 dis_buf_t *db = data; 274 size_t offset; 275 size_t len; 276 277 if (pc - db->db_addr >= db->db_bufsize) { 278 if ((db->db_bufsize = mdb_tgt_aread(db->db_tgt, db->db_as, 279 db->db_buf, sizeof (db->db_buf), pc)) == -1) 280 return (-1); 281 db->db_addr = pc; 282 } 283 284 offset = pc - db->db_addr; 285 286 len = MIN(buflen, db->db_bufsize - offset); 287 288 memcpy(buf, (char *)db->db_buf + offset, len); 289 db->db_nextaddr = pc + len; 290 291 return (len); 292 } 293 294 static mdb_tgt_addr_t 295 libdisasm_ins2str(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 296 char *buf, size_t len, mdb_tgt_addr_t pc) 297 { 298 dis_handle_t *dhp = dp->dis_data; 299 dis_buf_t db = { 0 }; 300 301 /* 302 * Set the libdisasm data to point to our buffer. This will be 303 * passed as the first argument to the lookup and read functions. 304 */ 305 db.db_tgt = t; 306 db.db_as = as; 307 308 dis_set_data(dhp, &db); 309 310 /* 311 * Attempt to disassemble the instruction 312 */ 313 if (dis_disassemble(dhp, pc, buf, len) != 0) 314 (void) mdb_snprintf(buf, len, 315 "***ERROR--unknown op code***"); 316 317 /* 318 * Return the updated location 319 */ 320 return (db.db_nextaddr); 321 } 322 323 static mdb_tgt_addr_t 324 libdisasm_previns(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 325 mdb_tgt_addr_t pc, uint_t n) 326 { 327 dis_handle_t *dhp = dp->dis_data; 328 dis_buf_t db = { 0 }; 329 330 /* 331 * Set the libdisasm data to point to our buffer. This will be 332 * passed as the first argument to the lookup and read functions. 333 */ 334 db.db_tgt = t; 335 db.db_as = as; 336 337 dis_set_data(dhp, &db); 338 339 return (dis_previnstr(dhp, pc, n)); 340 } 341 342 /*ARGSUSED*/ 343 static mdb_tgt_addr_t 344 libdisasm_nextins(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 345 mdb_tgt_addr_t pc) 346 { 347 mdb_tgt_addr_t npc; 348 char c; 349 350 if ((npc = libdisasm_ins2str(dp, t, as, &c, 1, pc)) == pc) 351 return (pc); 352 353 /* 354 * Probe the address to make sure we can read something from it - we 355 * want the address we return to actually contain something. 356 */ 357 if (mdb_tgt_aread(t, as, &c, 1, npc) != 1) 358 return (pc); 359 360 return (npc); 361 } 362 363 static void 364 libdisasm_destroy(mdb_disasm_t *dp) 365 { 366 dis_handle_t *dhp = dp->dis_data; 367 368 dis_handle_destroy(dhp); 369 } 370 371 static const mdb_dis_ops_t libdisasm_ops = { 372 libdisasm_destroy, 373 libdisasm_ins2str, 374 libdisasm_previns, 375 libdisasm_nextins 376 }; 377 378 /* 379 * Generic function for creating a libdisasm-backed disassembler. Creates an 380 * MDB disassembler with the given name backed by libdis with the given flags. 381 */ 382 static int 383 libdisasm_create(mdb_disasm_t *dp, const char *name, 384 const char *desc, int flags) 385 { 386 if ((dp->dis_data = dis_handle_create(flags, NULL, libdisasm_lookup, 387 libdisasm_read)) == NULL) 388 return (-1); 389 390 dp->dis_name = name; 391 dp->dis_ops = &libdisasm_ops; 392 dp->dis_desc = desc; 393 394 return (0); 395 } 396 397 398 #if defined(__i386) || defined(__amd64) 399 static int 400 ia32_create(mdb_disasm_t *dp) 401 { 402 return (libdisasm_create(dp, 403 "ia32", 404 "Intel 32-bit disassembler", 405 DIS_X86_SIZE32)); 406 } 407 #endif 408 409 #if defined(__amd64) 410 static int 411 amd64_create(mdb_disasm_t *dp) 412 { 413 return (libdisasm_create(dp, 414 "amd64", 415 "AMD64 and IA32e 64-bit disassembler", 416 DIS_X86_SIZE64)); 417 } 418 #endif 419 420 #if defined(__sparc) 421 static int 422 sparc1_create(mdb_disasm_t *dp) 423 { 424 return (libdisasm_create(dp, 425 "1", 426 "SPARC-v8 disassembler", 427 DIS_SPARC_V8)); 428 } 429 430 static int 431 sparc2_create(mdb_disasm_t *dp) 432 { 433 return (libdisasm_create(dp, 434 "2", 435 "SPARC-v9 disassembler", 436 DIS_SPARC_V9)); 437 } 438 439 static int 440 sparc4_create(mdb_disasm_t *dp) 441 { 442 return (libdisasm_create(dp, 443 "4", 444 "UltraSPARC1-v9 disassembler", 445 DIS_SPARC_V9 | DIS_SPARC_V9_SGI)); 446 } 447 448 static int 449 sparcv8_create(mdb_disasm_t *dp) 450 { 451 return (libdisasm_create(dp, 452 "v8", 453 "SPARC-v8 disassembler", 454 DIS_SPARC_V8)); 455 } 456 457 static int 458 sparcv9_create(mdb_disasm_t *dp) 459 { 460 return (libdisasm_create(dp, 461 "v9", 462 "SPARC-v9 disassembler", 463 DIS_SPARC_V9)); 464 } 465 466 static int 467 sparcv9plus_create(mdb_disasm_t *dp) 468 { 469 return (libdisasm_create(dp, 470 "v9plus", 471 "UltraSPARC1-v9 disassembler", 472 DIS_SPARC_V9 | DIS_SPARC_V9_SGI)); 473 } 474 #endif 475 476 /*ARGSUSED*/ 477 static void 478 defdis_destroy(mdb_disasm_t *dp) 479 { 480 /* Nothing to do here */ 481 } 482 483 /*ARGSUSED*/ 484 static mdb_tgt_addr_t 485 defdis_ins2str(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 486 char *buf, size_t len, mdb_tgt_addr_t addr) 487 { 488 return (addr); 489 } 490 491 /*ARGSUSED*/ 492 static mdb_tgt_addr_t 493 defdis_previns(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 494 mdb_tgt_addr_t addr, uint_t n) 495 { 496 return (addr); 497 } 498 499 /*ARGSUSED*/ 500 static mdb_tgt_addr_t 501 defdis_nextins(mdb_disasm_t *dp, mdb_tgt_t *t, mdb_tgt_as_t as, 502 mdb_tgt_addr_t addr) 503 { 504 return (addr); 505 } 506 507 static const mdb_dis_ops_t defdis_ops = { 508 defdis_destroy, 509 defdis_ins2str, 510 defdis_previns, 511 defdis_nextins 512 }; 513 514 static int 515 defdis_create(mdb_disasm_t *dp) 516 { 517 dp->dis_name = "default"; 518 dp->dis_desc = "default no-op disassembler"; 519 dp->dis_ops = &defdis_ops; 520 521 return (0); 522 } 523 524 mdb_dis_ctor_f *const mdb_dis_builtins[] = { 525 defdis_create, 526 #if defined(__amd64) 527 ia32_create, 528 amd64_create, 529 #elif defined(__i386) 530 ia32_create, 531 #elif defined(__sparc) 532 sparc1_create, 533 sparc2_create, 534 sparc4_create, 535 sparcv8_create, 536 sparcv9_create, 537 sparcv9plus_create, 538 #endif 539 NULL 540 }; 541