tstDisasm-2.cpp revision 7bf0220c3332700233120b513c9b4ba20a0caa9b
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Testcase - Generic Disassembler Tool.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * available from http://www.virtualbox.org. This file is free software;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * General Public License (GPL) as published by the Free Software
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * additional information or have any questions.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync/*******************************************************************************
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync* Header Files *
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync*******************************************************************************/
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync/*******************************************************************************
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync* Structures and Typedefs *
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync*******************************************************************************/
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsynctypedef enum { kAsmStyle_Default, kAsmStyle_yasm, kAsmStyle_masm, kAsmStyle_invalid } ASMSTYLE;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsynctypedef struct MYDISSTATE
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint64_t uAddress; /**< The current instruction address. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint8_t *pbInstr; /**< The current instruction (pointer). */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint32_t cbInstr; /**< The size of the current instruction. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync size_t cbLeft; /**< The number of bytes left. (read) */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint64_t uNextAddr; /**< The address of the next byte. (read) */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync char szLine[256]; /**< The disassembler text output. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Default style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasDefaultFormatter(PMYDISSTATE pState)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Yasm style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasYasmFormatter(PMYDISSTATE pState)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("yasm not implemented: %s", pState->szLine);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Masm style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasMasmFormatter(PMYDISSTATE pState)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("masm not implemented: %s", pState->szLine);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Callback for reading bytes.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic DECLCALLBACK(int) MyDisasInstrRead(RTUINTPTR uSrcAddr, uint8_t *pbDst, uint32_t cbRead, void *pvDisCpu)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Straight forward reading.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Jumping up the stream.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* reset the stream. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* skip ahead. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* do the reading. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "Reading before current instruction!\n");
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassembles a block of memory.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @returns VBox status code.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param argv0 Program name (for errors and warnings).
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param enmCpuMode The cpu mode to disassemble in.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param uAddress The address we're starting to disassemble at.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pbFile Where to start disassemble.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param cbFile How much to disassemble.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param enmStyle The assembly output style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param fListing Whether to print in a listing like mode.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic int MyDisasmBlock(const char *argv0, DISCPUMODE enmCpuMode, uint64_t uAddress, uint8_t *pbFile, size_t cbFile,
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Initialize the CPU context.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * The loop.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassemble it.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync int rc = DISInstr(&State.Cpu, State.uAddress, 0, &State.cbInstr, State.szLine);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: read beyond the end (%Rrc)\n", argv0, State.uAddress, rc);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d\n", argv0, State.uAddress, rc, State.cbInstr);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d!\n", argv0, State.uAddress, rc, State.cbInstr);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Prints usage info.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @returns 1.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param argv0 The program name.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync"usage: %s [options] <file1> [file2..fileN]\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" or: %s <--help|-h>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync"Options:\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --address|-a <address>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The base address. Default: 0\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --max-bytes|-b <bytes>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The maximum number of bytes to disassemble. Default: 1GB\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --cpumode|-c <16|32|64>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The cpu mode. Default: 32\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --listing|-l, --no-listing|-L\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" Enables or disables listing mode. Default: --no-listing\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --offset|-o <offset>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The file offset at which to start disassembling. Default: 0\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --style|-s <default|yasm|masm>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The assembly output style. Default: default\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* options */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync bool fListing = true;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Parse arguments.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync while ((ch = RTGetOpt(argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), &iArg, &ValueUnion)))
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "%s: Invalid CPU mode value %RU32\n", argv[0], ValueUnion.u32);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "%s: masm style isn't implemented yet\n", argv[0]);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "%s: unknown assembly style: %s\n", argv[0], ValueUnion.psz);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "%s: syntax error: %Rrc\n", argv[0], ch);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Process the files.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Read the file into memory.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync rc = RTFileReadAllEx(argv[iArg], off, cbMax, 0, &pvFile, &cbFile);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "%s: %s: %Rrc\n", argv[0], argv[iArg], rc);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassemble it.