source: dev/trunk/abdev/BasicCompiler_Common/WatchList.cpp@ 193

Last change on this file since 193 was 182, checked in by dai_9181, 17 years ago
File size: 12.5 KB
Line 
1#include <jenga/include/smoothie/Smoothie.h>
2
3#include "common.h"
4
5#ifdef _AMD64_
6#include "../BasicCompiler64/opcode.h"
7#else
8#include "../BasicCompiler32/opcode.h"
9#endif
10
11//デバッグ用
12#include "debug.h"
13
14int Debugging_GetArray( const int *SubScripts,char *array,const Type &type,LONG_PTR *plpOffset);
15
16ULONG_PTR Debugging_GetVarPtr(RELATIVE_VAR *pRelativeVar){
17 extern DWORD ImageBase;
18 extern int MemPos_RWSection;
19 int i2;
20
21 if(pRelativeVar->dwKind==VAR_GLOBAL){
22 return ImageBase+MemPos_RWSection+pRelativeVar->offset;
23 }
24 else if( pRelativeVar->dwKind == VAR_REFGLOBAL ){
25 extern HANDLE hDebugProcess;
26 LONG_PTR lpData;
27 SIZE_T accessBytes;
28 ReadProcessMemory(hDebugProcess,
29 (void *)(ImageBase+MemPos_RWSection+pRelativeVar->offset),
30 &lpData,
31 sizeof(LONG_PTR),
32 &accessBytes);
33
34 return lpData;
35 }
36 else if(pRelativeVar->dwKind==VAR_LOCAL){
37 extern HWND hDebugWnd;
38 i2=(int)SendDlgItemMessage(hDebugWnd,IDC_PROCCOMBO,CB_GETCURSEL,0,0);
39 i2=pobj_dti->iProcLevel-i2;
40
41 if(pobj_dti->lplpSpBase[i2]==0) return 0;
42
43 return pobj_dti->lplpSpBase[i2]+pRelativeVar->offset;
44 }
45 else if( pRelativeVar->dwKind == VAR_REFLOCAL ){
46 extern HWND hDebugWnd;
47 i2=(int)SendDlgItemMessage(hDebugWnd,IDC_PROCCOMBO,CB_GETCURSEL,0,0);
48 i2=pobj_dti->iProcLevel-i2;
49
50 if(pobj_dti->lplpSpBase[i2]==0) return 0;
51
52 extern HANDLE hDebugProcess;
53 LONG_PTR lpData;
54 SIZE_T accessBytes;
55 ReadProcessMemory(hDebugProcess,
56 (void *)(pobj_dti->lplpSpBase[i2]+(int)pRelativeVar->offset),
57 &lpData,
58 sizeof(LONG_PTR),
59 &accessBytes);
60
61 return lpData;
62 }
63 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
64 return pRelativeVar->offset;
65 }
66
67 return 0;
68}
69
70bool Debugging_SetRelativeOffset( Type &type,RELATIVE_VAR *pRelativeVar,char *lpPtrOffset){
71 int array_num;
72
73 _int64 i64data;
74 if( !StaticCalculation( true, lpPtrOffset, 0, &i64data, Type(), 1 ) ){
75 return false;
76 }
77 if( type.IsReal() ){
78 double dbl;
79 memcpy(&dbl,&i64data,sizeof(double));
80 i64data=(_int64)dbl;
81 }
82
83 array_num=(int)i64data;
84
85 if( type.PtrLevel() ){
86 type.PtrLevelDown();
87 array_num *= type.GetSize();
88 }
89 else{
90 //エラー
91 return false;
92 }
93
94 extern HANDLE hDebugProcess;
95 LONG_PTR lpData;
96 SIZE_T accessBytes;
97 lpData=Debugging_GetVarPtr(pRelativeVar);
98 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,&pRelativeVar->offset,sizeof(LONG_PTR),&accessBytes)){
99 return false;
100 }
101 pRelativeVar->dwKind=VAR_DIRECTMEM;
102
103 pRelativeVar->offset+=array_num;
104 return true;
105}
106
107int Debugging_GetMember( const CClass &objClass,char *member,RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess){
108 int i2;
109
110 //直接参照に切り替え
111 pRelativeVar->offset=(LONG_PTR)Debugging_GetVarPtr(pRelativeVar);
112 pRelativeVar->dwKind=VAR_DIRECTMEM;
113
114 //クラス、配列の構成要素を解析する
115 char VarName[VN_SIZE]; //変数名
116 char array[VN_SIZE]; //第1次配列
117 char lpPtrOffset[VN_SIZE]; //第2次配列
118 char NestMember[VN_SIZE]; //入れ子メンバ
119 CClass::RefType refType;
120 lstrcpy(VarName,member);
121 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember, refType ) ) return 0;
122
123
124 ////////////////////////////
125 // メンバオフセットを取得
126 ////////////////////////////
127
128 int memberIndex;
129 int offset = objClass.GetMemberOffset( VarName, &memberIndex );
130 if(memberIndex==objClass.GetDynamicMembers().size()) return 0;
131
132 CMember *pMember = objClass.GetDynamicMembers()[memberIndex];
133
134
135 //アクセシビリティをチェック
136 if(( bPrivateAccess==0 && pMember->IsPrivate() )||
137 pMember->IsNoneAccess() ){
138 return 0;
139 }
140 else if(bPrivateAccess==0&&pMember->IsProtected())
141 return 0;
142
143 resultType = pMember->GetType();
144
145 //ポインタ変数の場合
146 if( resultType.IsPointer() ){
147 if(pMember->SubScripts[0]==-1){
148 lstrcpy(lpPtrOffset,array);
149 array[0]=0;
150 }
151 }
152 else{
153 if(lpPtrOffset[0]) return 0;
154 }
155
156 pRelativeVar->offset+=offset;
157
158 if(array[0]){
159 //配列オフセット
160 i2=Debugging_GetArray(
161 pMember->SubScripts,
162 array,
163 resultType,
164 &pRelativeVar->offset);
165 if(i2==0){
166 //式エラー
167 return 0;
168 }
169 if(i2==-1){
170 //アクセスエラー
171 return -1;
172 }
173 }
174 else if(pMember->SubScripts[0]!=-1){
175 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR );
176 }
177
178 if(NestMember[0]){
179 //入れ子構造の場合
180
181 if( resultType.IsObject() || resultType.IsStruct() ){
182 if( refType != CClass::Dot ) return 0;
183
184 if( resultType.IsObject() ){
185 extern HANDLE hDebugProcess;
186 LONG_PTR lpData;
187 SIZE_T accessBytes;
188 lpData=Debugging_GetVarPtr(pRelativeVar);
189 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,&pRelativeVar->offset,sizeof(LONG_PTR),&accessBytes)) return -1;
190 pRelativeVar->dwKind=VAR_DIRECTMEM;
191 }
192 }
193 else if( resultType.IsObjectPtr() || resultType.IsStructPtr() ){
194 //構造体ポインタ型メンバ変数
195
196 if(lpPtrOffset[0]){
197 if( refType != CClass::Dot ) return 0;
198
199 //直接参照に切り替え
200 Debugging_SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
201
202 lpPtrOffset[0]=0;
203 }
204 else{
205 if( refType != CClass::Pointer ) return 0;
206
207 extern HANDLE hDebugProcess;
208 LONG_PTR lpData;
209 SIZE_T accessBytes;
210 lpData=Debugging_GetVarPtr(pRelativeVar);
211 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,&pRelativeVar->offset,sizeof(LONG_PTR),&accessBytes)) return -1;
212 pRelativeVar->dwKind=VAR_DIRECTMEM;
213 }
214 }
215
216 i2=Debugging_GetMember(pMember->GetType().GetClass(),
217 NestMember,
218 pRelativeVar,
219 resultType,
220 0);
221 if(i2==0){
222 //式エラー
223 return 0;
224 }
225 if(i2==-1){
226 //アクセスエラー
227 return -1;
228 }
229 }
230
231 if(lpPtrOffset[0]){
232 Debugging_SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
233 }
234
235 return 1;
236}
237int Debugging_GetArray( const int *SubScripts,char *array,const Type &type,LONG_PTR *plpOffset){
238 extern HANDLE hHeap;
239 int i,i2,i3,i4,i5,array_offset;
240 char temporary[VN_SIZE],*pParm[MAX_PARMS];
241
242 for(i=0,i2=0,i3=0;;i++,i2++){
243 if(array[i]=='('){
244 i4=GetStringInPare(temporary+i2,array+i);
245 i+=i4-1;
246 i2+=i4-1;
247 continue;
248 }
249 if(array[i]=='['){
250 i4=GetStringInBracket(temporary+i2,array+i);
251 i+=i4-1;
252 i2+=i4-1;
253 continue;
254 }
255 if(array[i]==','||array[i]=='\0'){
256 if(SubScripts[i3]==-1){
257 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]);
258 return 0;
259 }
260
261 temporary[i2]=0;
262
263 pParm[i3]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
264 lstrcpy(pParm[i3],temporary);
265
266 i3++;
267
268 if(array[i]=='\0'){
269 if(SubScripts[i3]!=-1){
270 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]);
271 return 0;
272 }
273 break;
274 }
275
276 i2=-1;
277 continue;
278 }
279 temporary[i2]=array[i];
280 }
281
282 array_offset=0;
283
284 for(i=i3-1;i>=0;i--){
285 _int64 i64data;
286 Type resultType;
287 bool isMemoryAccessError;
288 if( !StaticCalculation(true, pParm[i],0,&i64data,resultType,1, &isMemoryAccessError ) ){
289 //式エラー
290 return 0;
291 }
292 if(isMemoryAccessError){
293 //アクセスエラー
294 return -1;
295 }
296
297 if(resultType.IsReal()){
298 double dbl;
299 memcpy(&dbl,&i64data,sizeof(double));
300 i64data=(_int64)dbl;
301 }
302 i5=(int)i64data;
303
304 for(i2=i+1,i4=1;i2<i3;i2++) i4*=SubScripts[i2]+1;
305
306 array_offset+=i5*i4;
307
308 HeapDefaultFree(pParm[i]);
309 }
310
311 array_offset *= type.GetSize();
312
313 *plpOffset+=array_offset;
314
315 return 1;
316}
317ULONG_PTR Debugging_GetThisPtrOffset(LONG_PTR obp_Rip){
318 GlobalProc *pUserProc = GetSubFromObp(obp_Rip);
319
320 BOOST_FOREACH( Variable *pVar, pUserProc->localVars ){
321 if( pVar->GetName() == "_System_LocalThis" ){
322 return pVar->offset;
323 }
324 }
325 return 0;
326}
327int Debugging_GetVarOffset( char *variable,RELATIVE_VAR *pRelativeVar, Type &resultType, int *pss){
328 extern HANDLE hDebugProcess;
329 int i2,i3;
330 char member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];
331 LONG_PTR lpData;
332 SIZE_T accessBytes;
333
334 lstrcpy(VarName,variable);
335 CClass::RefType refType;
336 GetVarFormatString(VarName,array,lpPtrOffset,member,refType);
337
338 const int *pSubScripts;
339 bool isArray;
340
341
342 /////////////////
343 // ローカル変数
344 /////////////////
345 if( UserProc::IsLocalAreaCompiling() ){
346 const Variable *pVar = UserProc::CompilingUserProc().localVars.Find( Symbol( VarName ) );
347
348 if( pVar ){
349 //ポインタ変数の場合
350 if( pVar->IsPointer() ){
351 if( !pVar->IsArray() ){
352 lstrcpy(lpPtrOffset,array);
353 array[0]=0;
354 }
355 }
356 else{
357 if(lpPtrOffset[0]) return 0;
358 }
359
360 pRelativeVar->offset = pVar->offset;
361 if( pVar->IsRef() ){
362 pRelativeVar->dwKind=VAR_REFLOCAL;
363 }
364 else{
365 pRelativeVar->dwKind=VAR_LOCAL;
366 }
367 resultType = *pVar;
368 isArray = pVar->IsArray();
369 pSubScripts = pVar->GetSubScriptsPtr();
370 }
371 }
372
373 if(Smoothie::Temp::pCompilingClass){
374 ///////////////////////
375 // クラスメンバの参照
376 ///////////////////////
377
378 if(memicmp(variable,"This.",5)==0){
379 //Thisオブジェクトのメンバを参照するとき
380 SlideString(variable+5,-5);
381 lstrcpy(VarName,variable);
382 }
383 else{
384 //クラス内メンバを参照するとき(通常)
385
386 bool isFound = false;
387 BOOST_FOREACH( CMember *pMember, Smoothie::Temp::pCompilingClass->GetDynamicMembers() ){
388 if( pMember->GetName() == VarName ){
389 isFound = true;
390 break;
391 }
392 }
393 if( !isFound ) goto NonClassMember;
394 }
395
396 /////////////////////////////
397 // thisポインタを取得
398
399 extern HWND hDebugWnd;
400 i2=(int)SendDlgItemMessage(hDebugWnd,IDC_PROCCOMBO,CB_GETCURSEL,0,0);
401 i2=pobj_dti->iProcLevel-i2;
402
403 lpData=Debugging_GetThisPtrOffset(pobj_dti->lplpObp[i2]);
404 if(!lpData){
405 //式エラー
406 return 0;
407 }
408 lpData+=pobj_dti->lplpSpBase[i2];
409 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,&pRelativeVar->offset,sizeof(LONG_PTR),&accessBytes)){
410 //メモリにアクセスできないとき
411 return -1;
412 }
413
414 pRelativeVar->dwKind=VAR_DIRECTMEM;
415
416 i3=Debugging_GetMember(*Smoothie::Temp::pCompilingClass,variable,pRelativeVar,resultType,1);
417 if(i3==0){
418 //式エラー
419 return 0;
420 }
421 if(i3==-1){
422 //アクセスエラー
423 return -1;
424 }
425
426 return 1;
427 }
428
429NonClassMember:
430
431 {
432 ///////////////////
433 // グローバル変数
434 ///////////////////
435
436 const Variable *pVar = globalVars.Find( VarName );
437 if( !pVar ){
438 //一致しないとき
439 return 0;
440 }
441
442 //ポインタ変数の場合
443 if( pVar->IsPointer() ){
444 if( !pVar->IsArray() ){
445 lstrcpy(lpPtrOffset,array);
446 array[0]=0;
447 }
448 }
449 else{
450 if(lpPtrOffset[0]) return 0;
451 }
452
453 pRelativeVar->offset=pVar->offset;
454 if(pVar->IsRef()) pRelativeVar->dwKind=VAR_REFGLOBAL;
455 else pRelativeVar->dwKind=VAR_GLOBAL;
456 resultType = *pVar;
457 isArray = pVar->IsArray();
458 pSubScripts=pVar->GetSubScriptsPtr();
459 }
460
461
462 if(array[0]==0&&isArray){
463 //配列の先頭ポインタを示す場合
464 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR );
465 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM*sizeof(int));
466 return 1;
467 }
468
469 if(array[0]){
470 i3=Debugging_GetArray(pSubScripts,array,resultType,&pRelativeVar->offset);
471 if(i3==0){
472 //式エラー
473 return 0;
474 }
475 if(i3==-1){
476 //アクセスエラー
477 return -1;
478 }
479 }
480 if(member[0]){
481 if( resultType.IsObject() || resultType.IsStruct() ){
482 //実態オブジェクトのメンバを参照(obj.member)
483 if( refType != CClass::Dot ){
484 return 0;
485 }
486
487 i3=Debugging_GetMember(resultType.GetClass(),member,pRelativeVar,resultType,0);
488 if(i3==0){
489 //式エラー
490 return 0;
491 }
492 if(i3==-1){
493 //アクセスエラー
494 return -1;
495 }
496 }
497 else if( resultType.IsObjectPtr() || resultType.IsStructPtr() ){
498 //ポインタオブジェクトが示すメンバを参照
499 if(lpPtrOffset[0]){
500 //pObj[n].member
501 if( refType != CClass::Dot ) return 0;
502 Debugging_SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
503
504 i3=Debugging_GetMember(resultType.GetClass(),member,pRelativeVar,resultType,0);
505 if(i3==0){
506 //式エラー
507 return 0;
508 }
509 if(i3==-1){
510 //アクセスエラー
511 return -1;
512 }
513 }
514 else{
515 //pObj->member
516 if( refType != CClass::Pointer ) return 0;
517
518 pRelativeVar->offset=Debugging_GetVarPtr(pRelativeVar);
519 pRelativeVar->dwKind=VAR_DIRECTMEM;
520
521 if(!ReadProcessMemory(hDebugProcess,(void *)pRelativeVar->offset,&lpData,sizeof(LONG_PTR),&accessBytes)) return -1;
522 pRelativeVar->offset=lpData;
523
524 i3=Debugging_GetMember(resultType.GetClass(),member,pRelativeVar,resultType,0);
525 if(i3==0){
526 //式エラー
527 return 0;
528 }
529 if(i3==-1){
530 //アクセスエラー
531 return -1;
532 }
533 }
534 }
535 else{
536 return 0;
537 }
538 return 1;
539 }
540
541 if(lpPtrOffset[0]){
542 if(!Debugging_SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset)) return 0;
543 }
544
545 return 1;
546}
Note: See TracBrowser for help on using the repository browser.