-
Notifications
You must be signed in to change notification settings - Fork 263
/
debug.c
1450 lines (1234 loc) · 35.4 KB
/
debug.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
/*
* debug.c
*
* Copyright 2010 Alexander Petukhov <devel(at)apetukhov.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/*
* Debug activities handlers (Run, Stop, etc)
* Manages GUI that is debug-state dependent.
* Finaly, after checking current debug state - passes
* command to the active debug module.
* Also creates debug-related GUI (local, debug terminal pages)
* and handles run-time watches and breakpoints changes.
* Contains callbacks from debugger module to handle debug state changes.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
int unlockpt(int fildes);
int grantpt(int fd);
#include <string.h>
#include <unistd.h>
#if defined(HAVE_UTIL_H)
#include <util.h>
#elif defined(HAVE_LIBUTIL_H)
#include <libutil.h>
#elif defined(HAVE_PTY_H)
#include <pty.h>
#endif
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <vte/vte.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <geanyplugin.h>
extern GeanyData *geany_data;
#include "tpage.h"
#include "breakpoints.h"
#include "debug.h"
#include "utils.h"
#include "stree.h"
#include "watch_model.h"
#include "wtree.h"
#include "atree.h"
#include "tpage.h"
#include "calltip.h"
#include "bptree.h"
#include "btnpanel.h"
#include "dconfig.h"
#include "tabs.h"
/*
* calltip size
*/
#define CALLTIP_HEIGHT 20
#define CALLTIP_WIDTH 200
/* module description structure (name/module pointer) */
typedef struct _module_description {
const gchar *title;
dbg_module *module;
} module_description;
/* holds current debug state */
enum dbs debug_state = DBS_IDLE;
/* debug modules declarations */
extern dbg_module dbg_module_gdb;
/* extern dbg_module dbg_module_bash; */
/* active debug module */
dbg_module *active_module = NULL;
/* Interrupt relateed data
* Interrtion is requested when breakpoint is set/added/removed
* asyncronously. Then debug_request_interrupt is called,
* supplied with interrupt reason (interrupt_flags),
* breakpoint pointer (interrupt_data) and callback to call
* after interruption
*/
bs_callback interrupt_cb = NULL;
gpointer interrupt_data = NULL;
/* flag to set when debug stop is requested while debugger is running.
* Then this flag is set to TRUE, and debug_request_interrupt function is called
*/
gboolean exit_pending = FALSE;
/* debug terminal PTY master/slave file descriptors */
int pty_master, pty_slave;
/* debug terminal widget */
GtkWidget *terminal = NULL;
/* GtkTextView to put debugger messages */
static GtkWidget *debugger_messages_textview = NULL;
/*
* Adjustments of a scroll view containing debugger messages
* text view. Values are changed during new text adding.
*/
static GtkAdjustment *hadj = NULL;
static GtkAdjustment *vadj = NULL;
/* stack trace/watch/autos CtkTreeView widgets */
static GtkWidget *stree = NULL;
static GtkWidget *wtree = NULL;
static GtkWidget *atree = NULL;
/* watch tree view model and store */
GtkTreeStore *wstore = NULL;
GtkTreeModel *wmodel = NULL;
/* array of widgets, ti enable/disable regard of a debug state */
static GtkWidget **sensitive_widget[] = {&stree, &wtree, &atree, NULL};
/*
* current stack for holding
* position of ffreames markers
*/
static GList* stack = NULL;
/*
* pages which are loaded in debugger and therefore, are set readonly
*/
static GList *read_only_pages = NULL;
/* available modules */
static module_description modules[] =
{
{ "GDB", &dbg_module_gdb },
{ NULL, NULL }
};
/* calltips cache */
static GHashTable *calltips = NULL;
/*
* remove stack margin markers
*/
static void remove_stack_markers(void)
{
int active_frame_index = active_module->get_active_frame();
GList *iter;
int frame_index;
for (iter = stack, frame_index = 0; iter; iter = iter->next, frame_index++)
{
if (iter)
{
frame *f = (frame*)iter->data;
if (f->have_source)
{
if (active_frame_index == frame_index)
{
markers_remove_current_instruction(f->file, f->line);
}
else
{
markers_remove_frame(f->file, f->line);
}
}
}
}
}
/*
* add stack margin markers
*/
static void add_stack_markers(void)
{
int active_frame_index = active_module->get_active_frame();
GList *iter;
int frame_index;
for (iter = stack, frame_index = 0; iter; iter = iter->next, frame_index++)
{
if (iter)
{
frame *f = (frame*)iter->data;
if (f->have_source)
{
if (active_frame_index == frame_index)
{
markers_add_current_instruction(f->file, f->line);
}
else
{
markers_add_frame(f->file, f->line);
}
}
}
}
}
/*
* Handlers for GUI maked changes in watches
*/
/*
* watch expression has been changed
*/
static void on_watch_changed(GtkCellRendererText *renderer, gchar *path, gchar *new_text, gpointer user_data)
{
/* get iterator to the changed row */
GtkTreeIter iter;
gchar *oldvalue;
gchar *internal = NULL;
GtkTreePath *empty_path;
GtkTreePath *tree_path = gtk_tree_path_new_from_string (path);
gboolean is_empty_row;
gchar *striped;
gtk_tree_model_get_iter (
gtk_tree_view_get_model(GTK_TREE_VIEW(wtree)),
&iter,
tree_path);
/* get oldvalue */
gtk_tree_model_get (
wmodel,
&iter,
W_NAME, &oldvalue,
-1);
gtk_tree_model_get (
wmodel,
&iter,
W_INTERNAL, &internal,
-1);
/* check if it is empty row */
empty_path = wtree_empty_path();
is_empty_row = !gtk_tree_path_compare (tree_path, empty_path);
gtk_tree_path_free(empty_path);
striped = g_strstrip(g_strdup(new_text));
if (!strlen(striped) &&
!is_empty_row &&
dialogs_show_question(_("Delete variable?")))
{
/* if new value is empty string on non-empty row
* offer to delete watch */
gtk_tree_store_remove(wstore, &iter);
if (DBS_STOPPED == debug_state)
active_module->remove_watch(internal);
config_set_debug_changed();
}
else if (strcmp(oldvalue, striped))
{
/* new value is non empty */
/* insert new row if changing was the last empty row */
GtkTreeIter newiter;
if (is_empty_row)
gtk_tree_store_insert_before(wstore, &newiter, NULL, &iter);
/* set expression */
variable_set_name_only(wstore, is_empty_row ? &newiter : &iter, striped);
/* if debug is active - remove old watch and add new one */
if (DBS_STOPPED == debug_state)
{
variable *newvar;
active_module->remove_watch(internal);
newvar = active_module->add_watch(striped);
change_watch(GTK_TREE_VIEW(wtree), is_empty_row ? &newiter : &iter, newvar);
}
/* if new watch has been added - set selection to the new created row */
if (is_empty_row)
{
GtkTreePath *_path = gtk_tree_model_get_path(wmodel, &newiter);
GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(wtree));
gtk_tree_selection_unselect_all(selection);
gtk_tree_selection_select_path(selection, _path);
gtk_tree_path_free(_path);
}
config_set_debug_changed();
}
/* free resources */
gtk_tree_path_free(tree_path);
g_free(oldvalue);
g_free(internal);
g_free(striped);
}
/*
* text has been dragged into the watch tree view
*/
static void on_watch_dragged_callback(GtkWidget *wgt, GdkDragContext *context, int x, int y,
GtkSelectionData *seldata, guint info, guint _time,
gpointer userdata)
{
/* string that is dragged */
gchar *expression = (gchar*)seldata->data;
GtkTreePath *path = NULL;
GtkTreeViewDropPosition pos;
GtkTreePath *empty_path;
GtkTreeIter newvar;
/* lookup for where the text has been dropped */
gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(wtree), x, y, &path, &pos);
/* if dropped into last row - insert before it */
empty_path = wtree_empty_path();
if (!gtk_tree_path_compare(empty_path, path))
pos = GTK_TREE_VIEW_DROP_BEFORE;
gtk_tree_path_free(empty_path);
/* if dropped into children area - insert before parent */
if (gtk_tree_path_get_depth(path) > 1)
{
while (gtk_tree_path_get_depth(path) > 1)
gtk_tree_path_up(path);
pos = GTK_TREE_VIEW_DROP_BEFORE;
}
/* insert new row */
if (path)
{
GtkTreeIter sibling;
gtk_tree_model_get_iter(wmodel, &sibling, path);
if (GTK_TREE_VIEW_DROP_BEFORE == pos || GTK_TREE_VIEW_DROP_INTO_OR_BEFORE == pos)
gtk_tree_store_insert_before(wstore, &newvar, NULL, &sibling);
else
gtk_tree_store_insert_after(wstore, &newvar, NULL, &sibling);
}
else
{
GtkTreeIter empty;
wtree_empty_row(&empty);
gtk_tree_store_insert_before(wstore, &newvar, NULL, &empty);
}
/* if debugger is active (in stopped condition) - add to run-time watch list
* if not - just set new expession in the tree view */
if (DBS_STOPPED == debug_state)
{
variable *var = active_module->add_watch(expression);
change_watch(GTK_TREE_VIEW(wtree), &newvar, var);
}
else
variable_set_name_only(wstore, &newvar, expression);
config_set_debug_changed();
}
/*
* key has been pressed while being in watch tree view
*/
static gboolean on_watch_key_pressed_callback(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
GtkTreeSelection *selection;
GList *rows;
GtkTreePath *empty_path;
/* handling only Delete button pressing
* that means "delete selected rows" */
if (((GdkEventKey*)event)->keyval != GDK_Delete)
return FALSE;
/* get selected rows */
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(wtree));
rows = gtk_tree_selection_get_selected_rows(selection, &wmodel);
/* empty row path */
empty_path = wtree_empty_path();
/* check whether only empty row was selected */
if (1 != gtk_tree_selection_count_selected_rows(selection) ||
(rows && gtk_tree_path_compare((GtkTreePath*)rows->data, empty_path)))
{
/* path reference to select after deleteing finishes */
GtkTreeRowReference *reference_to_select = NULL;
GtkTreePath *path_to_select;
/* get references to the rows */
GList *references = NULL;
GList *iter = rows;
while (iter)
{
GtkTreePath *path = (GtkTreePath*)iter->data;
/* move up paths to the root elements */
while (gtk_tree_path_get_depth(path) > 1)
gtk_tree_path_up(path);
/* add path reference if it's not an empty row*/
if (gtk_tree_path_compare(path, empty_path))
references = g_list_prepend(references, gtk_tree_row_reference_new(wmodel, path));
iter = iter->next;
}
/* iterate through references and remove */
iter = g_list_reverse(references);
while (iter)
{
GtkTreeRowReference *reference = (GtkTreeRowReference*)iter->data;
/* check for valid reference because two or more equal
refernces could be put in the list if several child items
of the same node were selected and the path for the
current reference was already deleted */
if (gtk_tree_row_reference_valid(reference))
{
GtkTreeIter titer;
GtkTreePath *path = gtk_tree_row_reference_get_path(reference);
if (!reference_to_select)
{
/* select upper sibling of the upper
selected row that has unselected upper sibling */
GtkTreePath *sibling = gtk_tree_path_copy(path);
if(gtk_tree_path_prev(sibling))
{
if (!gtk_tree_selection_path_is_selected(selection, sibling))
reference_to_select = gtk_tree_row_reference_new(gtk_tree_view_get_model(GTK_TREE_VIEW(wtree)), sibling);
}
else if (gtk_tree_path_next(sibling), gtk_tree_path_compare(path, sibling))
reference_to_select = gtk_tree_row_reference_new(gtk_tree_view_get_model(GTK_TREE_VIEW(wtree)), sibling);
}
/* get iterator */
gtk_tree_model_get_iter(wmodel, &titer, path);
/* remove from the debug session, if it's active */
if (DBS_STOPPED == debug_state)
{
gchar *internal = NULL;
gtk_tree_model_get (
wmodel,
&titer,
W_INTERNAL, &internal,
-1);
active_module->remove_watch(internal);
g_free(internal);
}
gtk_tree_store_remove(wstore, &titer);
gtk_tree_path_free(path);
}
iter = iter->next;
}
/* if all (with or without empty row) was selected - set empty row
as a path to be selected after deleting */
if (!reference_to_select)
{
GtkTreePath *path = wtree_empty_path();
reference_to_select = gtk_tree_row_reference_new (gtk_tree_view_get_model(GTK_TREE_VIEW(wtree)), path);
gtk_tree_path_free(path);
}
/* set selection */
gtk_tree_selection_unselect_all(selection);
path_to_select = gtk_tree_row_reference_get_path(reference_to_select);
gtk_tree_selection_select_path(selection, path_to_select);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(widget), path_to_select, NULL, TRUE, 0.5, 0.5);
gtk_tree_path_free(path_to_select);
/* free references list */
g_list_foreach (references, (GFunc)gtk_tree_row_reference_free, NULL);
g_list_free (references);
config_set_debug_changed();
}
gtk_tree_path_free(empty_path);
/* free rows list */
g_list_foreach (rows, (GFunc)gtk_tree_path_free, NULL);
g_list_free (rows);
return FALSE;
}
/*
* mouse button has been pressed while being in watch(autos) tree view
*/
static gboolean on_watch_button_pressed_callback(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
{
if (event->type == GDK_2BUTTON_PRESS && event->button == 1)
{
GtkTreePath *path = NULL;
if (gtk_tree_view_get_path_at_pos(
GTK_TREE_VIEW(treeview),
(int)event->x, (int)event->y, &path, NULL, NULL, NULL))
{
gchar *expression = NULL;
GtkTreeIter iter;
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get(model, &iter,
W_EXPRESSION, &expression,
-1);
if (strlen(expression))
{
GtkTreeIter newvar, empty;
wtree_empty_row(&empty);
gtk_tree_store_insert_before(wstore, &newvar, NULL, &empty);
/* if debugger is active (in stopped condition) - add to run-time watch list
* if not - just set new expession in the tree view */
if (DBS_STOPPED == debug_state)
{
variable *var = active_module->add_watch(expression);
change_watch(GTK_TREE_VIEW(wtree), &newvar, var);
}
else
variable_set_name_only(wstore, &newvar, expression);
config_set_debug_changed();
}
g_free(expression);
}
}
return FALSE;
}
/*
* watch that has children has been expanded
*/
static void on_watch_expanded_callback(GtkTreeView *tree, GtkTreeIter *iter, GtkTreePath *path, gpointer user_data)
{
/* get tree view model and store as it can be watch or autos tree */
GtkTreeModel *model = gtk_tree_view_get_model(tree);
GtkTreeStore *store = GTK_TREE_STORE(model);
/* get a flag indicating that item has only stub andd need to
* evaluate its children */
gboolean only_stub = FALSE;
gtk_tree_model_get (
model,
iter,
W_STUB, &only_stub,
-1);
if (only_stub)
{
GList *children;
gchar *internal;
/* if item has not been expanded before */
gtk_tree_model_get (
model,
iter,
W_INTERNAL, &internal,
-1);
/* get children list */
children = active_module->get_children(internal);
/* remove stub and add children */
expand_stub(tree, iter, children);
/* free children list */
free_variables_list(children);
/* unset W_STUB flag */
gtk_tree_store_set (store, iter,
W_STUB, FALSE,
-1);
g_free(internal);
}
}
/*
* GUI functions
*/
/*
* enable/disable sensitive widgets
* arguments:
* enable - enable or disable widgets
*/
static void enable_sensitive_widgets(gboolean enable)
{
int i;
for(i = 0; sensitive_widget[i]; i++)
gtk_widget_set_sensitive(*sensitive_widget[i], enable);
}
/*
* Debug state changed hanflers
*/
/*
* called from debug module when debugger is being run
*/
static void on_debugger_run (void)
{
/* update debug state */
debug_state = DBS_RUNNING;
/* if curren instruction marker was set previously - remove it */
if (stack)
{
remove_stack_markers();
g_list_foreach(stack, (GFunc)frame_unref, NULL);
g_list_free(stack);
stack = NULL;
stree_remove_frames();
}
/* disable widgets */
enable_sensitive_widgets(FALSE);
/* update buttons panel state */
btnpanel_set_debug_state(debug_state);
}
/*
* called from debug module when debugger is being stopped
*/
static void on_debugger_stopped (int thread_id)
{
GList *iter, *files, *autos, *watches;
/* update debug state */
debug_state = DBS_STOPPED;
/* update buttons panel state */
if (!interrupt_data)
{
btnpanel_set_debug_state(debug_state);
}
/* clear calltips cache */
if (calltips)
g_hash_table_remove_all(calltips);
/* if a stop was requested for asyncronous exiting -
* stop debug module and exit */
if (exit_pending)
{
active_module->stop();
exit_pending = FALSE;
return;
}
/* check for async activities pending */
if (interrupt_data)
{
interrupt_cb(interrupt_data);
interrupt_data = NULL;
active_module->resume();
return;
}
/* clear stack tree view */
stree_set_active_thread_id(thread_id);
/* get current stack trace and put in the tree view */
stack = active_module->get_stack();
stree_add (stack);
stree_select_first_frame(TRUE);
/* files */
files = active_module->get_files();
/* remove from list and make writable those files,
that are not in the current list */
iter = read_only_pages;
while (iter)
{
if (!g_list_find_custom(files, iter->data, (GCompareFunc)g_strcmp0))
{
GList *next;
/* set document writable */
GeanyDocument *doc = document_find_by_real_path((const gchar*)iter->data);
if (doc)
scintilla_send_message(doc->editor->sci, SCI_SETREADONLY, 0, 0);
/* free file name */
g_free(iter->data);
/* save next item pointer */
next = iter->next;
/* remove current item */
read_only_pages = g_list_delete_link(read_only_pages, iter);
/* set next item and continue */
iter = next;
continue;
}
iter = iter->next;
}
/* add to the list and make readonly those files
from the current list that are new */
iter = files;
while (iter)
{
if (!g_list_find_custom(read_only_pages, iter->data, (GCompareFunc)g_strcmp0))
{
/* set document readonly */
GeanyDocument *doc = document_find_by_real_path((const gchar*)iter->data);
if (doc)
scintilla_send_message(doc->editor->sci, SCI_SETREADONLY, 1, 0);
/* add new file to the list */
read_only_pages = g_list_append(read_only_pages, g_strdup((gchar*)iter->data));
}
iter = iter->next;
}
g_list_free(files);
/* autos */
autos = active_module->get_autos();
update_variables(GTK_TREE_VIEW(atree), NULL, autos);
/* watches */
watches = active_module->get_watches();
update_variables(GTK_TREE_VIEW(wtree), NULL, watches);
if (stack)
{
frame *current = (frame*)stack->data;
if (current->have_source)
{
/* open current instruction position */
editor_open_position(current->file, current->line);
}
/* add current instruction marker */
add_stack_markers();
}
/* enable widgets */
enable_sensitive_widgets(TRUE);
/* remove breaks readonly if current module doesn't support run-time breaks operation */
if (!(active_module->features & MF_ASYNC_BREAKS))
bptree_set_readonly(FALSE);
}
/*
* called when debugger exits
*/
static void on_debugger_exited (int code)
{
GtkTextIter start, end;
GtkTextBuffer *buffer;
GList *iter;
/* remove marker for current instruction if was set */
if (stack)
{
remove_stack_markers();
g_list_foreach(stack, (GFunc)frame_unref, NULL);
g_list_free(stack);
stack = NULL;
}
/* clear watch page */
clear_watch_values(GTK_TREE_VIEW(wtree));
/* clear autos page */
gtk_tree_store_clear(GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(atree))));
/* clear stack trace tree */
stree_clear();
/* clear debug terminal */
vte_terminal_reset(VTE_TERMINAL(terminal), TRUE, TRUE);
/* clear debug messages window */
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(debugger_messages_textview));
gtk_text_buffer_get_bounds(buffer, &start, &end);
gtk_text_buffer_delete(buffer, &start, &end);
/* enable target page */
tpage_set_readonly(FALSE);
/* remove breaks readonly if current module doesn't support run-time breaks operation */
if (!(active_module->features & MF_ASYNC_BREAKS))
bptree_set_readonly(FALSE);
/* set files that was readonly during debug writable */
for (iter = read_only_pages; iter; iter = iter->next)
{
GeanyDocument *doc = document_find_by_real_path((const gchar*)iter->data);
if (doc)
scintilla_send_message(doc->editor->sci, SCI_SETREADONLY, 0, 0);
/* free file name */
g_free(iter->data);
}
g_list_free(read_only_pages);
read_only_pages = NULL;
/* clear and destroy calltips cache */
if (calltips)
{
g_hash_table_destroy(calltips);
calltips = NULL;
}
/* enable widgets */
enable_sensitive_widgets(TRUE);
/* update buttons panel state */
btnpanel_set_debug_state(DBS_IDLE);
/* update debug state */
debug_state = DBS_IDLE;
}
/*
* called from debugger module to show a message in debugger messages pane
*/
static void on_debugger_message (const gchar* message, const gchar *color)
{
gchar *msg = g_strdup_printf("%s\n", message);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(debugger_messages_textview));
GtkTextIter iter;
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, msg, -1, color, NULL);
g_free(msg);
gtk_adjustment_set_value(vadj, gtk_adjustment_get_upper(vadj));
}
/*
* called from debugger module to clear messages tab
*/
static void on_debugger_messages_clear (void)
{
/* clear debug messages window */
GtkTextIter start, end;
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(debugger_messages_textview));
gtk_text_buffer_get_bounds(buffer, &start, &end);
gtk_text_buffer_delete(buffer, &start, &end);
}
/*
* called from debugger module to show an error message box
*/
static void on_debugger_error (const gchar* message)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, "%s", message);
}
/*
* called from debugger module when a thead has been removed
*/
static void on_thread_removed(int thread_id)
{
stree_remove_thread(thread_id);
}
/*
* called from debugger module when a new thead has been added
*/
static void on_thread_added (int thread_id)
{
stree_add_thread(thread_id);
}
/* callbacks structure to pass to debugger module */
dbg_callbacks callbacks = {
on_debugger_run,
on_debugger_stopped,
on_debugger_exited,
on_debugger_message,
on_debugger_messages_clear,
on_debugger_error,
on_thread_added,
on_thread_removed,
};
/*
* Interface functions
*/
/*
* called when a frame in the stack tree has been selected
*/
static void on_select_frame(int frame_number)
{
GList *autos, *watches;
frame *f = (frame*)g_list_nth_data(stack, active_module->get_active_frame());
if (f)
{
markers_remove_current_instruction(f->file, f->line);
markers_add_frame(f->file, f->line);
}
active_module->set_active_frame(frame_number);
/* clear calltips cache */
if (calltips)
g_hash_table_remove_all(calltips);
/* autos */
autos = active_module->get_autos();
update_variables(GTK_TREE_VIEW(atree), NULL, autos);
/* watches */
watches = active_module->get_watches();
update_variables(GTK_TREE_VIEW(wtree), NULL, watches);
f = (frame*)g_list_nth_data(stack, frame_number);
if (f)
{
markers_remove_frame(f->file, f->line);
markers_add_current_instruction(f->file, f->line);
}
}
/*
* called when a thread should been selected
*/
static void on_select_thread(int thread_id)
{
gboolean success;
if (stack)
remove_stack_markers();
if ((success = active_module->set_active_thread(thread_id)))
{
g_list_free_full(stack, (GDestroyNotify)frame_unref);
stack = active_module->get_stack();
/* update the stack tree */
stree_remove_frames();
stree_set_active_thread_id(thread_id);
stree_add(stack);
stree_select_first_frame(TRUE);
}
if (stack)
add_stack_markers();
if (success)
on_select_frame(0);
}
/*
* init debug related GUI (watch tree view)
* arguments:
*/
void debug_init(void)
{
GtkWidget *scrollbar;
GtkWidget *hbox;
GKeyFile *config;
gchar *configfile;
gchar *font;