bbdebug.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 T> bbString bbDBType(){
  35. return bbDBType( (T*)0 );
  36. }
  37. template<class T> bbString bbDBType( bbGCVar<T> *p ){ return bbDBType<T*>(); }
  38. template<class T> bbString bbDBValue( bbGCVar<T> *p ){ T *t=p->get();return t ? bbDBValue( &t ) : "Null"; }
  39. template<class T> bbString bbDBType( T **p ){ return bbDBType<T>()+" Ptr"; }
  40. template<class T> bbString bbDBValue( T **p ){ char buf[64];sprintf( buf,"$%p",*p );return buf; }
  41. struct bbDBVarType{
  42. virtual bbString type()=0;
  43. virtual bbString value( void *p )=0;
  44. };
  45. template<class T> struct bbDBVarType_t : public bbDBVarType{
  46. bbString type(){
  47. return bbDBType( (T*)0 );
  48. }
  49. bbString value( void *p ){
  50. return bbDBValue( (T*)p );
  51. }
  52. static bbDBVarType_t info;
  53. };
  54. template<class T> bbDBVarType_t<T> bbDBVarType_t<T>::info;
  55. struct bbDBVar{
  56. const char *name;
  57. bbDBVarType *type;
  58. void *var;
  59. };
  60. struct bbDBContext{
  61. bbDBFrame *frames=nullptr;
  62. bbDBVar *localsBuf=nullptr;
  63. bbDBVar *locals=nullptr;
  64. int stepMode;
  65. int stopped;
  66. ~bbDBContext();
  67. void init();
  68. };
  69. namespace bbDB{
  70. extern int nextSeq;
  71. extern bbDBContext *currentContext;
  72. void init();
  73. void stop();
  74. void stopped();
  75. void error( bbString err );
  76. bbArray<bbString> stack();
  77. void emitStack();
  78. }
  79. struct bbDBBlock{
  80. bbDBVar *locals;
  81. bbDBBlock():locals( bbDB::currentContext->locals ){
  82. if( bbDB::currentContext->stepMode=='l' ) --bbDB::currentContext->stopped;
  83. }
  84. ~bbDBBlock(){
  85. if( bbDB::currentContext->stepMode=='l' ) ++bbDB::currentContext->stopped;
  86. bbDB::currentContext->locals=locals;
  87. }
  88. };
  89. struct bbDBFrame : public bbDBBlock{
  90. bbDBFrame *succ;
  91. const char *decl;
  92. const char *srcFile;
  93. int srcPos;
  94. int seq;
  95. bbDBFrame( const char *decl,const char *srcFile ):succ( bbDB::currentContext->frames ),decl( decl ),srcFile( srcFile ),seq( ++bbDB::nextSeq ){
  96. bbDB::currentContext->frames=this;
  97. if( bbDB::currentContext->stepMode=='s' ) --bbDB::currentContext->stopped;
  98. }
  99. ~bbDBFrame(){
  100. if( bbDB::currentContext->stepMode=='s' ) ++bbDB::currentContext->stopped;
  101. bbDB::currentContext->frames=succ;
  102. }
  103. };
  104. struct bbDBLoop : public bbDBBlock{
  105. bbDBLoop(){
  106. }
  107. ~bbDBLoop(){
  108. }
  109. };
  110. inline void bbDBStmt( int srcPos ){
  111. bbDB::currentContext->frames->srcPos=srcPos;
  112. if( bbDB::currentContext->stopped>0 ) bbDB::stopped();
  113. }
  114. template<class T> void bbDBEmit( const char *name,T *var ){
  115. bbDBVarType *type=&bbDBVarType_t<T>::info;
  116. puts( (BB_T( name )+":"+type->type()+"="+type->value( var )).c_str() );
  117. }
  118. template<class T> void bbDBEmit( const char *name,bbGCVar<T> *p ){
  119. T *var=p->get();return bbDBEmit( name,&var );
  120. }
  121. template<class T> void bbDBLocal( const char *name,T *var ){
  122. bbDB::currentContext->locals->name=name;
  123. bbDB::currentContext->locals->type=&bbDBVarType_t<T>::info;
  124. bbDB::currentContext->locals->var=var;
  125. ++bbDB::currentContext->locals;
  126. }
  127. #endif