bbdebug.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef BB_DEBUG_H
  2. #define BB_DEBUG_H
  3. #include "bbstring.h"
  4. struct bbDBFiber;
  5. struct bbDBFrame;
  6. struct bbDBVarType;
  7. struct bbDBVar;
  8. inline bbString bbDBType( const void *p ){ return "Void"; }
  9. inline bbString bbDBType( bbBool *p ){ return "Bool"; }
  10. inline bbString bbDBType( bbByte *p ){ return "Byte"; }
  11. inline bbString bbDBType( bbUByte *p ){ return "UByte"; }
  12. inline bbString bbDBType( bbShort *p ){ return "Short"; }
  13. inline bbString bbDBType( bbUShort *p ){ return "UShort"; }
  14. inline bbString bbDBType( bbInt *p ){ return "Int"; }
  15. inline bbString bbDBType( bbUInt *p ){ return "UInt"; }
  16. inline bbString bbDBType( bbLong *p ){ return "Long"; }
  17. inline bbString bbDBType( bbULong *p ){ return "ULong"; }
  18. inline bbString bbDBType( bbFloat *p ){ return "Float"; }
  19. inline bbString bbDBType( bbDouble *p ){ return "Double"; }
  20. inline bbString bbDBType( bbString *p ){ return "String"; }
  21. inline bbString bbDBValue( void *p ){ return "?????"; }
  22. inline bbString bbDBValue( bbBool *p ){ return *p ? "True" : "False"; }
  23. inline bbString bbDBValue( bbByte *p ){ return bbString(*p); }
  24. inline bbString bbDBValue( bbUByte *p ){ return bbString(*p); }
  25. inline bbString bbDBValue( bbShort *p ){ return bbString(*p); }
  26. inline bbString bbDBValue( bbUShort *p ){ return bbString(*p); }
  27. inline bbString bbDBValue( bbInt *p ){ return bbString(*p); }
  28. inline bbString bbDBValue( bbUInt *p ){ return bbString(*p); }
  29. inline bbString bbDBValue( bbLong *p ){ return bbString(*p); }
  30. inline bbString bbDBValue( bbULong *p ){ return bbString(*p); }
  31. inline bbString bbDBValue( bbFloat *p ){ return bbString(*p); }
  32. inline bbString bbDBValue( bbDouble *p ){ return bbString(*p); }
  33. extern bbString bbDBValue( bbString *p );
  34. template<class R,class...A> bbString bbDBType( R(*)(A...) ){ return "Extern?(?)"; }
  35. template<class R,class...A> bbString bbDBValue( R(*)(A...) ){ return "?"; }
  36. template<class T> bbString bbDBType(){
  37. return bbDBType( (T*)0 );
  38. }
  39. template<class T> bbString bbDBType( bbGCVar<T> *p ){ return bbDBType<T*>(); }
  40. template<class T> bbString bbDBValue( bbGCVar<T> *p ){ T *t=p->get();return t ? bbDBValue( &t ) : "Null"; }
  41. template<class T> bbString bbDBType( T **p ){ return bbDBType<T>()+" Ptr"; }
  42. template<class T> bbString bbDBValue( T **p ){ char buf[64];sprintf( buf,"$%p",*p );return buf; }
  43. struct bbDBVarType{
  44. virtual bbString type()=0;
  45. virtual bbString value( void *p )=0;
  46. };
  47. template<class T> struct bbDBVarType_t : public bbDBVarType{
  48. bbString type(){
  49. return bbDBType( (T*)0 );
  50. }
  51. bbString value( void *p ){
  52. return bbDBValue( (T*)p );
  53. }
  54. static bbDBVarType_t info;
  55. };
  56. template<class T> bbDBVarType_t<T> bbDBVarType_t<T>::info;
  57. struct bbDBVar{
  58. const char *name;
  59. bbDBVarType *type;
  60. void *var;
  61. };
  62. struct bbDBContext{
  63. bbDBFrame *frames=nullptr;
  64. bbDBVar *localsBuf=nullptr;
  65. bbDBVar *locals=nullptr;
  66. int stepMode=0;
  67. int stopped=0;
  68. ~bbDBContext();
  69. void init();
  70. };
  71. namespace bbDB{
  72. #ifdef BB_THREADS
  73. extern std::atomic_int nextSeq;
  74. extern thread_local bbDBContext *currentContext;
  75. #else
  76. extern int nextSeq;
  77. extern bbDBContext *currentContext;
  78. #endif
  79. void init();
  80. void stop();
  81. void stopped();
  82. void error( bbString err );
  83. bbArray<bbString> stack();
  84. void emitStack();
  85. }
  86. struct bbDBBlock{
  87. bbDBVar *locals;
  88. bbDBBlock():locals( bbDB::currentContext->locals ){
  89. if( bbDB::currentContext->stepMode=='l' ) --bbDB::currentContext->stopped;
  90. }
  91. ~bbDBBlock(){
  92. if( bbDB::currentContext->stepMode=='l' ) ++bbDB::currentContext->stopped;
  93. bbDB::currentContext->locals=locals;
  94. }
  95. };
  96. struct bbDBFrame : public bbDBBlock{
  97. bbDBFrame *succ;
  98. const char *decl;
  99. const char *srcFile;
  100. int srcPos;
  101. int seq;
  102. bbDBFrame( const char *decl,const char *srcFile ):succ( bbDB::currentContext->frames ),decl( decl ),srcFile( srcFile ),seq( ++bbDB::nextSeq ){
  103. bbDB::currentContext->frames=this;
  104. if( bbDB::currentContext->stepMode=='s' ) --bbDB::currentContext->stopped;
  105. }
  106. ~bbDBFrame(){
  107. if( bbDB::currentContext->stepMode=='s' ) ++bbDB::currentContext->stopped;
  108. bbDB::currentContext->frames=succ;
  109. }
  110. };
  111. struct bbDBLoop : public bbDBBlock{
  112. bbDBLoop(){
  113. }
  114. ~bbDBLoop(){
  115. }
  116. };
  117. inline void bbDBStmt( int srcPos ){
  118. bbDB::currentContext->frames->srcPos=srcPos;
  119. if( bbDB::currentContext->stopped>0 ) bbDB::stopped();
  120. }
  121. template<class T> void bbDBEmit( const char *name,T *var ){
  122. bbDBVarType *type=&bbDBVarType_t<T>::info;
  123. puts( (BB_T( name )+":"+type->type()+"="+type->value( var )).c_str() );
  124. }
  125. template<class T> void bbDBEmit( const char *name,bbGCVar<T> *p ){
  126. T *var=p->get();return bbDBEmit( name,&var );
  127. }
  128. template<class T> void bbDBLocal( const char *name,T *var ){
  129. bbDB::currentContext->locals->name=name;
  130. bbDB::currentContext->locals->type=&bbDBVarType_t<T>::info;
  131. bbDB::currentContext->locals->var=var;
  132. ++bbDB::currentContext->locals;
  133. }
  134. #endif