-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.cpp
executable file
·3571 lines (3211 loc) · 85.9 KB
/
debugger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "debugger.h"
#include "gui.h"
#include "resource.h"
#include "ccore.h"
#include "machine.h"
using namespace std;
extern ICore *cpu;
DebugWindow::DebugWindow() : DebugLog(){
_window=NULL;
_nItems=0;
_searchWord=NULL;
thread1=0;
InitializeCriticalSection(&_cr);
Reset();
}
DebugWindow::~DebugWindow(){
//printf("DebugWWindow Destroy\n");
if(thread1)
pthread_join(thread1,NULL);
if(_searchWord)
delete []_searchWord;
_searchWord=NULL;
}
void DebugWindow::_calc_lb_item_height(HWND win){
#ifdef __WIN32__
#else
GtkTreeViewColumn *column;
int x,y,width,i;
column = gtk_tree_view_get_column(GTK_TREE_VIEW(win),0);
gtk_tree_view_column_cell_get_size(column,NULL,&x,&y,&width,&_itemHeight);
gtk_widget_style_get(GTK_WIDGET(win),"expander-size", &i,NULL);
x = 1;
//i=0;
if(i >= _itemHeight){
_itemHeight = i;
x = 2;
}
i = 0;
gtk_widget_style_get(GTK_WIDGET(win),"vertical-separator", &i,NULL);
i *= x;
_itemHeight += i;
#endif
}
void DebugWindow::OnMoveResize(u32 id,HWND,int w,int h){
HWND win;
#ifdef __WIN32__
#else
GtkAllocation al;
if(!_window)
return;
if(!(win=GetDlgItem(_window,RES(IDC_DEBUG_LB_DIS))))
return;
_calc_lb_item_height(win);
gtk_widget_get_allocation(win,&al);
_nItems=ceil((float)(h-10)/ (float)_itemHeight / 2.0f);
//win=GetDlgItem(_window,RES(5890));
//int i=gtk_paned_get_position(GTK_PANED(win));
//gtk_widget_get_allocation(win,&al);
//printf("onmoveresize %d %d %d %d\n",_itemHeight,_nItems,al.height,e->height);
//memset(&_views,0,sizeof(_views));
#endif
_memPages._reset();
_invalidateView();
_adjustCodeViewport((u32)-1,_iCurrentView);
}
int DebugWindow::_create_treeview_model(HWND win){
#ifdef __WIN32__
#else
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkListStore *list_store;
int height;
list_store = gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_INT);
gtk_tree_view_set_model(GTK_TREE_VIEW(win),GTK_TREE_MODEL(list_store));
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(win), FALSE);
gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(win),TRUE);
column = gtk_tree_view_column_new();
g_object_set(column,"sizing",GTK_TREE_VIEW_COLUMN_FIXED,NULL);
gtk_tree_view_column_set_title(column,"Text");
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer,"ypad",0,"yalign",0.5,"family","Monospace","family-set",true,NULL);
gtk_cell_renderer_set_fixed_size(renderer,-1,12);
gtk_tree_view_column_pack_start(column,renderer,TRUE);
gtk_tree_view_column_set_attributes(column,renderer,"text",0,NULL);
gtk_tree_view_append_column((GtkTreeView *)win, column);
gtk_widget_get_preferred_height(win,NULL,&height);
_calc_lb_item_height(win);
_nItems=ceil((float)(height-10)/(float)_itemHeight);
g_object_unref (list_store);
#endif
return 0;
}
int DebugWindow::Init(HWND w){
HWND win;
#ifdef __WIN32__
#else
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkListStore *list_store;
_window=w;
gtk_widget_hide_on_delete(_window);
DebugLog::Init(w);
pthread_create(&thread1, NULL, thread1_func, (void*) this);
win=GetDlgItem(_window,RES(IDC_DEBUG_LB_DIS));
_create_treeview_model(win);
win=GetDlgItem(_window,RES(IDC_DEBUG_LB_BP));
list_store = gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_INT);
gtk_tree_view_set_model(GTK_TREE_VIEW(win),GTK_TREE_MODEL(list_store));
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(win), FALSE);
gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(win),TRUE);
column = gtk_tree_view_column_new();
g_object_set(column,"sizing",GTK_TREE_VIEW_COLUMN_FIXED,NULL);
gtk_tree_view_column_set_title(column,"Text");
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer,"ypad",0,"yalign",0.5,"family","Monospace","family-set",true,NULL);
//gtk_cell_renderer_set_fixed_size(renderer,-1,12);
gtk_tree_view_column_pack_start(column,renderer,TRUE);
gtk_tree_view_column_set_attributes(column,renderer,"text",0,NULL);
gtk_tree_view_append_column((GtkTreeView *)win, column);
#ifdef _DEBUG
GtkWidget *m,*mm;
win=gtk_menu_bar_new();
gtk_widget_set_name(win,RES(IDC_DEBUG_MENUBAR));
w=gtk_bin_get_child(GTK_BIN(_window));
gtk_container_add(GTK_CONTAINER(w),win);
gtk_box_reorder_child(GTK_BOX(w),win,0);
m=gtk_menu_new();
w=gtk_menu_item_new_with_label ("Debug");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), m);
gtk_menu_shell_append (GTK_MENU_SHELL (win), w);
w = gtk_menu_item_new_with_label ("Break on...");
gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
mm=gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), mm);
w = gtk_check_menu_item_new_with_label ("IRQ");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,5));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_check_menu_item_new_with_label ("BREAK");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,6));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_check_menu_item_new_with_label ("DEVICE");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,7));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_check_menu_item_new_with_label ("VBLANK");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,8));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_menu_item_new_with_label ("Level");
gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
mm=gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), mm);
w = gtk_radio_menu_item_new_with_label (NULL,"Off");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,1));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
GSList *group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(w));
w = gtk_radio_menu_item_new_with_label (group,"On");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,2));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
//gtk_radio_menu_item_set_group(GTK_RADIO_MENU_ITEM(w),group);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_check_menu_item_new_with_label ("DEV");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,9));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_menu_item_new_with_label ("Notes");
gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
mm=gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), mm);
w = gtk_menu_item_new_with_label ("Load");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,100));
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_menu_item_new_with_label ("Save");
gtk_menu_shell_append (GTK_MENU_SHELL (mm), w);
gtk_widget_show(w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
w = gtk_menu_item_new_with_label ("Close");
gtk_widget_set_name(w,MAKELONG(IDC_DEBUG_MENUBAR,IDC_DEBUG_CLOSE));
gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
g_signal_connect (w, "activate", G_CALLBACK (::on_menuitem_select), NULL);
/*
m=gtk_viewport_new(NULL,NULL);
w=gtk_text_view_new();
gtk_text_view_set_monospace(GTK_TEXT_VIEW(w),true);
gtk_text_view_set_editable(GTK_TEXT_VIEW(w),true);
gtk_widget_set_name(w,RES(IDC_DEBUG_TV1));
g_signal_connect(G_OBJECT(w),"populate-popup",G_CALLBACK(on_menu_init),0);
gtk_container_add (GTK_CONTAINER (m),w);
w = gtk_scrolled_window_new(NULL,NULL);
gtk_container_add (GTK_CONTAINER (w),m);
m=gtk_label_new ("Notes");
gtk_notebook_append_page(GTK_NOTEBOOK (GetDlgItem(_window,RES(IDC_DEBUG_TAB1))), w, m);
mm=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
m=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
w=gtk_combo_box_text_new();
gtk_widget_set_name(w,RES(IDC_DEBUG_LAYERS_CB));
gtk_container_add (GTK_CONTAINER (m),w);
{
GtkAdjustment *adjustment;
adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
w = gtk_spin_button_new (adjustment, 1.0, 0);
gtk_container_add (GTK_CONTAINER (m),w);
}
gtk_container_add (GTK_CONTAINER (mm),m);
m=gtk_viewport_new(NULL,NULL);
w=gtk_drawing_area_new();
gtk_widget_set_hexpand(w,true);
gtk_widget_set_vexpand(w,true);
gtk_widget_set_name(w,RES(IDC_DEBUG_DA1));
g_signal_connect(G_OBJECT(w),"draw",G_CALLBACK(on_paint),0);
gtk_container_add (GTK_CONTAINER (m),w);
w = gtk_scrolled_window_new(NULL,NULL);
gtk_container_add (GTK_CONTAINER (w),m);
gtk_container_add (GTK_CONTAINER (mm),w);
m=gtk_label_new ("Layers");
gtk_notebook_append_page(GTK_NOTEBOOK (GetDlgItem(_window,RES(IDC_DEBUG_TAB1))), mm, m);
mm=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
m=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
w=gtk_combo_box_text_new();
gtk_widget_set_name(w,RES(IDC_DEBUG_PALETTE_CB));
gtk_container_add (GTK_CONTAINER (m),w);
gtk_container_add (GTK_CONTAINER (mm),m);
w=gtk_drawing_area_new();
gtk_widget_set_hexpand(w,true);
gtk_widget_set_vexpand(w,true);
gtk_widget_set_name(w,RES(IDC_DEBUG_DA2));
g_signal_connect(G_OBJECT(w),"draw",G_CALLBACK(on_paint),0);
gtk_container_add (GTK_CONTAINER (mm),w);
m=gtk_label_new ("Palette");
gtk_notebook_append_page(GTK_NOTEBOOK (GetDlgItem(_window,RES(IDC_DEBUG_TAB1))), mm, m);*/
#endif
#endif
return 0;
}
void DebugWindow::Update(){
#ifdef _DEBUG
int i,nn;
char *p,*pp;
HWND win;
if(!_window || !cpu)
return;
//return;
p=(char *)&_memPages;
i=cpu->Dump(&p);
//p=NULL;
if(!p || i< 1)
goto A;
pp=p;
while(*pp){
#ifdef __WIN32__
win = GetDlgItem(_window,atoi(pp));
#else
win = GetDlgItem(_window,pp);
#endif
pp += strlen(pp)+5;
nn = strlen(pp);
if(win){
#ifdef __WIN32__
#else
GtkTextBuffer *b= gtk_text_view_get_buffer((GtkTextView *)win);
if(*pp){
char *s,*ss;
s=pp;
gtk_text_buffer_set_text(b,s,0);
while((ss=strtok(s,"\\"))){
GtkTextIter ei;
char tag[2];
tag[0]=tag[1]=0;
if((u64)ss - (u64)pp)
tag[0]=*ss++;
gtk_text_buffer_get_end_iter(b,&ei);
switch(tag[0]){
case 'b':
case 'u':
gtk_text_buffer_insert_with_tags_by_name(b,&ei,ss,-1,tag,NULL);
break;
default:
gtk_text_buffer_insert(b,&ei,ss,-1);
break;
}
s=NULL;
}
}
else
gtk_text_buffer_set_text(b,"",-1);
#endif
}
pp += nn+1;
}
A:
{
u32 pc;
cpu->Query(ICORE_QUERY_PC,&pc);
_adjustCodeViewport(pc,DBV_RUN);
_updateAddressInfo(pc);
}
win=GetDlgItem(_window,RES(IDC_DEBUG_LB_BP));
#ifdef __WIN32__
#else
GtkListStore *model = (GtkListStore *)gtk_tree_view_get_model((GtkTreeView *)win);
if(model != NULL)
gtk_list_store_clear(model);
{
u64 *bp;
char c[330];
HWND w=NULL;
if(!cpu->Query(ICORE_QUERY_BREAKPOINT_ENUM,&bp) && bp){
pp=(char *)bp;
for(;*bp;bp++){
HWND e;
char s='*',ec[][3]={"!=","==","<",">"},*p__;
if(!w){
w = gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(GetDlgItem(_window,RES(5517))));
if(w){
GList *children = gtk_container_get_children(GTK_CONTAINER(w));
for(i=0;children;children = g_list_next(children),i++){
gtk_container_remove(GTK_CONTAINER(w),GTK_WIDGET(children->data));
}
}
else
w=gtk_menu_new();
}
if(!(*bp & 0x8000000000000000ULL))
s=32;
sprintf(c,"%c 0x%08X",s,(u32)*bp);
e=gtk_menu_item_new_with_label (c);
gtk_menu_shell_append (GTK_MENU_SHELL (w), e);
if(ISMEMORYBREAKPOINT(*bp)){//memory
char s_[][4]={"R","W","R/W"};
char s__[][3]={"B","W","L","*"};
sprintf(c,"%c %c 0x%X %s %s %s",s,SR(*bp,34) & 1 ?'M': 'm',(u32)*bp,s__[SR(*bp,38) & 3],s_[SR(*bp,60)&3],ec[SR(*bp,35) & 7]);
}
else if(*bp & 0x800000000000ULL){
u32 v=SR(*bp,32);
if(v&0x40000000)
sprintf(c,"%c 0x%X r%d %s %x",s,(u32)*bp,SR(v,3) & 0x1f, ec[v&7],SR(v,8)&0x7f|SR(v&0x3fff0000,9));
else
sprintf(c,"%c 0x%X r%d %s r%d",s,(u32)*bp,SR(v,3) & 0x1f, ec[v&7],SR(v,8)&0x1f);
}
else
sprintf(c,"%c 0x%X %5d %5d",s,(u32)*bp,(u16)SR(*bp,32),(u16)(SR(*bp,48) & 0x7fff));
if(!_getNoteFromAddress((u32)*bp,&p__)){
for(i=strlen(c);*p__ && *p__!=10;p__++,i++)
c[i]=*p__;
c[i]=0;
}
HTREEITEM pi=AddListItem(win,c);
delete pi;
}
free(pp);
if(w){
gtk_widget_show_all(w);
gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(GetDlgItem(_window,RES(5517))),w);
}
}
}
win= GetDlgItem(_window,RES(7101));
if(win){
char c[20];
sprintf(c,"LA: %x",(u32)_memPages._lastAccessAddress);
gtk_label_set_text(GTK_LABEL(win),c);
}
win= GetDlgItem(_window,RES(7100));
if(win){
char c[20];
sprintf(c,"EA: %x",(u32)_memPages._editAddress);
gtk_label_set_text(GTK_LABEL(win),c);
}
#endif
DebugLog::Update();
DebugWindow::_updateToolbar();
if(p)
delete []p;
#endif
}
int DebugWindow::_getNoteFromAddress(u32 pc,char **p){
char *pp;
int nn;
HWND w;
#ifdef _DEBUG
#ifdef __WIN32__
#else
GtkTextIter s,e;
GtkTextBuffer *b;
if(!(w=GetDlgItem(_window,RES(IDC_DEBUG_TV1))))
return -1;
if(!(b = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w))))
return -2;
gtk_text_buffer_get_start_iter(b,&s);
gtk_text_buffer_get_end_iter(b,&e);
if(!(pp=gtk_text_buffer_get_text(b,&s,&e,false)))
return -3;
nn=0;
while(*pp && nn==0){
char *c=pp;
int n=0,i=0;
while(c[n]){
if(i==0 && c[n]=='\t'){
if(n){
u32 d;
sscanf(c,"%x",&d);
if(d==pc){
*p=&c[n];
nn=n;
goto A;
}
}
i=1;
}
n++;
if(c[n]=='\n'){
n++;
break;
}
}
pp+=n;
}
return -4;
A:
#endif
#endif
return 0;
}
int DebugWindow::_updateAddressInfo(u32 pc){
char *p;
if(_getNoteFromAddress(pc,&p)) return -1;
ShowToastMessage(p);
return 0;
}
void DebugWindow::_adjustCodeViewport(u32 adr,int v){
HWND win;
u32 _data[10];
LPDISVIEW pv;
if(!(win=GetDlgItem(_window,RES(IDC_DEBUG_SB))) || !GTK_IS_WIDGET(win) || !cpu)
return;
if(adr==(u32)-1)
cpu->Query(ICORE_QUERY_PC,&adr);
if(v==-1)
v=_iCurrentView;
if(v==-1)
v=DBV_VIEW;
pv=&_views[v];
if(adr<pv->uAdr || adr > (pv->uAdr+pv->uSize) || _iCurrentView != v){
_data[0]=adr;
*((u64 *)&_data[1])=(u64)&_data[3];
if(cpu->Query(ICORE_QUERY_ADDRESS_INFO,_data))
return;
pv->uAdr=_data[3];
pv->uSize=_data[4];
_data[9]=(u32)-1;
if(cpu->Query(ICORE_QUERY_NEXT_STEP,&_data[9]))
_data[9]=2;
//printf("_adjustCodeViewport: %d %x %x %d %x\n",v,_data[3],_data[4],_data[9],adr);
#ifdef __WIN32__
#else
gtk_range_set_range(GTK_RANGE(win),0,_data[4]-_nItems*_data[9]);
gtk_range_set_increments(GTK_RANGE(win),_data[9],_nItems*_data[9]);
#endif
if(v != DBV_VIEW)
memcpy(&_views[DBV_VIEW],pv,sizeof(DISVIEW));
}
#ifdef __WIN32__
#else
gtk_range_set_value(GTK_RANGE(win),adr & (pv->uSize-1));
#endif
_fillCodeWindow(adr,v);
}
void DebugWindow::_fillCodeWindow(u32 adr,int cv){
HWND win;
LPDISVIEW pv;
char *p;
if(!cpu || !(win=GetDlgItem(_window,RES(IDC_DEBUG_LB_DIS))))
return;
if(!(p = new char[1000]))
return;
#ifdef __WIN32__
#else
g_object_freeze_notify(G_OBJECT(win));
A:
pv = &_views[cv];
if(adr<=pv->uStart || adr > pv->uEnd || _iCurrentView!=cv){
u64 *bp;
_iCurrentView=cv;
GtkListStore *model = (GtkListStore *)gtk_tree_view_get_model((GtkTreeView *)win);
if(model != NULL)
gtk_list_store_clear(model);
pv->uStart=adr;
if(!cpu || cpu->Query(ICORE_QUERY_BREAKPOINT_ENUM,&bp))
bp=NULL;
// printf("fi %x %d ",adr,_nItems);
for(int i=0;i<_nItems && cpu;i++){
u64 *pb,d[]={(u64)&p[4],(u64)&adr};
*((u64 *)p)=0x2020202020;
for(pb=bp;pb && *pb;pb++){
if((u32)*pb != adr)
continue;
p[0]=!(*pb & 0x8000000000000000) ? '+' : '*';
break;
}
cpu->Query(ICORE_QUERY_DISASSEMBLE,d);
HTREEITEM pi=AddListItem(win,p);
if(pi)
delete pi;
}
//printf(" %x\n",adr);
pv->uEnd=adr-1;
}
GtkTreeSelection *select = gtk_tree_view_get_selection((GtkTreeView *)win);
if(select != NULL && _iCurrentView==DBV_RUN &&& cpu){
GtkListStore *model;
GtkTreeIter iter;
model = (GtkListStore *)gtk_tree_view_get_model((GtkTreeView *)win);
if(model != NULL){
char *string;
int n,i;
cpu->Query(ICORE_QUERY_PC,&_pc);
n=0;
i = gtk_tree_model_get_iter_first((GtkTreeModel *)model,&iter);
while(i){
n=1;
gtk_tree_model_get((GtkTreeModel *)model, &iter, 0, &string, -1);
sscanf(string+4,"%x",&i);
free(string);
if((u32)i == _pc){
gtk_tree_selection_select_iter(select,&iter);
n=2;
break;
}
i=gtk_tree_model_iter_next((GtkTreeModel *)model,&iter);
}
if(n==1 && _pc){
//printf("lino %x %x %d\n\n",adr,_pc,i);
adr=_pc;
memset(pv,0,sizeof(DISVIEW));
goto A;
}
}
}
g_object_thaw_notify(G_OBJECT(win));
//printf("fl %x %x\n",pv->uStart,pv->uEnd);
#endif
delete []p;
}
HTREEITEM DebugWindow::AddListItem(HWND listbox, char *sText){
HTREEITEM iter;
#ifdef __WIN32__
#else
GtkListStore *model;
// g_object_freeze_notify(G_OBJECT(listbox));
model = (GtkListStore *)gtk_tree_view_get_model((GtkTreeView *)listbox);
if(model == NULL)
return NULL;
iter = new GtkTreeIter[1];
if(iter == NULL)
return NULL;
gtk_list_store_append(model,iter);
gtk_list_store_set(model,iter,0,sText, -1);
gtk_widget_queue_resize_no_redraw(listbox);
//g_object_thaw_notify(G_OBJECT(listbox));
#endif
return iter;
}
HTREEITEM DebugWindow::GetListItem(HWND w,u32 n){
HTREEITEM piter;
u32 i;
#ifdef __WIN32__
#else
GtkListStore *model;
model = (GtkListStore *)gtk_tree_view_get_model((GtkTreeView *)w);
if(model == NULL)
return NULL;
if((piter = new GtkTreeIter[1]) == NULL)
return NULL;
gtk_tree_model_get_iter_first((GtkTreeModel *)model,piter);
for(i=1;i<=n;i++){
if(!gtk_tree_model_iter_next((GtkTreeModel *)model,piter))
goto _err;
}
return piter;
_err:
delete piter;
#endif
return NULL;
}
HTREEITEM DebugWindow::GetSelectedListItem(HWND w){
HTREEITEM piter;
#ifdef __WIN32__
#else
GtkListStore *model;
GtkTreeSelection *select;
select = gtk_tree_view_get_selection((GtkTreeView *)w);
if(select == NULL)
return NULL;
if((piter = new GtkTreeIter[1]) == NULL)
return NULL;
if(!gtk_tree_selection_get_selected(select,(GtkTreeModel **)&model,piter))
goto _err;
return piter;
_err:
delete piter;
#endif
return NULL;
}
u32 DebugWindow::GetSelectedListItemIndex(HWND w){
u32 res;
#ifdef __WIN32__
#else
GtkListStore *model;
GtkTreeSelection *select;
GtkTreeIter *piter,iter;
GtkTreePath *path,*path1;
path=path1=NULL;
select = gtk_tree_view_get_selection((GtkTreeView *)w);
if(select == NULL)
return res;
if((piter = new GtkTreeIter[1]) == NULL)
return res;
if(!gtk_tree_selection_get_selected(select,(GtkTreeModel **)&model,piter))
goto E;
path = gtk_tree_model_get_path((GtkTreeModel *)model,piter);
if(path == NULL)
goto E;
if(!gtk_tree_model_get_iter_first((GtkTreeModel *)model,&iter))
goto E;
res = 0;
do{
path1 = gtk_tree_model_get_path((GtkTreeModel*)model,&iter);
if(path1 != NULL){
int x = gtk_tree_path_compare(path,path1);
gtk_tree_path_free(path1);
if(x == 0)
goto A;
}
if(!gtk_tree_model_iter_next((GtkTreeModel *)model,&iter))
break;
res++;
}while(1);
E:
res=(u32)-1;
A:
if(piter)
delete piter;
if(path)
gtk_tree_path_free(path);
#endif
return res;
}
void DebugWindow::_adjustScrollViewport(int v,HWND w,double *r,u32 attr){
#ifdef __WIN32__
#else
double d[6];
int i;
GtkAdjustment *control;
if(!w || (control = gtk_range_get_adjustment(GTK_RANGE(w))) == NULL)
return;
i=1;
if(v<0){
i*=-1;
v*=-1;
}
g_object_get((gpointer)control,"lower",&d[0],"upper",&d[1],"value",&d[2],"page-increment",&d[3],"step-increment",&d[4],NULL);
d[5]=(d[2]+d[v]*i);
if(r)
*r=d[5];
if(attr&1)
gtk_range_set_value(GTK_RANGE(w),d[5]);
#endif
}
void DebugWindow::OnKeyScrollEvent(int v,u32 id,HWND w){//fixme
double d;
u32 i;
#ifdef __WIN32__
#else
if(!w) w=GetDlgItem(_window,RES(IDC_DEBUG_SB));
if(id==IDC_DEBUG_LB_DIS){
_adjustScrollViewport(v,w,&d,0);
_adjustCodeViewport((u32)d+_views[DBV_VIEW].uAdr,DBV_VIEW);
}
else{
w=gtk_window_get_focus(GTK_WINDOW(_window));
i = (u32)(u64)g_object_get_data(G_OBJECT(w),"type");
if(i==2){
_adjustScrollViewport(v,_memPages._sb);
// g_signal_emit_by_name((gpointer)_memPages._sb,"scroll-event",0,d,&d);
if(!_memPages._onScroll(id,_memPages._sb,GTK_SCROLL_END))
Update();
}
}
#endif
}
int DebugWindow::OnKeyDown(int key,u32 wParam){
u32 id=(u32)-1;
#ifdef __WIN32__
#else
HWND w=gtk_window_get_focus(GTK_WINDOW(_window));
if(w){
char *name = (char *)gtk_widget_get_name(w);
if(name)
id=atoi(name);
}
switch(id){
case IDC_DEBUG_DA1*10:{
int v;
switch(key){
case GDK_KEY_Page_Down:
v=3;
break;
case GDK_KEY_Page_Up:
v=-3;
break;
case GDK_KEY_Down:
v=4;
break;
case GDK_KEY_Up:
v=-4;
break;
}
_adjustScrollViewport(v,w,0);
gtk_container_set_focus_child(GTK_CONTAINER(_window),w);
}
return 0;
}
switch(key){
case GDK_KEY_Page_Down:
OnKeyScrollEvent(3,id);
break;
case GDK_KEY_Page_Up:
OnKeyScrollEvent(-3,id);
break;
case GDK_KEY_Down:
OnKeyScrollEvent(4,id);
break;
case GDK_KEY_Up:
OnKeyScrollEvent(-4,id);
break;
case GDK_KEY_F12://Run framee
DebugMode(8);
break;
case GDK_KEY_F3://Stop
if(id==3102)
SearchWord();
else
DebugMode(1);
break;
case GDK_KEY_F6:
DebugMode(6);
break;
case GDK_KEY_F5://Go
DebugMode(5);
break;
case GDK_KEY_F4:
DebugMode(3);//Step In
break;
case GDK_KEY_F7://Run to cuursor
DebugMode(4);
break;
case GDK_KEY_F8:
DebugMode(7);//Step Over
break;
case GDK_KEY_F9:
if(id==IDC_DEBUG_LB_BP){
u32 d[2];
if((d[0]=GetSelectedListItemIndex(w)) != (u32)-1){
d[1]=(u32)-1;
if(!cpu->Query(ICORE_QUERY_SET_BREAKPOINT_STATE,d)){
_invalidateView();
Update();
}
}
}
else{
u32 adr,d[2];
if(!GetAddressFromSelectedItem(GetDlgItem(_window,RES(IDC_DEBUG_LB_DIS)),&adr)){
d[0]=adr;
d[1]=0;
if(!cpu->Query(ICORE_QUERY_BREAKPOINT_ADD,(void *)d)){
Update();
w=GetDlgItem(_window,RES(IDC_DEBUG_TAB1));
int i = gtk_notebook_get_n_pages(GTK_NOTEBOOK (w))-5;
gtk_notebook_set_current_page(GTK_NOTEBOOK(w),i);
w=GetDlgItem(_window,RES(IDC_DEBUG_LB_BP));
HTREEITEM im=GetListItemFromAddress(w,adr);
if(im){
GtkTreeSelection *select = gtk_tree_view_get_selection((GtkTreeView *)w);
gtk_tree_selection_select_iter(select,im);
delete im;
}
}
}
}
break;
case GDK_KEY_Delete:
//printf("f %d %d\n",id,key);
if(id==IDC_DEBUG_LB_BP){
//cpu->Query(ICORE_QUERY_BREAKPOINT_DEL,(void *)adr);
u32 d[2];
d[0]=GetSelectedListItemIndex(w);
d[1]=(u32)-1;
if(!cpu->Query(ICORE_QUERY_BREAKPOINT_DEL,d)){
_invalidateView();
Update();
}
}
break;
}
#endif
return 0;
}
int DebugWindow::DebugMode(int m,u64 v,u64 vv){
#ifdef _DEBUG
switch(m){
case 0:
BC(_status,S_DEBUG|S_PAUSE);
if(machine)
machine->OnEvent(ME_LEAVEDEBUG);
gtk_widget_hide(_window);
break;
case 1:
if(!v || BT(_status,v)){
BS(_status,S_DEBUG|S_PAUSE);
BC(_status,S_DEBUG_NEXT|S_DEBUG_NEXT_FRAME);
_pc=0;
#ifdef __WIN32__
#else
gtk_widget_show_all(_window);
gtk_notebook_set_current_page(GTK_NOTEBOOK(GetDlgItem(_window,RES(IDC_DEBUG_TAB1))),0);
#endif
OnChangeCpu(vv);
if(machine) machine->OnEvent(ME_ENTERDEBUG);
Update();
FlashWindow(_window,2);
}
break;
case 5://Run
if(!BT(_status,S_DEBUG))
return -1;
BS(_status,S_DEBUG_NEXT);
_pc=0x1000000000000002;
DebugWindow::_updateToolbar();
break;
case 2:
BC(_status,S_PAUSE);
DebugWindow::_updateToolbar();
break;
case 3:{//next F4
u32 n[2]={2,0};
if(!BT(_status,S_DEBUG))
return -1;
BS(_status,S_DEBUG_NEXT);
// n=(u32)2;
if(cpu->Query(ICORE_QUERY_NEXT_STEP,n)){
n[0]=2;
//cpu->Query(ICORE_QUERY_PC,&i);
//n+=i;
}
//printf("ICORE_QUERY_NEXT_STEP %x\n",n[0]);
_pc = (u64)n[0];
DebugWindow::_updateToolbar();
}
break;
case 4://run to cursor F7
if(!BT(_status,S_DEBUG))
return -1;
BS(_status,S_DEBUG_NEXT);
{
u32 i;
if(!GetAddressFromSelectedItem(GetDlgItem(_window,RES(IDC_DEBUG_LB_DIS)),&i))
_pc = 0x8000000000000000|(u32)i;
}
DebugWindow::_updateToolbar();
break;