tstDisasm-2.cpp revision b82fef5dcbe08cae8ce37a09825adc4c6b0bf762
/* $Id$ */
/** @file
* Testcase - Generic Disassembler Tool.
*/
/*
* Copyright (C) 2008 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef enum { kAsmStyle_Default, kAsmStyle_yasm, kAsmStyle_masm, kAsmStyle_gas, kAsmStyle_invalid } ASMSTYLE;
typedef struct MYDISSTATE
{
bool fUndefOp; /**< Whether the current instruction is really an undefined opcode.*/
int rc; /**< Set if we hit EOF. */
} MYDISSTATE;
typedef MYDISSTATE *PMYDISSTATE;
/**
* Default style.
*
* @param pState The disassembler state.
*/
{
}
/**
* Yasm style.
*
* @param pState The disassembler state.
*/
{
char szTmp[256];
#if 0
/* a very quick hack. */
*psz = '\0';
if (psz)
*pszEnd++ = ' ';
*pszEnd = '\0';
#else
while (cch < 71)
#endif
}
/**
* Masm style.
*
* @param pState The disassembler state.
*/
{
}
/**
* This is a temporary workaround for catching a few illegal opcodes
* that the disassembler is currently letting thru, just enough to make
* the assemblers happy.
*
* We're too close to a release to dare mess with these things now as
* they may consequences for performance and let alone introduce bugs.
*
* @returns true if it's valid. false if it isn't.
*
* @param pCpu The disassembler output.
*/
{
{
/* These doesn't take memory operands. */
case OP_MOV_CR:
case OP_MOV_DR:
case OP_MOV_TR:
return false;
break;
/* The 0x8f /0 variant of this instruction doesn't get its /r value verified. */
case OP_POP:
return false;
break;
/* The 0xc6 /0 and 0xc7 /0 variants of this instruction don't get their /r values verified. */
case OP_MOV:
return false;
break;
default:
break;
}
return true;
}
/**
* Callback for reading bytes.
*
* @todo This should check that the disassembler doesn't do unnecessary reads,
* however the current doesn't do this and is just complicated...
*/
static DECLCALLBACK(int) MyDisasInstrRead(RTUINTPTR uSrcAddr, uint8_t *pbDst, uint32_t cbRead, void *pvDisCpu)
{
{
/*
* Straight forward reading.
*/
if (cbRead == 1)
{
}
else
{
}
}
else
{
/*
* Jumping up the stream.
* This occurs when the byte sequence is added to the output string.
*/
if (offReq64 < 32)
{
{
return VERR_EOF;
}
/* reset the stream. */
/* skip ahead. */
/* do the reading. */
{
}
else
{
{
}
return VERR_EOF;
}
}
else
{
return VERR_INTERNAL_ERROR;
}
}
return VINF_SUCCESS;
}
/**
* Disassembles a block of memory.
*
* @returns VBox status code.
* @param argv0 Program name (for errors and warnings).
* @param enmCpuMode The cpu mode to disassemble in.
* @param uAddress The address we're starting to disassemble at.
* @param uHighlightAddr The address of the instruction that should be
* highlighted. Pass UINT64_MAX to keep quiet.
* @param pbFile Where to start disassemble.
* @param cbFile How much to disassemble.
* @param enmStyle The assembly output style.
* @param fListing Whether to print in a listing like mode.
* @param enmUndefOp How to deal with undefined opcodes.
*/
{
/*
* Initialize the CPU context.
*/
switch (enmStyle)
{
case kAsmStyle_Default:
break;
case kAsmStyle_yasm:
break;
case kAsmStyle_masm:
break;
default:
}
/*
* The loop.
*/
int rcRet = VINF_SUCCESS;
{
/*
* Disassemble it.
*/
if ( RT_SUCCESS(rc)
|| ( ( rc == VERR_DIS_INVALID_OPCODE
|| rc == VERR_DIS_GEN_FAILURE)
{
|| rc == VERR_DIS_GEN_FAILURE
{
RTPrintf(" db");
{
uint8_t b;
}
}
{
RTPrintf("%s: error at %#RX64: unexpected valid instruction (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->opcode);
}
{
RTPrintf("%s: error at %#RX64: undefined opcode (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->opcode);
}
else
{
/* Use db for odd encodings that we can't make the assembler use. */
{
RTPrintf(" db");
{
uint8_t b;
}
RTPrintf(" ; ");
}
}
}
else
{
else
{
if (rcRet == VINF_SUCCESS)
break;
}
}
/* Highlight this instruction? */
RTPrintf("; ^^^^^^^^^^^^^^^^^^^^^\n");
/* next */
}
return rcRet;
}
/**
* Converts a hex char to a number.
*
* @returns 0..15 on success, -1 on failure.
* @param ch The character.
*/
static int HexDigitToNum(char ch)
{
switch (ch)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A':
case 'a': return 0xa;
case 'B':
case 'b': return 0xb;
case 'C':
case 'c': return 0xc;
case 'D':
case 'd': return 0xd;
case 'E':
case 'e': return 0xe;
case 'F':
case 'f': return 0xf;
default:
return -1;
}
}
/**
* Prints usage info.
*
* @returns 1.
* @param argv0 The program name.
*/
{
"usage: %s [options] <file1> [file2..fileN]\n"
" or: %s [options] <-x|--hex-bytes> <hex byte> [more hex..]\n"
" or: %s <--help|-h>\n"
"\n"
"Options:\n"
" --address|-a <address>\n"
" The base address. Default: 0\n"
" --max-bytes|-b <bytes>\n"
" The maximum number of bytes to disassemble. Default: 1GB\n"
" --cpumode|-c <16|32|64>\n"
" The cpu mode. Default: 32\n"
" --listing|-l, --no-listing|-L\n"
" Enables or disables listing mode. Default: --no-listing\n"
" --offset|-o <offset>\n"
" The file offset at which to start disassembling. Default: 0\n"
" --style|-s <default|yasm|masm>\n"
" The assembly output style. Default: default\n"
" --undef-op|-u <fail|all|db>\n"
" How to treat undefined opcodes. Default: fail\n"
return 1;
}
{
/* options */
bool fListing = true;
bool fHexBytes = false;
/*
* Parse arguments.
*/
static const RTGETOPTDEF g_aOptions[] =
{
};
int ch;
RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
&& ch != VINF_GETOPT_NOT_OPTION)
{
switch (ch)
{
case 'a':
break;
case 'b':
break;
case 'c':
else
{
return 1;
}
break;
case 'h':
case 'l':
fListing = true;
break;
case 'L':
fListing = false;
break;
case 'o':
break;
case 's':
{
return 1;
}
else
{
return 1;
}
break;
case 'u':
else
{
RTStrmPrintf(g_pStdErr, "%s: unknown undefined opcode handling method: %s\n", argv0, ValueUnion.psz);
return 1;
}
break;
case 'x':
fHexBytes = true;
break;
case 'V':
RTPrintf("$Revision: $\n");
return 0;
default:
}
}
int rc = VINF_SUCCESS;
if (fHexBytes)
{
/*
* Convert the remaining arguments from a hex byte string into
* a buffer that we disassemble.
*/
{
char ch2;
while (*psz)
{
/** @todo this stuff belongs in IPRT, same stuff as mac address reading. Could be reused for IPv6 with a different item size.*/
/* skip white space, and for the benefit of linux panics '<' and '>'. */
{
if (ch2 == '<')
psz++;
}
if (!ch2)
break;
/* one digit followed by a space or EOS, or two digits. */
if (iNum == -1)
return 1;
{
if (iDigit == -1)
return 1;
}
/* add the byte */
{
if (!pb)
{
return 1;
}
}
}
}
/*
* Disassemble it.
*/
rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, pb, cb, enmStyle, fListing, enmUndefOp);
}
else
{
/*
* Process the files.
*/
{
/*
* Read the file into memory.
*/
void *pvFile;
if (RT_FAILURE(rc))
{
break;
}
/*
* Disassemble it.
*/
rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, (uint8_t *)pvFile, cbFile, enmStyle, fListing, enmUndefOp);
if (RT_FAILURE(rc))
break;
}
}
}