diff --git a/moo/lib/err.c b/moo/lib/err.c index d467691..0b69549 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -365,147 +365,3 @@ void moo_getsynerr (moo_t* moo, moo_synerr_t* synerr) if (synerr) *synerr = moo->c->synerr; } #endif - -/* -------------------------------------------------------------------------- - * ASSERTION SUPPORT - * -------------------------------------------------------------------------- */ - -#if defined(MOO_BUILD_RELEASE) - -void moo_assertfailed (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) -{ - /* do nothing */ -} - -#else /* defined(MOO_BUILD_RELEASE) */ - -#if defined(MOO_ENABLE_LIBUNWIND) -# include -#elif defined(HAVE_EXECINFO_H) -# include -# include -#endif - -#if defined(_WIN32) -# include -# include -#elif defined(__OS2__) -# define INCL_DOSPROCESS -# define INCL_DOSFILEMGR -# define INCL_DOSERRORS -# include -#elif defined(__DOS__) -# include -# if defined(_INTELC32_) -# define DOS_EXIT 0x4C -# else -# include -# endif -# include -#elif defined(vms) || defined(__vms) -# define __NEW_STARLET 1 -# include /* (SYS$...) */ -# include /* (SS$...) */ -# include /* (lib$...) */ -#elif defined(macintosh) -# include -# include -# include -# include -#else -# include -# include -# include -# include -#endif - -#if defined(MOO_ENABLE_LIBUNWIND) -static void backtrace_stack_frames (moo_t* moo) -{ - unw_cursor_t cursor; - unw_context_t context; - int n; - - unw_getcontext(&context); - unw_init_local(&cursor, &context); - - moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, "[BACKTRACE]\n"); - for (n = 0; unw_step(&cursor) > 0; n++) - { - unw_word_t ip, sp, off; - char symbol[256]; - - unw_get_reg (&cursor, UNW_REG_IP, &ip); - unw_get_reg (&cursor, UNW_REG_SP, &sp); - - if (unw_get_proc_name(&cursor, symbol, MOO_COUNTOF(symbol), &off)) - { - moo_copy_bcstr (symbol, MOO_COUNTOF(symbol), ""); - } - - moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, - "#%02d ip=0x%*p sp=0x%*p %s+0x%zu\n", - n, MOO_SIZEOF(void*) * 2, (void*)ip, MOO_SIZEOF(void*) * 2, (void*)sp, symbol, (moo_oow_t)off); - } -} -#elif defined(HAVE_BACKTRACE) -static void backtrace_stack_frames (moo_t* moo) -{ - void* btarray[128]; - moo_oow_t btsize; - char** btsyms; - - btsize = backtrace (btarray, MOO_COUNTOF(btarray)); - btsyms = backtrace_symbols (btarray, btsize); - if (btsyms) - { - moo_oow_t i; - moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, "[BACKTRACE]\n"); - - for (i = 0; i < btsize; i++) - { - moo_logbfmt(moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, " %s\n", btsyms[i]); - } - free (btsyms); - } -} -#else -static void backtrace_stack_frames (moo_t* moo) -{ - /* do nothing. not supported */ -} -#endif - -void moo_assertfailed (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) -{ - moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_FATAL, "ASSERTION FAILURE: %s at %s:%zu\n", expr, file, line); - backtrace_stack_frames (moo); - -#if defined(_WIN32) - ExitProcess (249); -#elif defined(__OS2__) - DosExit (EXIT_PROCESS, 249); -#elif defined(__DOS__) - { - union REGS regs; - regs.h.ah = DOS_EXIT; - regs.h.al = 249; - intdos (®s, ®s); - } -#elif defined(vms) || defined(__vms) - lib$stop (SS$_ABORT); /* use SS$_OPCCUS instead? */ - /* this won't be reached since lib$stop() terminates the process */ - sys$exit (SS$_ABORT); /* this condition code can be shown with - * 'show symbol $status' from the command-line. */ -#elif defined(macintosh) - - ExitToShell (); - -#else - - kill (getpid(), SIGABRT); - _exit (1); -#endif -} - -#endif /* defined(MOO_BUILD_RELEASE) */ diff --git a/moo/lib/main.c b/moo/lib/main.c index 374b8b8..89a80a9 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -64,6 +64,7 @@ # define INCL_DOSEXCEPTIONS # define INCL_DOSMISC # define INCL_DOSDATETIME +# define INCL_DOSFILEMGR # define INCL_DOSERRORS # include # include @@ -83,6 +84,12 @@ # include # include +# if defined(_INTELC32_) +# define DOS_EXIT 0x4C +# else +# include +# endif + /* fake XPOLLXXX values */ # define XPOLLIN (1 << 0) # define XPOLLOUT (1 << 1) @@ -95,6 +102,10 @@ # include #else +# include +# include +# include +# include # if defined(MOO_ENABLE_LIBLTDL) # include @@ -122,10 +133,6 @@ # include # endif -# include -# include -# include - # if defined(USE_THREAD) # include # include @@ -1000,6 +1007,127 @@ static moo_errnum_t syserrstrb (moo_t* moo, int syserr_type, int syserr_code, mo /* ========================================================================= */ +#if defined(MOO_BUILD_RELEASE) + +static void assert_fail (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) +{ + /* do nothing */ +} + +#else /* defined(MOO_BUILD_RELEASE) */ + +#if defined(MOO_ENABLE_LIBUNWIND) +# include +#elif defined(HAVE_EXECINFO_H) +# include +# include +#endif + +#if defined(vms) || defined(__vms) +# define __NEW_STARLET 1 +# include /* (SYS$...) */ +# include /* (SS$...) */ +# include /* (lib$...) */ +#elif defined(macintosh) +# include +# include +# include +# include +#endif + +#if defined(MOO_ENABLE_LIBUNWIND) +static void backtrace_stack_frames (moo_t* moo) +{ + unw_cursor_t cursor; + unw_context_t context; + int n; + + unw_getcontext(&context); + unw_init_local(&cursor, &context); + + moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, "[BACKTRACE]\n"); + for (n = 0; unw_step(&cursor) > 0; n++) + { + unw_word_t ip, sp, off; + char symbol[256]; + + unw_get_reg (&cursor, UNW_REG_IP, &ip); + unw_get_reg (&cursor, UNW_REG_SP, &sp); + + if (unw_get_proc_name(&cursor, symbol, MOO_COUNTOF(symbol), &off)) + { + moo_copy_bcstr (symbol, MOO_COUNTOF(symbol), ""); + } + + moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, + "#%02d ip=0x%*p sp=0x%*p %s+0x%zu\n", + n, MOO_SIZEOF(void*) * 2, (void*)ip, MOO_SIZEOF(void*) * 2, (void*)sp, symbol, (moo_oow_t)off); + } +} +#elif defined(HAVE_BACKTRACE) +static void backtrace_stack_frames (moo_t* moo) +{ + void* btarray[128]; + moo_oow_t btsize; + char** btsyms; + + btsize = backtrace (btarray, MOO_COUNTOF(btarray)); + btsyms = backtrace_symbols (btarray, btsize); + if (btsyms) + { + moo_oow_t i; + moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, "[BACKTRACE]\n"); + + for (i = 0; i < btsize; i++) + { + moo_logbfmt(moo, MOO_LOG_UNTYPED | MOO_LOG_DEBUG, " %s\n", btsyms[i]); + } + free (btsyms); + } +} +#else +static void backtrace_stack_frames (moo_t* moo) +{ + /* do nothing. not supported */ +} +#endif + +static void assert_fail (moo_t* moo, const moo_bch_t* expr, const moo_bch_t* file, moo_oow_t line) +{ + moo_logbfmt (moo, MOO_LOG_UNTYPED | MOO_LOG_FATAL, "ASSERTION FAILURE: %s at %s:%zu\n", expr, file, line); + backtrace_stack_frames (moo); + +#if defined(_WIN32) + ExitProcess (249); +#elif defined(__OS2__) + DosExit (EXIT_PROCESS, 249); +#elif defined(__DOS__) + { + union REGS regs; + regs.h.ah = DOS_EXIT; + regs.h.al = 249; + intdos (®s, ®s); + } +#elif defined(vms) || defined(__vms) + lib$stop (SS$_ABORT); /* use SS$_OPCCUS instead? */ + /* this won't be reached since lib$stop() terminates the process */ + sys$exit (SS$_ABORT); /* this condition code can be shown with + * 'show symbol $status' from the command-line. */ +#elif defined(macintosh) + + ExitToShell (); + +#else + + kill (getpid(), SIGABRT); + _exit (1); +#endif +} + +#endif /* defined(MOO_BUILD_RELEASE) */ + +/* ========================================================================= */ + #if defined(USE_LTDL) # define sys_dl_error() lt_dlerror() # define sys_dl_open(x) lt_dlopen(x) @@ -1550,7 +1678,7 @@ static int _mod_poll_fd (moo_t* moo, int fd, int event_mask) if (_del_poll_fd (moo, fd) <= -1) return -1; - if (_add_poll_fd (moo, fd, event_mask, event_data) <= -1) + if (_add_poll_fd (moo, fd, event_mask) <= -1) { /* TODO: any good way to rollback successful deletion? */ return -1; @@ -2896,6 +3024,7 @@ int main (int argc, char* argv[]) } vmprim.log_write = log_write; vmprim.syserrstrb = syserrstrb; + vmprim.assertfail = assert_fail; vmprim.dl_open = dl_open; vmprim.dl_close = dl_close; vmprim.dl_getsym = dl_getsym; diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 3b533bf..50f0837 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1085,6 +1085,13 @@ typedef moo_errnum_t (*moo_syserrstru_t) ( moo_oow_t len ); +typedef void (*moo_assertfail_t) ( + moo_t* moo, + const moo_bch_t* expr, + const moo_bch_t* file, + moo_oow_t line +); + enum moo_vmprim_dlopen_flag_t { MOO_VMPRIM_DLOPEN_PFMOD = (1 << 0) @@ -1163,6 +1170,7 @@ struct moo_vmprim_t moo_log_write_t log_write; moo_syserrstrb_t syserrstrb; moo_syserrstru_t syserrstru; + moo_assertfail_t assertfail; moo_vmprim_dlopen_t dl_open; moo_vmprim_dlclose_t dl_close; @@ -1626,7 +1634,7 @@ struct moo_t #if defined(MOO_BUILD_RELEASE) # define MOO_ASSERT(moo,expr) ((void)0) #else -# define MOO_ASSERT(moo,expr) ((void)((expr) || (moo_assertfailed (moo, #expr, __FILE__, __LINE__), 0))) +# define MOO_ASSERT(moo,expr) ((void)((expr) || ((moo)->vmprim.assertfail(moo, #expr, __FILE__, __LINE__), 0))) #endif @@ -2288,13 +2296,6 @@ MOO_EXPORT int moo_decode ( const moo_oocs_t* classfqn ); -MOO_EXPORT void moo_assertfailed ( - moo_t* moo, - const moo_bch_t* expr, - const moo_bch_t* file, - moo_oow_t line -); - MOO_EXPORT const moo_ooch_t* moo_errnum_to_errstr ( moo_errnum_t errnum );