forked from radareorg/radare2
/
panels.c
1491 lines (1418 loc) · 39.9 KB
/
panels.c
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
/* Copyright radare2 2014-2018 - Author: pancake, vane11ope */
// pls move the typedefs into roons and rename it -> RConsPanel
#include <r_core.h>
#define LIMIT 256
#define PANEL_TITLE_SYMBOLS "Symbols"
#define PANEL_TITLE_STACK "Stack"
#define PANEL_TITLE_REGISTERS "Registers"
#define PANEL_TITLE_REGISTERREFS "RegisterRefs"
#define PANEL_TITLE_DISASSEMBLY "Disassembly"
#define PANEL_TITLE_NEWFILES "New files"
#define PANEL_TITLE_INFO "Info"
#define PANEL_TITLE_DATABASE "Database"
#define PANEL_TITLE_HEXDUMP "Hexdump"
#define PANEL_TITLE_FUNCTIONS "Functions"
#define PANEL_TITLE_COMMENTS "Comments"
#define PANEL_TITLE_ENTROPY "Entropy"
#define PANEL_TITLE_DRX "DRX"
#define PANEL_TITLE_SECTIONS "Sections"
#define PANEL_TITLE_STRINGS "Strings"
#define PANEL_TITLE_MAPS "Maps"
#define PANEL_TITLE_MODULES "Modules"
#define PANEL_TITLE_BACKTRACE "Backtrace"
#define PANEL_TITLE_BREAKPOINTS "Breakpoints"
#define PANEL_TITLE_IMPORTS "Imports"
#define PANEL_TITLE_CLIPBOARD "Clipboard"
#define PANEL_TITLE_FCNINFO "FcnInfo"
#define PANEL_CMD_SYMBOLS "isq"
#define PANEL_CMD_STACK "px 256@r:SP"
#define PANEL_CMD_REGISTERS "dr="
#define PANEL_CMD_REGISTERREFS "drr"
#define PANEL_CMD_DISASSEMBLY "pd $r"
static const int layoutMaxCount = 2;
enum {
LAYOUT_DEFAULT = 0,
LAYOUT_BALANCE = 1
};
static const char *menus[] = {
"File", "Edit", "View", "Tools", "Search", "Debug", "Analyze", "Help",
NULL
};
static const char *menus_File[] = {
"New", "Open", "Close", ".", "Sections", "Strings", "Symbols", "Imports", "Info", "Database", ".", "Quit",
NULL
};
static const char *menus_Edit[] = {
"Copy", "Paste", "Clipboard", "Write String", "Write Hex", "Write Value", "Assemble", "Fill", "io.cache",
NULL
};
static const char *menus_View[] = {
"Hexdump", "Disassembly", "Graph", "FcnInfo", "Functions", "Comments", "Entropy", "Colors",
NULL
};
static const char *menus_Tools[] = {
"Assembler", "Calculator", "R2 Shell", "System Shell",
NULL
};
static const char *menus_Search[] = {
"String", "ROP", "Code", "Hexpairs",
NULL
};
static const char *menus_Debug[] = {
"Registers", "RegisterRefs", "DRX", "Breakpoints",
"Watchpoints", "Maps", "Modules",
"Backtrace",
".",
"Continue", "Cont until.",
"Step", "Step Over",
NULL
};
static const char *menus_Analyze[] = {
"Function", "Program", "Calls", "References",
NULL
};
static const char *menus_Help[] = {
"Fortune", "Commands", "2048", "License", ".", "About",
NULL
};
static const char **menus_sub[] = {
menus_File,
menus_Edit,
menus_View,
menus_Tools,
menus_Search,
menus_Debug,
menus_Analyze,
menus_Help,
NULL
};
static void layoutMenu(RPanel *panel);
static void panelPrint(RCore *core, RConsCanvas *can, RPanel *panel, int color);
static void addPanelFrame(RCore* core, RPanels* panels, const char *title, const char *cmd);
static bool checkFunc(RCore *core);
static void cursorLeft(RCore *core);
static void cursorRight(RCore *core);
static void delCurPanel(RPanels *panels);
static void doPanelsRefresh(RCore *core);
static bool handleCursorMode(RCore *core, const int key);
static void handleUpKey(RCore *core);
static void handleDownKey(RCore *core);
static void handleLeftKey(RCore *core);
static void handleRightKey(RCore *core);
static bool handleEnterKey(RCore *core);
static int havePanel(RPanels *panels, const char *s);
static bool init(RCore *core, RPanels *panels, int w, int h);
static bool initPanels(RCore *core, RPanels *panels);
static void panelBreakpoint(RCore *core);
static void panelContinue(RCore *core);
static void panelPrompt(const char *prompt, char *buf, int len);
static void panelSingleStepIn(RCore *core);
static void panelSingleStepOver(RCore *core);
static void setRefreshAll(RPanels *panels);
static void setCursor(RCore *core, bool cur);
static void zoom(RPanels *panels);
static void panelPrint(RCore *core, RConsCanvas *can, RPanel *panel, int color) {
if (!can || !panel|| !panel->refresh) {
return;
}
panel->refresh = false;
char *text;
char title[128];
int delta_x, delta_y;
delta_x = panel->sx;
delta_y = panel->sy;
// clear the canvas first
r_cons_canvas_fill (can, panel->x, panel->y, panel->w, panel->h, ' ');
// for menu
RCons *cons = r_cons_singleton ();
if (panel->type == PANEL_TYPE_MENU) {
(void) r_cons_canvas_gotoxy (can, panel->x + 2, panel->y + 2);
text = r_str_ansi_crop (panel->title,
delta_x, delta_y, panel->w + 5, panel->h - delta_y);
if (text) {
r_cons_canvas_write (can, text);
free (text);
} else {
r_cons_canvas_write (can, panel->title);
}
} else {
if (color) {
const char *k = cons->pal.graph_box;
snprintf (title, sizeof (title) - 1,
"%s[x] %s"Color_RESET, k, panel->title);
} else {
snprintf (title, sizeof (title) - 1,
" %s ", panel->title);
}
if (r_cons_canvas_gotoxy (can, panel->x + 1, panel->y + 1)) {
r_cons_canvas_write (can, title); // delta_x
}
(void) r_cons_canvas_gotoxy (can, panel->x + 2, panel->y + 2);
char *cmdStr;
bool ce = core->print->cur_enabled;
if (!strcmp (panel->title, PANEL_TITLE_DISASSEMBLY)) {
core->print->cur_enabled = false;
cmdStr = r_core_cmd_str (core, panel->cmd);
} else if (!strcmp (panel->title, PANEL_TITLE_STACK)) {
const int delta = r_config_get_i (core->config, "stack.delta");
const char sign = (delta < 0)? '+': '-';
const int absdelta = R_ABS (delta);
cmdStr = r_core_cmd_strf (core, "%s%c%d", PANEL_CMD_STACK, sign, absdelta);
} else {
cmdStr = r_core_cmd_str (core, panel->cmd);
}
if (delta_y < 0) {
delta_y = 0;
}
if (delta_x < 0) {
char *white = (char*)r_str_pad (' ', 128);;
int idx = -delta_x;
if (idx >= sizeof (white)) {
idx = sizeof (white) - 1;
}
white[idx] = 0;
text = r_str_ansi_crop (cmdStr,
0, delta_y, panel->w + delta_x - 3, panel->h - 2 + delta_y);
char *newText = r_str_prefix_all (text, white);
if (newText) {
free (text);
text = newText;
}
} else {
text = r_str_ansi_crop (cmdStr,
delta_x, delta_y, panel->w + delta_x - 3, panel->h - 2 + delta_y);
}
if (text) {
r_cons_canvas_write (can, text);
free (text);
} else {
r_cons_canvas_write (can, panel->title);
}
free (cmdStr);
core->print->cur_enabled = ce;
}
if (color) {
r_cons_canvas_box (can, panel->x, panel->y, panel->w, panel->h, cons->pal.graph_box2);
} else {
r_cons_canvas_box (can, panel->x, panel->y, panel->w, panel->h, cons->pal.graph_box);
}
}
static void layoutMenu(RPanel *panel) {
panel->w = r_str_bounds (panel->title, &panel->h);
panel->h += 4;
}
R_API void r_core_panels_layout(RPanels *panels) {
int h, w = r_cons_get_size (&h);
int i, j;
int colpos = w - panels->columnWidth;
RPanel *panel = panels->panel;
if (colpos < 0) {
panels->columnWidth = w;
colpos = 0;
}
panels->can->sx = 0;
panels->can->sy = 0;
for (i = j = 0; i < panels->n_panels; i++) {
switch (panel[i].type) {
case PANEL_TYPE_MENU:
layoutMenu (&panel[i]);
break;
case PANEL_TYPE_FRAME:
switch (panels->layout) {
case LAYOUT_DEFAULT:
if (j == 0) {
panel[i].x = 0;
panel[i].y = 1;
if (panel[j + 1].title) {
panel[i].w = colpos + 1;
} else {
panel[i].w = w;
}
panel[i].h = h - 1;
} else {
int ph = ((h - 1) / (panels->n_panels - 2));
panel[i].x = colpos;
panel[i].y = 1 + (ph * (j - 1));
panel[i].w = w - colpos;
if (panel[i].w < 0) {
panel[i].w = 0;
}
panel[i].h = ph;
if (!panel[i + 1].title) {
panel[i].h = h - panel[i].y;
}
if (j != 1) {
panel[i].y--;
panel[i].h++;
}
}
break;
case LAYOUT_BALANCE:
if (j == 0) {
panel[i].x = 0;
panel[i].y = 1;
if (panel[j + 1].title) {
panel[i].w = colpos + 1;
} else {
panel[i].w = w;
}
panel[i].h = (h / 2) + 1;
} else if (j == 1) {
panel[i].x = 0;
panel[i].y = (h / 2) + 1;
if (panel[j + 1].title) {
panel[i].w = colpos + 1;
} else {
panel[i].w = w;
}
panel[i].h = (h - 1) / 2;
} else {
int ph = ((h - 1) / (panels->n_panels - 3));
panel[i].x = colpos;
panel[i].y = 1 + (ph * (j - 2));
panel[i].w = w - colpos;
if (panel[i].w < 0) {
panel[i].w = 0;
}
panel[i].h = ph;
if (!panel[i + 1].title) {
panel[i].h = h - panel[i].y;
}
if (j != 2) {
panel[i].y--;
panel[i].h++;
}
}
break;
}
j++;
}
}
}
R_API void r_core_panels_layout_refresh(RCore *core) {
r_core_panels_check_stackbase (core);
r_core_panels_layout (core->panels);
r_core_panels_refresh (core);
}
R_API void r_core_panels_graph(RCore *core, RConsCanvas *can) {
if (!core->panels || !core->panels->isGraphInPanels) {
return;
}
core->panels->can = can;
int origx = can->sx, origy = can->sy;
r_core_panels_refresh_except_mainpanel (core->panels);
r_core_panels_layout_refresh (core);
can->sx = origx;
can->sy = origy;
}
R_API void r_core_panels_refresh_except_mainpanel(RPanels *panels) {
int i;
for (i = 0; i < panels->n_panels; i++) {
panels->panel[i].refresh = i == 1 ? false : true;
}
}
static void setCursor(RCore *core, bool cur) {
core->print->cur_enabled = cur;
if (cur) {
core->print->cur = core->panels->panel[core->panels->curnode].curpos;
} else {
core->panels->panel[core->panels->curnode].curpos = core->print->cur;
}
core->print->col = core->print->cur_enabled ? 1: 0;
}
static void cursorRight(RCore *core) {
if (!strcmp (core->panels->panel[core->panels->curnode].title, PANEL_TITLE_STACK) && core->print->cur >= 15) {
return;
}
core->print->cur++;
core->panels->panel[core->panels->curnode].addr++;
return;
}
static void cursorLeft(RCore *core) {
if (core->print->cur > 0) {
core->print->cur--;
core->panels->panel[core->panels->curnode].addr--;
}
return;
}
static void handleUpKey(RCore *core) {
RPanels *panels = core->panels;
r_cons_switchbuf (false);
panels->panel[panels->curnode].refresh = true;
if (panels->panel[panels->curnode].type == PANEL_TYPE_MENU) {
panels->menu_y--;
if (panels->menu_y < 0) {
panels->menu_y = 0;
}
} else {
if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_DISASSEMBLY)) {
r_core_cmd0 (core, "s-8");
} else if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_STACK)) {
int width = r_config_get_i (core->config, "hex.cols");
if (width < 1) {
width = 16;
}
r_config_set_i (core->config, "stack.delta",
r_config_get_i (core->config, "stack.delta") + width);
panels->panel[panels->curnode].addr -= width;
} else if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_REGISTERS)) {
if (core->print->cur_enabled) {
int cur = core->print->cur;
int cols = core->dbg->regcols;
cols = cols > 0 ? cols : 3;
cur -= cols;
if (cur >= 0) {
core->print->cur = cur;
}
}
} else {
if (panels->panel[panels->curnode].sy > 0) {
panels->panel[panels->curnode].sy--;
}
}
}
}
static void handleDownKey(RCore *core) {
RPanels *panels = core->panels;
r_cons_switchbuf (false);
panels->panel[panels->curnode].refresh = true;
if (panels->panel[panels->curnode].type == PANEL_TYPE_MENU) {
if (menus_sub[panels->menu_x][panels->menu_y]) {
panels->menu_y++;
}
} else {
if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_DISASSEMBLY)) {
int cols = core->print->cols;
RAnalFunction *f = NULL;
RAsmOp op;
f = r_anal_get_fcn_in (core->anal, core->offset, 0);
op.size = 1;
if (f && f->folded) {
cols = core->offset - f->addr + r_anal_fcn_size (f);
} else {
r_asm_set_pc (core->assembler, core->offset);
cols = r_asm_disassemble (core->assembler,
&op, core->block, 32);
}
if (cols < 1) {
cols = op.size > 1 ? op.size : 1;
}
r_core_seek (core, core->offset + cols, 1);
r_core_block_read (core);
} else if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_STACK)) {
int width = r_config_get_i (core->config, "hex.cols");
if (width < 1) {
width = 16;
}
r_config_set_i (core->config, "stack.delta",
r_config_get_i (core->config, "stack.delta") - width);
panels->panel[panels->curnode].addr += width;
} else if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_REGISTERS)) {
if (core->print->cur_enabled) {
const int cols = core->dbg->regcols;
core->print->cur += cols > 0 ? cols : 3;
}
} else {
panels->panel[panels->curnode].sy++;
}
}
}
static void handleLeftKey(RCore *core) {
RPanels *panels = core->panels;
r_cons_switchbuf (false);
panels->panel[panels->curnode].refresh = true;
if (core->print->cur_enabled) {
cursorLeft (core);
} else {
if (panels->curnode == 0) {
if (panels->menu_x) {
panels->menu_x--;
panels->menu_y = panels->menu_y? 1: 0;
}
} else {
if (panels->panel[panels->curnode].sx > 0) {
panels->panel[panels->curnode].sx--;
}
}
}
}
static void handleRightKey(RCore *core) {
RPanels *panels = core->panels;
r_cons_switchbuf (false);
panels->panel[panels->curnode].refresh = true;
if (core->print->cur_enabled) {
cursorRight (core);
} else {
if (panels->curnode == 0) {
if (menus[panels->menu_x + 1]) {
panels->menu_x++;
panels->menu_y = panels->menu_y ? 1: 0;
}
} else {
panels->panel[panels->curnode].sx++;
}
}
}
static bool handleCursorMode(RCore *core, const int key) {
const char *creg;
const RPanels *panels = core->panels;
char buf[128];
if (core->print->cur_enabled) {
switch (key) {
case 9: // TAB
case 'J':
case 'Z': // SHIFT-TAB
case 'K':
return true;
case 'Q':
case 'q':
setCursor (core, !core->print->cur_enabled);
panels->panel[panels->curnode].refresh = true;
return true;
case 'i':
if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_STACK)) {
// insert mode
const char *prompt = "insert hex: ";
panelPrompt (prompt, buf, sizeof (buf));
r_core_cmdf (core, "wx %s @ 0x%08" PFMT64x, buf, panels->panel[panels->curnode].addr);
panels->panel[panels->curnode].refresh = true;
} else if (!strcmp (panels->panel[panels->curnode].title, PANEL_TITLE_REGISTERS)) {
creg = core->dbg->creg;
if (creg) {
const char *prompt = "new-reg-value> ";
panelPrompt (prompt, buf, sizeof (buf));
r_core_cmdf (core, "dr %s = %s", creg, buf);
panels->panel[panels->curnode].refresh = true;
}
}
return true;
}
}
return false;
}
static void delCurPanel(RPanels *panels) {
int i;
if (panels->curnode > 0 && panels->n_panels > 3) {
for (i = panels->curnode; i < (panels->n_panels - 1); i++) {
panels->panel[i] = panels->panel[i + 1];
}
panels->panel[i].title = 0;
panels->n_panels--;
if (panels->curnode >= panels->n_panels) {
panels->curnode = panels->n_panels - 1;
}
}
}
static void zoom(RPanels *panels) {
if (panels->n_panels > 2) {
if (panels->curnode < 2) {
panels->curnode = 2;
}
RPanel ocurnode = panels->panel[panels->curnode];
panels->panel[panels->curnode] = panels->panel[1];
panels->panel[1] = ocurnode;
panels->curnode = 1;
}
}
static bool checkFunc(RCore *core) {
RAnalFunction *fun = r_anal_get_fcn_in (core->anal, core->offset, R_ANAL_FCN_TYPE_NULL);
if (!fun) {
r_cons_message ("Not in a function. Type 'df' to define it here");
return false;
}
if (r_list_empty (fun->bbs)) {
r_cons_message ("No basic blocks in this function. You may want to use 'afb+'.");
return false;
}
return true;
}
static void setRefreshAll(RPanels *panels) {
int i;
for (i = 0; i < panels->n_panels; i++) {
panels->panel[i].refresh = true;
}
}
static void addPanelFrame(RCore *core, RPanels* panels, const char *title, const char *cmd) {
RPanel* panel = panels->panel;
int n_panels = panels->n_panels;
if (title) {
panel[n_panels].title = strdup (title);
panel[n_panels].cmd = r_str_newf (cmd);
} else {
panel[n_panels].title = r_core_cmd_str (core, cmd);
panel[n_panels].cmd = NULL;
}
panel[panels->n_panels].type = PANEL_TYPE_FRAME;
panel[panels->n_panels].refresh = true;
panel[panels->n_panels].curpos = 0;
if (!strcmp (panel[panels->n_panels].title, PANEL_TITLE_STACK)) {
const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
const ut64 stackbase = r_reg_getv (core->anal->reg, sp);
panel[panels->n_panels].baseAddr = stackbase;
panel[panels->n_panels].addr = stackbase - r_config_get_i (core->config, "stack.delta");
}
panels->n_panels++;
panels->curnode = panels->n_panels - 1;
zoom (panels);
panels->menu_y = 0;
}
static bool initPanels(RCore *core, RPanels *panels) {
panels->panel = NULL;
panels->panel = calloc (sizeof (RPanel), LIMIT);
if (!panels->panel) return false;
panels->n_panels = 0;
panels->panel[panels->n_panels].title = strdup ("");
panels->panel[panels->n_panels].type = PANEL_TYPE_MENU;
panels->panel[panels->n_panels].refresh = true;
panels->n_panels++;
addPanelFrame (core, panels, PANEL_TITLE_SYMBOLS, PANEL_CMD_SYMBOLS);
addPanelFrame (core, panels, PANEL_TITLE_STACK, PANEL_CMD_STACK);
addPanelFrame (core, panels, PANEL_TITLE_REGISTERS, PANEL_CMD_REGISTERS);
addPanelFrame (core, panels, PANEL_TITLE_REGISTERREFS, PANEL_CMD_REGISTERREFS);
addPanelFrame (core, panels, PANEL_TITLE_DISASSEMBLY, PANEL_CMD_DISASSEMBLY);
panels->curnode = 1;
return true;
}
// damn singletons.. there should be only one screen and therefor
// only one visual instance of the graph view. refactoring this
// into a struct makes the code to reference pointers unnecesarily
// we can look for a non-global solution here in the future if
// necessary
R_API void r_core_panels_refresh(RCore *core) {
RPanels *panels = core->panels;
RPanel *panel = panels->panel;
RConsCanvas *can = panels->can;
int menu_pos = panels->menu_pos;
int menu_x = panels->menu_x;
int menu_y = panels->menu_y;
char title[1024];
char str[1024];
int i, j, h, w = r_cons_get_size (&h);
const char *color = panels->curnode ? core->cons->pal.graph_box : core->cons->pal.graph_box2;
r_cons_gotoxy (0, 0);
if (!can) {
return;
}
if (panels->isResizing) {
panels->isResizing = false;
r_cons_canvas_resize (can, w, h);
setRefreshAll (panels);
}
#if 0
/* avoid flickering */
r_cons_canvas_clear (can);
r_cons_flush ();
#endif
if (panel) {
panel[menu_pos].x = (menu_y > 0) ? menu_x * 6 : w;
panel[menu_pos].y = 1;
free (panel[menu_pos].title);
panel[menu_pos].title = calloc (1, 1024); // r_str_newf ("%d", menu_y);
int maxsub = 0;
for (i = 0; menus_sub[i]; i++) {
maxsub = i;
}
if (menu_x < 0) {
panels->menu_x = 0;
}
if (menu_x > maxsub) {
panels->menu_x = maxsub;
}
if (menu_x >= 0 && menu_x <= maxsub && menus_sub[menu_x]) {
for (j = 0; menus_sub[menu_x][j]; j++) {
if (menu_y - 1 == j) {
strcat (panel[menu_pos].title, "> ");
} else {
strcat (panel[menu_pos].title, " ");
}
strcat (panel[menu_pos].title, menus_sub[menu_x][j]);
strcat (panel[menu_pos].title, " \n");
}
layoutMenu (&panel[menu_pos]);
}
for (i = 0; i < panels->n_panels; i++) {
if (i != panels->curnode) {
panelPrint (core, can, &panel[i], 0);
}
}
}
if (menu_y) {
panels->curnode = menu_pos;
}
if (panels->panel) {
if (panel[panels->curnode].type == PANEL_TYPE_MENU) {
panel[panels->curnode].refresh = true;
panelPrint (core, can, &panel[panels->curnode], menu_y);
} else {
panelPrint (core, can, &panel[panels->curnode], 1);
}
}
(void) r_cons_canvas_gotoxy (can, -can->sx, -can->sy);
title[0] = 0;
if (panels->curnode == 0) {
strcpy (title, "> ");
}
for (i = 0; menus[i]; i++) {
if (menu_x == i) {
snprintf (str, sizeof (title) - 1, "%s[%s]"Color_RESET, color, menus[i]);
} else {
snprintf (str, sizeof (title) - 1, "%s %s "Color_RESET, color, menus[i]);
}
strcat (title, str);
}
if (panels->curnode == 0) {
r_cons_canvas_write (can, Color_BLUE);
r_cons_canvas_write (can, title);
r_cons_canvas_write (can, Color_RESET);
} else {
r_cons_canvas_write (can, Color_RESET);
r_cons_canvas_write (can, title);
}
snprintf (title, sizeof (title) - 1,
"[0x%08"PFMT64x "]", core->offset);
(void) r_cons_canvas_gotoxy (can, -can->sx + w - strlen (title), -can->sy);
r_cons_canvas_write (can, title);
r_cons_canvas_print (can);
r_cons_flush ();
}
static void doPanelsRefresh(RCore *core) {
core->panels->isResizing = true;
r_core_panels_layout (core->panels);
r_core_panels_refresh (core);
}
static int havePanel(RPanels *panels, const char *s) {
int i;
if (!panels->panel || !panels->panel[0].title) {
return 0;
}
// add new panel for testing
for (i = 1; panels->panel[i].title; i++) {
if (!strcmp (panels->panel[i].title, s)) {
return 1;
}
}
return 0;
}
static void panelSingleStepIn(RCore *core) {
if (r_config_get_i (core->config, "cfg.debug")) {
if (core->print->cur_enabled) {
// dcu 0xaddr
r_core_cmdf (core, "dcu 0x%08"PFMT64x, core->offset + core->print->cur);
core->print->cur_enabled = 0;
} else {
r_core_cmd (core, "ds", 0);
r_core_cmd (core, ".dr*", 0);
}
} else {
r_core_cmd (core, "aes", 0);
r_core_cmd (core, ".ar*", 0);
}
}
static void panelSingleStepOver(RCore *core) {
bool io_cache = r_config_get_i (core->config, "io.cache");
r_config_set_i (core->config, "io.cache", false);
if (r_config_get_i (core->config, "cfg.debug")) {
r_core_cmd (core, "dso", 0);
r_core_cmd (core, ".dr*", 0);
} else {
r_core_cmd (core, "aeso", 0);
r_core_cmd (core, ".ar*", 0);
}
r_config_set_i (core->config, "io.cache", io_cache);
}
static void panelBreakpoint(RCore *core) {
r_core_cmd (core, "dbs $$", 0);
}
static void panelContinue(RCore *core) {
r_core_cmd (core, "dc", 0);
}
R_API void r_core_panels_check_stackbase(RCore *core) {
if (!core || !core->panels) {
return;
}
int i;
const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
const ut64 stackbase = r_reg_getv (core->anal->reg, sp);
RPanels *panels = core->panels;
for (i = 1; i < panels->n_panels; i++) {
const RPanel panel = panels->panel[i];
if (!strcmp (panel.title, PANEL_TITLE_STACK) && panel.baseAddr != stackbase) {
panels->panel[panels->curnode].baseAddr = stackbase;
panels->panel[panels->curnode].addr = stackbase - r_config_get_i (core->config, "stack.delta") + core->print->cur;
panels->panel[panels->curnode].refresh = true;
}
}
}
static void panelPrompt(const char *prompt, char *buf, int len) {
r_line_set_prompt (prompt);
*buf = 0;
r_cons_fgets (buf, len, 0, NULL);
}
static bool init (RCore *core, RPanels *panels, int w, int h) {
panels->panel = NULL;
panels->n_panels = 0;
panels->columnWidth = 80;
panels->layout = 0;
panels->menu_pos = 0;
panels->menu_x = 0;
panels->menu_y = 0;
panels->callgraph = 0;
panels->isResizing = false;
panels->isGraphInPanels = false;
panels->can = r_cons_canvas_new (w, h);
r_cons_canvas_fill (panels->can, 0, 0, w, h, ' ');
if (!panels->can) {
eprintf ("Cannot create RCons.canvas context\n");
return false;
}
panels->can->linemode = r_config_get_i (core->config, "graph.linemode");
panels->can->color = r_config_get_i (core->config, "scr.color");
if (w < 140) {
panels->columnWidth = w / 3;
}
return true;
}
static bool handleEnterKey(RCore *core) {
RPanels *panels = core->panels;
if (panels->curnode == 0 && panels->menu_y) {
const char *action = menus_sub[panels->menu_x][panels->menu_y - 1];
if (strstr (action, "New")) {
addPanelFrame (core, panels, PANEL_TITLE_NEWFILES, "o");
} else if (strstr (action, "Open")) {
/* XXX doesnt autocompletes filenames */
r_cons_enable_mouse (false);
char *res = r_cons_input ("open file: ");
if (res) {
if (*res) {
r_core_cmdf (core, "o %s", res);
}
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "RegisterRefs")) {
addPanelFrame (core, panels, "drr", "drr");
} else if (strstr (action, "Registers")) {
addPanelFrame (core, panels, "dr=", "dr=");
} else if (strstr (action, "Info")) {
addPanelFrame (core, panels, PANEL_TITLE_INFO, "i");
} else if (strstr (action, "Database")) {
addPanelFrame (core, panels, PANEL_TITLE_DATABASE, "k ***");
} else if (strstr (action, "Registers")) {
if (!havePanel (panels, "Registers")) {
addPanelFrame (core, panels, PANEL_TITLE_REGISTERS, "dr=");
}
} else if (strstr (action, "About")) {
char *s = r_core_cmd_str (core, "?V");
r_cons_message (s);
free (s);
} else if (strstr (action, "Hexdump")) {
addPanelFrame (core, panels, PANEL_TITLE_HEXDUMP, "px 512");
} else if (strstr (action, "Disassembly")) {
addPanelFrame (core, panels, PANEL_TITLE_DISASSEMBLY, "pd 128");
} else if (strstr (action, "Functions")) {
addPanelFrame (core, panels, PANEL_TITLE_FUNCTIONS, "afl");
} else if (strstr (action, "Comments")) {
addPanelFrame (core, panels, PANEL_TITLE_COMMENTS, "CC");
} else if (strstr (action, "Entropy")) {
addPanelFrame (core, panels, PANEL_TITLE_ENTROPY, "p=e");
} else if (strstr (action, "Function")) {
r_core_cmdf (core, "af");
} else if (strstr (action, "DRX")) {
addPanelFrame (core, panels, PANEL_TITLE_DRX, "drx");
} else if (strstr (action, "Program")) {
r_core_cmdf (core, "aaa");
} else if (strstr (action, "Calls")) {
r_core_cmdf (core, "aac");
} else if (strstr (action, "ROP")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("rop grep: ");
if (res) {
r_core_cmdf (core, "\"/R %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "String")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("search string: ");
if (res) {
r_core_cmdf (core, "\"/ %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Hexpairs")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("search hexpairs: ");
if (res) {
r_core_cmdf (core, "\"/x %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Code")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("search code: ");
if (res) {
r_core_cmdf (core, "\"/c %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Copy")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("How many bytes? ");
if (res) {
r_core_cmdf (core, "\"y %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Write String")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("insert string: ");
if (res) {
r_core_cmdf (core, "\"w %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Write Value")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("insert number: ");
if (res) {
r_core_cmdf (core, "\"wv %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Write Hex")) {
r_cons_enable_mouse (false);
char *res = r_cons_input ("insert hexpairs: ");
if (res) {
r_core_cmdf (core, "\"wx %s\"", res);
free (res);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Calculator")) {
r_cons_enable_mouse (false);
for (;;) {
char *s = r_cons_input ("> ");
if (!s || !*s) {
free (s);
break;
}
r_core_cmdf (core, "? %s", s);
r_cons_flush ();
free (s);
}
r_cons_enable_mouse (true);
} else if (strstr (action, "Assemble")) {
r_core_visual_asm (core, core->offset);
} else if (strstr (action, "Sections")) {
addPanelFrame (core, panels, PANEL_TITLE_SECTIONS, "iSq");
} else if (strstr (action, "Close")) {
r_core_cmd0 (core, "o-*");
} else if (strstr (action, "Strings")) {
addPanelFrame (core, panels, PANEL_TITLE_STRINGS, "izq");
} else if (strstr (action, "Maps")) {
addPanelFrame (core, panels, PANEL_TITLE_MAPS, "dm");
} else if (strstr (action, "Modules")) {
addPanelFrame (core, panels, PANEL_TITLE_MODULES, "dmm");
} else if (strstr (action, "Backtrace")) {
addPanelFrame (core, panels, PANEL_TITLE_BACKTRACE, "dbt");
} else if (strstr (action, "Step")) {
r_core_cmd (core, "ds", 0);
r_cons_flush ();
} else if (strstr (action, "Step Over")) {
r_core_cmd (core, "dso", 0);
r_cons_flush ();
} else if (strstr (action, "Continue")) {
r_core_cmd (core, "dc", 0);
r_cons_flush ();
} else if (strstr (action, "Breakpoints")) {
addPanelFrame (core, panels, PANEL_TITLE_BREAKPOINTS, "db");
} else if (strstr (action, "Symbols")) {
addPanelFrame (core, panels, PANEL_TITLE_SYMBOLS, "isq");
} else if (strstr (action, "Imports")) {
addPanelFrame (core, panels, PANEL_TITLE_IMPORTS, "iiq");
} else if (strstr (action, "Paste")) {
r_core_cmd0 (core, "yy");
} else if (strstr (action, "Clipboard")) {
addPanelFrame (core, panels, PANEL_TITLE_CLIPBOARD, "yx");
} else if (strstr (action, "io.cache")) {
r_core_cmd0 (core, "e!io.cache");
} else if (strstr (action, "Fill")) {
r_cons_enable_mouse (false);
char *s = r_cons_input ("Fill with: ");
r_core_cmdf (core, "wow %s", s);