Commit e2f4988c authored by vaillancour's avatar vaillancour
Browse files

Merge branch 'sgi_improve_stack_printing' into basop_noglob

parents 2f4efb59 ec873d9b
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -6,6 +6,12 @@ set(CMAKE_C_STANDARD 99)
include(CTest)
enable_testing()

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
  # Needed for printing stack trace on Linux
  add_compile_options(-rdynamic)
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -export-dynamic")
endif()

set(dirs lib_enc lib_dec lib_com basic_op basic_math lib_debug)
include_directories(${dirs})

@@ -19,13 +25,13 @@ endif()
add_executable(EVS_cod lib_enc/encoder.c)
target_link_libraries(EVS_cod lib_evs)
if(WIN32)
  target_link_libraries(EVS_cod Ws2_32)
  target_link_libraries(EVS_cod Ws2_32 Dbghelp)
endif()

add_executable(EVS_dec lib_dec/decoder.c)
target_link_libraries(EVS_dec lib_evs)
if(WIN32)
  target_link_libraries(EVS_dec Ws2_32)
  target_link_libraries(EVS_dec Ws2_32 Dbghelp)
endif()

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
+6 −0
Original line number Diff line number Diff line
@@ -30,6 +30,12 @@ ifeq "$(RELEASE)" "1"
CFLAGS   += -DRELEASE
DELIVERY  = 1
OPTIM    ?= 2
else
ifeq ($(shell uname -s), Linux)
# Needed for printing stack trace on Linux
CFLAGS   += -rdynamic
LDFLAGS  += -export-dynamic
endif
endif

ifneq "$(DEBUG)" "0"
+1 −1
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@
      <Culture>0x0c0c</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies />
      <AdditionalDependencies>Dbghelp.lib</AdditionalDependencies>
      <OutputFile>..\IVAS_dec.exe</OutputFile>
      <SuppressStartupBanner>true</SuppressStartupBanner>
      <ManifestFile />
+1 −1
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@
      <Culture>0x0c0c</Culture>
    </ResourceCompile>
    <Link>
      <AdditionalDependencies />
      <AdditionalDependencies>Dbghelp.lib</AdditionalDependencies>
      <OutputFile>..\IVAS_cod.exe</OutputFile>
      <SuppressStartupBanner>true</SuppressStartupBanner>
      <ManifestFile />
+77 −39
Original line number Diff line number Diff line
@@ -152,14 +152,79 @@ 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;                                                            \
    DWORD64 offset;                                                            \
    SYMBOL_INFO *symbol;                                                       \
    IMAGEHLP_MODULE module;                                                    \
    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);                                \
    module.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);                           \
                                                                               \
    num_frames = CaptureStackBackTrace(0, 100, call_stack, NULL);              \
    for (i = 0; i < num_frames; ++i) {                                         \
      SymFromAddr(process, (DWORD64)(call_stack[i]), &offset, symbol);         \
      SymGetModuleInfo(process, (DWORD)symbol->ModBase, &module);              \
      printf("[BASOP] %i\t%s(%s+0x%llX) [0x%llX]\n", i, module.ModuleName,     \
             symbol->Name, offset, 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 +232,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 +244,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,36 +266,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);
}
#else
void print_stack(void) {
  printf("[BASOP] <call stack would be here> - printing call stack currently only supported on UNIX\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;