Commit 0cabf5ca authored by sagnowski's avatar sagnowski
Browse files

Switch to using macros for stack printing

parent ced44222
Loading
Loading
Loading
Loading
+74 −65
Original line number Diff line number Diff line
@@ -152,14 +152,76 @@ static Word16 saturate (Word32 L_var1);
 |___________________________________________________________________________|
*/
#ifdef BASOP_NOGLOB
/*
Flag BASOP_Overflow = 0;
Flag BASOP_Carry = 0;
*/
#ifdef BASOP_NOGLOB_DEV_USE_GLOBALS
Flag Overflow = 0;
Flag Carry = 0;
#endif /* BASOP_NOGLOB_DEV_USE_GLOBALS */

#ifdef BASOP_PRINT_ON_WARNING
#include <stdio.h>
#if defined(__unix__) || defined(__unix) || defined(__APPLE__)
#include <execinfo.h>
/* Using macro instead of function here to avoid pushing new frames onto the stack */
#define PRINT_STACK()                                                          \
  do {                                                                         \
    void *call_stack[200];                                                     \
    int i;                                                                     \
    int num_frames;                                                            \
    char **strings;                                                            \
                                                                               \
    num_frames = backtrace(call_stack, 200);                                   \
    **strings = backtrace_symbols(call_stack, num_frames);                     \
    for (i = 0; i < num_frames; ++i) {                                         \
      printf("[BASOP] %s\n", strings[i]);                                      \
    }                                                                          \
    free(strings);                                                             \
  } while (0);
#elif defined _WIN32 || defined _WIN64
#include <windows.h>
#include <dbghelp.h>
/* Using macro instead of function here to avoid pushing new frames onto the stack */
#define PRINT_STACK()                                                          \
  do {                                                                         \
    void *call_stack[200];                                                     \
    int i;                                                                     \
    int num_frames;                                                            \
    SYMBOL_INFO *symbol;                                                       \
    HANDLE process;                                                            \
                                                                               \
    process = GetCurrentProcess();                                             \
    SymInitialize(process, NULL, TRUE);                                        \
    symbol =                                                                   \
        (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);    \
    symbol->MaxNameLen = 255;                                                  \
    symbol->SizeOfStruct = sizeof(SYMBOL_INFO);                                \
                                                                               \
    num_frames = CaptureStackBackTrace(0, 100, call_stack, NULL);              \
                                                                               \
    for (i = 0; i < num_frames; ++i) {                                         \
      SymFromAddr(process, (DWORD64)(call_stack[i]), 0, symbol);               \
      printf("[BASOP] %i %s - 0x%llX\n", i, symbol->Name, symbol->Address);    \
    }                                                                          \
                                                                               \
    free(symbol);                                                              \
  } while (0);
#else
#define PRINT_STACK()                                                          \
  do {                                                                         \
    printf("[BASOP] <call stack would be here> - printing call stack is not "  \
           "supported on this platform\n");                                    \
  } while (0);
#endif

/* Using macro instead of function here to avoid pushing new frames onto the stack */
#define PRINT_BASOP_NOGLOB_WARNING()                                           \
  do {                                                                         \
    printf("[BASOP] Overflow occured. Call stack:\n");                         \
    PRINT_STACK();                                                             \
  } while (0);

/* Using macro instead of function here to avoid pushing new frames onto the stack */
#define PRINT_BASOP_NOGLOB_ERROR()                                             \
  do {                                                                         \
    printf("[BASOP] Overflow error occured. Call stack:\n");                   \
    PRINT_STACK();                                                             \
  } while (0);
#endif /* BASOP_PRINT_ON_WARNING */

#if defined BASOP_PRINT_ON_WARNING || defined BASOP_ABORT_ON_WARNING
int BASOP_saturation_warning_enable=1, BASOP_saturation_warning_disable_counter=0;
@@ -167,7 +229,7 @@ int BASOP_warnings_as_errors=0;
#endif

#ifdef BASOP_PRINT_ON_WARNING
#define B_HELPER_PRINT_WARNING() do { if (BASOP_saturation_warning_enable) { if(BASOP_warnings_as_errors) { print_basop_noglob_error(); } else { print_basop_noglob_warning(); } } } while(0)
#define B_HELPER_PRINT_WARNING() do { if (BASOP_saturation_warning_enable) { if(BASOP_warnings_as_errors) { PRINT_BASOP_NOGLOB_ERROR(); } else { PRINT_BASOP_NOGLOB_WARNING(); } } } while(0)
#else
#define B_HELPER_PRINT_WARNING() do { (void)0; } while(0) /* no-op */
#endif
@@ -179,6 +241,9 @@ int BASOP_warnings_as_errors=0;
#endif

#ifdef BASOP_NOGLOB_DEV_USE_GLOBALS
Flag Overflow = 0;
Flag Carry = 0;

#define B_HELPER_SET_GLOBAL(global_flag, value) do { (global_flag) = (value); } while (0)
#define B_HELPER_GET_GLOBAL(global_flag) (global_flag)
#else
@@ -198,62 +263,6 @@ Flag get_carry(const Flag* carry) { if (carry) return *carry; else { B_HELPER_PR
#undef B_HELPER_SET_GLOBAL
#undef B_HELPER_GET_GLOBAL

#ifdef BASOP_PRINT_ON_WARNING
#include <stdio.h>
#if defined(__unix__) || defined(__unix) || defined(__APPLE__)
#include <execinfo.h>
void print_stack(void) {
  void *call_stack[200];
  int i;
  int num_frames = backtrace(call_stack, 200);
  char **strs = backtrace_symbols(call_stack, num_frames);
  for (i = 0; i < num_frames; ++i) {
    printf("[BASOP] %s\n", strs[i]);
  }
  free(strs);
}
#elif defined _WIN32 || defined _WIN64
#include <windows.h>
#include <dbghelp.h>
void print_stack(void) {
  void *call_stack[200];
  int i;
  int num_frames;

  HANDLE process;
  SYMBOL_INFO *symbol;

  process = GetCurrentProcess();
  SymInitialize(process, NULL, TRUE);
  symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
  symbol->MaxNameLen = 255;
  symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

  num_frames = CaptureStackBackTrace(0, 100, call_stack, NULL);

  for (i = 0; i < num_frames; ++i) {
    SymFromAddr(process, (DWORD64)(call_stack[i]), 0, symbol);
    printf("[BASOP] %i %s - 0x%llX\n", i, symbol->Name, symbol->Address);
  }

  free(symbol);
}
#else
void print_stack(void) {
  printf("[BASOP] <call stack would be here> - printing call stack is not supported on this platform\n");
}
#endif

void print_basop_noglob_warning(void) {
  printf("[BASOP] Overflow occured. Call stack:\n");
  print_stack();
}
void print_basop_noglob_error(void) {
  printf("[BASOP] Overflow error occured. Call stack:\n");
  print_stack();
}
#endif /* BASOP_PRINT_ON_WARNING */

#else /* BASOP_NOGLOB */
Flag Overflow = 0;
Flag Carry = 0;