1 /// Exceptions and handling of Capstone-internal errors 2 module capstone.error; 3 4 import std.conv: to; 5 import std.format: format; 6 import std..string: fromStringz; 7 8 import capstone.internal.api: cs_strerror; 9 10 /** Original error codes of the wrapped Capstone library 11 12 Note that some cannot occur by design of the bindings, e.g. using an invalid handle to the Capstone engine. 13 */ 14 enum ErrorCode { 15 Ok = 0, /// Not an error (will not occur in an exception) 16 OutOfMemory, /// Ran out of memory, e.g. when disassembling a long byte-stream 17 UnsupportedArchitecture, /// Requested an unsupported architecture, e.g. when compiled without corresponding support 18 InvalidHandle, /// Using an invalid handle to a Capstone engine instance (cannot happen) 19 InvalidCshArgument, /// Using an invalid handle as argument (cannot happen) 20 InvalidMode, /// Requested invalid/unsupported mode, e.g. `Mode.bigEndian` for `Arch.x86` 21 InvalidOption, /// Using invalid/unsupported option 22 UnavailableInstructionDetail, /// Trying to access unavailable instruction detail 23 UninitializedDynamicMemoryManagement, /// Dynamic memory management uninitialised (cannot happen - not implemented yet) 24 UnsupportedVersion, /// Mismatch of bindings and library version 25 IrrelevantDataAccessInDietEngine, /// Accessing data that is unavailable/invalid in diet mode 26 IrrelevantDataAccessInSkipdataMode, /// Accessing data that is irrelevant for "data" instruction in SKIPDATA mode (cannot happen) 27 UnsupportedATnTSyntax, /// Requesting unsupported AT&T syntax (opt-out at compile time) 28 UnsupportedIntelSyntax, /// Requesting unsupported Intel syntax (opt-out at compile time) 29 UnsupportedMasmSyntax, /// Requesting unsupported MASM syntax (opt-out at compile time) 30 } 31 32 /// Exception thrown on errors in the wrapped Capstone library 33 class CapstoneException : Exception { 34 /// Denotes the kind of error 35 const ErrorCode errCode; 36 37 package this(string msg, in ErrorCode errno, string file = __FILE__, size_t line = __LINE__, Throwable next = null){ 38 super(msg, file, line, next); 39 this.errCode = errno; 40 } 41 42 package this(in ErrorCode errno, string file = __FILE__, size_t line = __LINE__, Throwable next = null){ 43 const msg = format!"Capstone Error %d: %s"(errno, cs_strerror(errno.to!int).fromStringz); 44 super(msg, file, line, next); 45 this.errCode = errno; 46 } 47 } 48 49 /// Handles Capstone's error codes and raises corresponding exceptions 50 package void checkErrno(in int errno, string file = __FILE__, size_t line = __LINE__, Throwable next = null){ 51 auto code = errno.to!ErrorCode; 52 if(code != ErrorCode.Ok) 53 throw new CapstoneException(code, file, line, next); 54 }