source: dev/BasicCompiler_Common/WatchList.cpp@ 108

Last change on this file since 108 was 100, checked in by dai_9181, 18 years ago

名前空間機能をグローバル関数に適用。

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