Catching opcode 0xCC as an exception
Asked Answered
J

2

2

Say a C program might trigger an exception of opcode 0xCC
How can I catch it?

I tried:

__try   
    {

  ...some code

    }

__except(GetExceptionCode()==EXCEPTION_EXECUTE_HANDLER)     
{ 
     _tprintf(_T("Error\n"),i);

      return 0;
}

This is not working for me. What am I doing wrong? Thanks!

Jansenism answered 17/5, 2011 at 17:34 Comment(3)
C doesn't have a built in exception handler, so you should specify what you are using to provide exceptions. Is this Win32 exception handling?Calceiform
@Calceiform yes it is, e.g. here's the GetExceptionCode documentation. Not sure what tags to add, though.Keystone
That's not an API, it's a modified language.Copier
N
4

You're not checking for the right exception code.

int 3 throws EXCEPTION_SINGLE_STEP.

You handle it this way :

__try 
{
    // some code that might cause an int3
}
__except(GetExceptionCode() == EXCEPTION_SINGLE_STEP ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
{
    // error handling of int3
}

EDIT: Please note that a code running with a debugger attached will not see the exception, because the debugger handles it and clears it before passing the hand back to the code.

Near answered 17/5, 2011 at 17:50 Comment(0)
S
1

Since this is a win32 program you can always set up your own "crashhandler" Here is a rough snip from one of my programs. Use at own risk ;)

LONG WINAPI crashhandler(struct _EXCEPTION_POINTERS* ExceptionInfo); 

SetErrorMode(SEM_FAILCRITICALERRORS);
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)crashhandler);

LONG WINAPI crashhandler(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
  DWORD exceptioncode;
  char  exceptiondesc[8192];

  exceptioncode = ExceptionInfo->ExceptionRecord->ExceptionCode;

  switch(exceptioncode)
  {
    case EXCEPTION_ACCESS_VIOLATION:
      strcpy(exceptiondesc,"EXCEPTION_ACCESS_VIOLATION:");
      break;
    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
      strcpy(exceptiondesc,"EXCEPTION_ARRAY_BOUNDS_EXCEEDED:");
      break;
    case EXCEPTION_BREAKPOINT:
      strcpy(exceptiondesc,"EXCEPTION_BREAKPOINT:");
      break;
    case EXCEPTION_DATATYPE_MISALIGNMENT:
      strcpy(exceptiondesc,"EXCEPTION_DATATYPE_MISALIGNMENT:");
      break;
    case EXCEPTION_FLT_DENORMAL_OPERAND:
      strcpy(exceptiondesc,"EXCEPTION_FLT_DENORMAL_OPERAND:");
      break;
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
      strcpy(exceptiondesc,"EXCEPTION_FLT_DIVIDE_BY_ZERO:");
      break;
    case EXCEPTION_FLT_INEXACT_RESULT:
      strcpy(exceptiondesc,"EXCEPTION_FLT_INEXACT_RESULT:");
      break;
    case EXCEPTION_FLT_INVALID_OPERATION:
      strcpy(exceptiondesc,"EXCEPTION_FLT_INVALID_OPERATION:");
      break;
    case EXCEPTION_FLT_OVERFLOW:
      strcpy(exceptiondesc,"EXCEPTION_FLT_OVERFLOW:");
      break;
    case EXCEPTION_FLT_STACK_CHECK:
      strcpy(exceptiondesc,"EXCEPTION_FLT_STACK_CHECK:");
      break;
    case EXCEPTION_FLT_UNDERFLOW:
      strcpy(exceptiondesc,"EXCEPTION_FLT_UNDERFLOW:");
      break;
    case EXCEPTION_ILLEGAL_INSTRUCTION:
      strcpy(exceptiondesc,"EXCEPTION_ILLEGAL_INSTRUCTION:");
      break;
    case EXCEPTION_IN_PAGE_ERROR:
      strcpy(exceptiondesc,"EXCEPTION_IN_PAGE_ERROR:");
      break;
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
      strcpy(exceptiondesc,"EXCEPTION_INT_DIVIDE_BY_ZERO:");
      break;
    case EXCEPTION_INT_OVERFLOW:
      strcpy(exceptiondesc,"EXCEPTION_INT_OVERFLOW:");
      break;
    case EXCEPTION_INVALID_DISPOSITION:
      strcpy(exceptiondesc,"EXCEPTION_INVALID_DISPOSITION:");
      break;
    case EXCEPTION_NONCONTINUABLE_EXCEPTION:
      strcpy(exceptiondesc,"EXCEPTION_NONCONTINUABLE_EXCEPTION:");
      break;
    case EXCEPTION_PRIV_INSTRUCTION:
      strcpy(exceptiondesc,"EXCEPTION_PRIV_INSTRUCTION:");
      break;
    case EXCEPTION_SINGLE_STEP:
      strcpy(exceptiondesc,"EXCEPTION_SINGLE_STEP:");
      break;
    case EXCEPTION_STACK_OVERFLOW:
      strcpy(exceptiondesc,"EXCEPTION_STACK_OVERFLOW:");
      break;
    default:
      strcpy(exceptiondesc,"Unknown exception\n");
      break;

  }

  printf("** --- CRASH BANG BOOM ---\n");
  printf("** Exception             : 0x%08x\n",exceptioncode);
  printf("** Exception description : %s\n"    ,exceptiondesc);
  printf("** Exception flags       : 0x%08x\n",ExceptionInfo->ExceptionRecord->ExceptionFlags);
  printf("** Exception address     : 0x%08x\n",ExceptionInfo->ExceptionRecord->ExceptionAddress);
  printf("**\n");

  //return EXCEPTION_EXECUTE_HANDLER;
  //return EXCEPTION_CONTINUE_EXECUTION;
  //return EXCEPTION_CONTINUE_SEARCH;

  return 0;
}
Suomi answered 27/5, 2011 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.