debugtree.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "stdafx.h"
  2. #include "debugtree.h"
  3. #include "prefs.h"
  4. #include "../bbruntime/basic.h"
  5. IMPLEMENT_DYNAMIC( DebugTree,CTreeCtrl )
  6. BEGIN_MESSAGE_MAP( DebugTree,CTreeCtrl )
  7. ON_WM_CREATE()
  8. END_MESSAGE_MAP()
  9. DebugTree::DebugTree():st_nest(0){
  10. }
  11. DebugTree::~DebugTree(){
  12. }
  13. int DebugTree::OnCreate( LPCREATESTRUCT lpCreateStruct ){
  14. CTreeCtrl::OnCreate( lpCreateStruct );
  15. SetBkColor( prefs.rgb_bkgrnd );
  16. SetTextColor( prefs.rgb_default );
  17. SetFont( &prefs.debugFont );
  18. return 0;
  19. }
  20. static string typeTag( Type *t ){
  21. if( t->intType() ) return "";
  22. if( t->floatType() ) return "#";
  23. if( t->stringType() ) return "$";
  24. if( StructType *s=t->structType() ) return "."+s->ident;
  25. if( VectorType *v=t->vectorType() ){
  26. string s=typeTag( v->elementType )+"[";
  27. for( int k=0;k<v->sizes.size();++k ){
  28. if( k ) s+=",";
  29. s+=itoa( v->sizes[k]-1 );
  30. }
  31. return s+"]";
  32. }
  33. return "";
  34. }
  35. HTREEITEM DebugTree::insertVar( void *var,Decl *d,const string &name,HTREEITEM it,HTREEITEM parent ){
  36. string s=name;
  37. ConstType *ct=d->type->constType();
  38. StructType *st=d->type->structType();
  39. VectorType *vt=d->type->vectorType();
  40. if( ct ){
  41. Type *t=ct->valueType;
  42. s+=typeTag(t);
  43. if( t->intType() ){
  44. s+="="+itoa( ct->intValue );
  45. }else if( t->floatType() ){
  46. s+="="+ftoa( ct->floatValue );
  47. }else if( t->stringType() ){
  48. s+="=\""+ct->stringValue+'\"';
  49. }
  50. }else if( var ){
  51. Type *t=d->type;
  52. s+=typeTag( t );
  53. if( t->intType() ){
  54. s+="="+itoa( *(int*)var );
  55. }else if( t->floatType() ){
  56. s+="="+ftoa( *(float*)var );
  57. }else if( t->stringType() ){
  58. BBStr *str=*(BBStr**)var;
  59. if( str ) s+="=\""+*str+'\"';
  60. else s+="=\"\"";
  61. }else if( st ){
  62. var=*(void**)var;
  63. if( var ) var=*(void**)var;
  64. if( !var ) s+=" (Null)";
  65. }
  66. }
  67. if( it ){
  68. if( GetItemText( it )!=s.c_str() ){
  69. SetItemText( it,s.c_str() );
  70. }
  71. }else{
  72. it=InsertItem( s.c_str(),parent );
  73. }
  74. ++st_nest;
  75. if( st ){
  76. if( var ){
  77. if( st_nest<4 ){
  78. HTREEITEM st_it=GetChildItem( it );
  79. for( int k=0;k<st->fields->size();++k ){
  80. Decl *st_d=st->fields->decls[k];
  81. void *st_var=(char*)var+st_d->offset;
  82. char name[256];
  83. st_d->getName( name );
  84. st_it=insertVar( st_var,st_d,name,st_it,it );
  85. }
  86. }
  87. }else{
  88. while( HTREEITEM t=GetChildItem( it ) ){
  89. DeleteItem( t );
  90. }
  91. }
  92. }
  93. --st_nest;
  94. return it ? GetNextSiblingItem( it ) : 0;
  95. }
  96. /******************************* CONSTS ***********************************/
  97. ConstsTree::ConstsTree(){
  98. }
  99. void ConstsTree::reset( Environ *env ){
  100. HTREEITEM it=GetChildItem( TVI_ROOT );
  101. for( int k=0;k<env->decls->size();++k ){
  102. Decl *d=env->decls->decls[k];
  103. if( !(d->kind & (DECL_GLOBAL) ) ) continue;
  104. if( d->type->constType() ){
  105. char name[256];
  106. d->getName( name );
  107. it=insertVar( 0,d,name,it,TVI_ROOT );
  108. }
  109. }
  110. }
  111. /******************************* GLOBALS **********************************/
  112. GlobalsTree::GlobalsTree():module(0),envron(0){
  113. }
  114. void GlobalsTree::reset( Module *mod,Environ *env ){
  115. module=mod;
  116. envron=env;
  117. }
  118. void GlobalsTree::refresh(){
  119. if( !module || !envron ) return;
  120. HTREEITEM it=GetChildItem( TVI_ROOT );
  121. for( int k=0;k<envron->decls->size();++k ){
  122. Decl *d=envron->decls->decls[k];
  123. if( !(d->kind & (DECL_GLOBAL) ) ) continue;
  124. if( !d->type->constType() ){
  125. char name[256];
  126. d->getName( name );
  127. void *var=0;
  128. module->findSymbol( ("_v"+string(name)).c_str(),(int*)&var );
  129. it=insertVar( var,d,name,it,TVI_ROOT );
  130. }
  131. }
  132. }
  133. /******************************** LOCALS **********************************/
  134. LocalsTree::LocalsTree():envron(0){
  135. }
  136. void LocalsTree::reset( Environ *env ){
  137. envron=env;
  138. }
  139. void LocalsTree::refresh(){
  140. if( !envron || !frames.size() ) return;
  141. HTREEITEM item=GetChildItem( TVI_ROOT );
  142. int n=0;
  143. for( n=0;n<frames.size();++n ){
  144. if( !item || item!=frames[n].item ) break;
  145. item=GetNextSiblingItem( item );
  146. }
  147. while( item ){
  148. HTREEITEM next=GetNextSiblingItem( item );
  149. DeleteItem( item );
  150. item=next;
  151. }
  152. for( ;n<frames.size();++n ){
  153. item=frames[n].item=InsertItem( frames[n].func,TVI_ROOT,TVI_LAST );
  154. if( n<frames.size()-1 ) refreshFrame( frames[n] );
  155. }
  156. refreshFrame( frames.back() );
  157. }
  158. void LocalsTree::refreshFrame( const Frame &f ){
  159. HTREEITEM it=GetChildItem( f.item );
  160. for( int n=0;n<f.env->decls->size();++n ){
  161. Decl *d=f.env->decls->decls[n];
  162. if( !(d->kind & (DECL_LOCAL|DECL_PARAM) ) ) continue;
  163. char name[256];
  164. d->getName( name );
  165. if( !isalpha( name[0] ) ) continue;
  166. it=insertVar( (char*)f.frame+d->offset,d,name,it,f.item );
  167. }
  168. }
  169. void LocalsTree::pushFrame( void *f,void *e,const char *func ){
  170. frames.push_back( Frame( f,(Environ*)e,func ) );
  171. }
  172. void LocalsTree::popFrame(){
  173. frames.pop_back();
  174. }