bbdebug.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "bbdebug.h"
  2. #include "bbarray.h"
  3. #if _WIN32
  4. #include <windows.h>
  5. #else
  6. #include <signal.h>
  7. #endif
  8. namespace bbDB{
  9. bbDBContext *currentContext;
  10. #if _WIN32
  11. BOOL WINAPI stopHandler( DWORD dwCtrlType ){
  12. if( dwCtrlType==CTRL_BREAK_EVENT ){
  13. // printf( "CTRL_BREAK_EVENT\n" );fflush( stdout );
  14. currentContext->stopped=0;
  15. return TRUE;
  16. }
  17. return FALSE;
  18. }
  19. #else
  20. void sighandler( int sig ){
  21. // printf( "SIGTSTP\n" );fflush( stdout );
  22. currentContext->stopped=0;
  23. }
  24. #endif
  25. void init(){
  26. currentContext=new bbDBContext;
  27. currentContext->init();
  28. #if _WIN32
  29. SetConsoleCtrlHandler( stopHandler,TRUE );
  30. #else
  31. signal( SIGTSTP,sighandler );
  32. #endif
  33. }
  34. void emitStack(){
  35. bbDBVar *ev=currentContext->locals;
  36. for( bbDBFrame *f=currentContext->frames;f;f=f->succ ){
  37. printf( ">%s;%s;%i\n",f->decl,f->srcFile,f->srcPos );
  38. for( bbDBVar *v=f->locals;v!=ev;++v ){
  39. char id[64],type[64],value[128];
  40. strcpy( id,v->name );
  41. strcpy( type,v->type->name().c_str() );
  42. strcpy( value,v->type->value( v->var ).c_str() );
  43. printf( "%s:%s=%s\n",id,type,value );
  44. }
  45. ev=f->locals;
  46. }
  47. }
  48. void stop(){
  49. currentContext->stopped=0;
  50. }
  51. void stopped(){
  52. printf( "{{!DEBUG!}}\n" );
  53. emitStack();
  54. printf( "\n" );
  55. fflush( stdout );
  56. char buf[256];
  57. char *e=fgets( buf,256,stdin );
  58. if( !e ) exit( -1 );
  59. switch( e[0] ){
  60. case 's':currentContext->stopped=0;return;
  61. case 'e':currentContext->stopped=1;return;
  62. case 'l':currentContext->stopped=-1;return;
  63. case 'r':currentContext->stopped=-0x10000000;return;
  64. case 'q':
  65. printf( "Quitting!!!!!\n" );fflush( stdout );
  66. exit( 0 );
  67. }
  68. printf( "???? %s\n",buf );fflush( stdout );
  69. exit( -1 );
  70. }
  71. bbArray<bbString> *stack(){
  72. int n=0;
  73. for( bbDBFrame *frame=currentContext->frames;frame;frame=frame->succ ) ++n;
  74. //TODO: Fix GC issues! Can't have a free local like this in case bbString ctors cause gc sweep!!!!
  75. bbArray<bbString> *st=bbArray<bbString>::create( n );
  76. int i=0;
  77. for( bbDBFrame *frame=currentContext->frames;frame;frame=frame->succ ){
  78. st->at( i++ )=BB_T( frame->srcFile )+" ["+bbString( frame->srcPos>>12 )+"] "+frame->decl;
  79. }
  80. return st;
  81. }
  82. }
  83. void bbDBContext::init(){
  84. if( !localsBuf ) localsBuf=new bbDBVar[16384];
  85. locals=localsBuf;
  86. frames=nullptr;
  87. stopped=0;
  88. }
  89. bbDBContext::~bbDBContext(){
  90. delete[] localsBuf;
  91. }
  92. template<> bbDBType *bbDBTypeOf( void* ){
  93. struct type : public bbDBType{
  94. bbString name(){ return "Void"; }
  95. };
  96. static type _type;
  97. return &_type;
  98. }
  99. template<> bbDBType *bbDBTypeOf( bbInt* ){
  100. struct type : public bbDBType{
  101. bbString name(){ return "Int"; }
  102. bbString value( void *var ){ return *(bbInt*)var; }
  103. };
  104. static type _type;
  105. return &_type;
  106. }
  107. template<> bbDBType *bbDBTypeOf( bbString* ){
  108. struct type : public bbDBType{
  109. bbString name(){ return "String"; }
  110. bbString value( void *var ){ return BB_T("\"")+*(bbString*)var+BB_T( "\"" ); }
  111. };
  112. static type _type;
  113. return &_type;
  114. }