source: dev/BasicCompiler32/WatchList.cpp@ 8

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