-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
1375 lines (1139 loc) · 33.1 KB
/
main.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
/*
* dwarview - DWARF debug info viewer
*
* Copyright (C) 2016 Namhyung Kim <[email protected]>
*
* 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; version 2 or later of the License.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "dwarview.h"
/* Dwarf FL wrappers */
static char *debuginfo_path; /* Currently dummy */
static const Dwfl_Callbacks offline_callbacks = {
.find_debuginfo = dwfl_standard_find_debuginfo,
.debuginfo_path = &debuginfo_path,
.section_address = dwfl_offline_section_address,
/* We use this table for core files too. */
.find_elf = dwfl_build_id_find_elf,
};
static Dwarf *dwarf;
static GtkBuilder *builder;
struct content_arg {
char *filename;
GtkBuilder *builder;
GtkTreeStore *main_store;
GtkTreeStore *attr_store;
GtkStatusbar *status;
guint status_ctx;
Dwarf_Off off;
size_t total_size;
char msgbuf[4096];
};
static struct content_arg *arg;
struct search_status {
bool on_going;
bool try_var;
bool with_decl;
gint found;
guint ctx_id;
GList *curr;
gchar *text;
GPatternSpec *patt;
GtkSearchEntry *entry;
GtkButton *button;
GtkToggleButton *func;
GtkToggleButton *var;
GtkToggleButton *decl;
GtkTreeView *result;
GtkTreeView *main_view;
GtkStatusbar *status;
char msgbuf[4096];
};
static struct search_status *search;
static GList *func_list;
static GList *func_first;
static GList *var_list;
static GList *var_first;
struct search_item {
char *name;
GtkTreePath *path;
};
static GHashTable *die_map;
static void add_contents(GtkBuilder *builder, char *filename);
static void destroy_item(gpointer data);
extern void setup_demangler(void);
extern void finish_demangler(void);
extern bool demangler_enabled(void);
extern int demangle(const char *input, char *output, int outlen);
/* Get a Dwarf from offline image */
static int open_dwarf_file(char *path)
{
int fd;
int err;
Dwfl *dwfl;
Dwfl_Module *mod;
Dwarf_Addr bias;
fd = open(path, O_RDONLY);
if (fd < 0)
return errno;
dwfl = dwfl_begin(&offline_callbacks);
if (!dwfl)
goto error;
dwfl_report_begin(dwfl);
mod = dwfl_report_offline(dwfl, "", "", fd);
if (!mod)
goto error;
dwarf = dwfl_module_getdwarf(mod, &bias);
if (!dwarf)
goto error;
dwfl_report_end(dwfl, NULL, NULL);
return 0;
error:
err = dwarf_errno();
if (dwfl)
dwfl_end(dwfl);
else
close(fd);
if (err == 0)
err = 6; /* no DWARF information */
return err;
}
static void close_dwarf_file(void)
{
if (dwarf == NULL)
return;
dwarf_end(dwarf);
dwarf = NULL;
gtk_tree_store_clear(arg->main_store);
gtk_tree_store_clear(arg->attr_store);
gtk_tree_store_clear(GTK_TREE_STORE(gtk_tree_view_get_model(search->result)));
g_list_free_full(func_list, destroy_item);
g_list_free_full(var_list, destroy_item);
func_list = NULL;
var_list = NULL;
g_hash_table_destroy(die_map);
die_map = NULL;
/* stop and re-enable search */
search->on_going = FALSE;
g_object_set(search->entry, "editable", TRUE, NULL);
gtk_button_set_label(search->button, "Search");
gtk_statusbar_pop(arg->status, arg->status_ctx);
gtk_statusbar_pop(search->status, search->ctx_id);
g_free(search->text);
search->text = NULL;
if (search->patt) {
g_pattern_spec_free(search->patt);
search->patt = NULL;
}
g_free(arg->filename);
g_free(arg);
}
static void show_warning(GtkWidget *parent, const char *fmt, ...)
{
GtkWidget *dialog;
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
char *msg;
va_list ap;
va_start(ap, fmt);
msg = g_strdup_vprintf(fmt, ap);
va_end(ap);
dialog = gtk_message_dialog_new(GTK_WINDOW(parent), flags,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
"%s", msg);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_free(msg);
}
static Elf_Data *get_elf_secdata(Elf *elf, char *sec_name)
{
size_t i, num_sec, strndx;
Elf_Scn *sec;
elf_getshdrnum(elf, &num_sec);
elf_getshdrstrndx(elf, &strndx);
for (i = 0; i < num_sec; i++) {
char *name;
GElf_Shdr shdr;
sec = elf_getscn(elf, i);
gelf_getshdr(sec, &shdr);
name = elf_strptr(elf, strndx, shdr.sh_name);
if (!g_strcmp0(name, sec_name)) {
return elf_getdata(sec, NULL);
}
}
return NULL;
}
static char *print_block(Dwarf_Block *block)
{
int i;
int len = block->length;
char *result = g_malloc(len * 3 + 1);
for (i = 0; i < len; i++) {
snprintf(&result[i * 3], 4, "%02x ", block->data[i]);
}
return result;
}
long read_sleb128(unsigned char *data, int *nbytes)
{
int n = 1;
long val = *data & 0x7f;
while (*data & 0x80) {
data++;
val |= ((unsigned long)(*data & 0x7f) << (7 * n));
n++;
}
if (val & (1UL << (7 * n - 1)))
val |= (-1UL << (7 * n)); /* sign extension */
*nbytes = n;
return val;
}
static const char *get_regname(int regno)
{
static char buf[32];
static const char *gp_regs[] = {
"rax", "rdx", "rcx", "rbx", "rsi", "rdi", "rbp", "rsp",
};
const char *reg = buf;
switch (regno) {
case 0 ... 7:
reg = gp_regs[regno];
break;
case 8 ... 15:
snprintf(buf, sizeof(buf), "r%d", regno);
break;
case 16:
reg = "RA"; /* return address ??? */
break;
case 17 ... 32:
snprintf(buf, sizeof(buf), "xmm%d", regno - 17);
break;
case 33 ... 40:
snprintf(buf, sizeof(buf), "st%d", regno - 33);
break;
case 41 ... 48:
snprintf(buf, sizeof(buf), "mm%d", regno - 41);
break;
default:
reg = "unknown";
break;
}
return reg;
}
static char *print_exprloc(Dwarf_Block *block)
{
int i, n, k;
int len = block->length;
int pos = 0;
int size = 4096;
char *result = g_malloc(size);
long sarg;
unsigned long uarg;
for (i = 0; i < len; i++) {
switch (block->data[i]) {
case 0x03:
memcpy(&uarg, block->data + i + 1, sizeof(uarg));
pos += snprintf(result + pos, size - pos, "03 ");
for (k = 0; k < sizeof(uarg); k++)
pos += snprintf(result + pos, size - pos,
"%02x ", block->data[i + k + 1]);
pos += snprintf(result + pos, size - pos,
"(addr %#lx) ", uarg);
i += sizeof(uarg);
break;
case 0x06:
pos += snprintf(result + pos, size - pos,
"06 (deref) ");
break;
case 0x30 ... 0x4f:
pos += snprintf(result + pos, size - pos,
"%02x (literal %d) ", block->data[i],
block->data[i] - 0x30);
break;
case 0x50 ... 0x6f:
pos += snprintf(result + pos, size - pos,
"%02x (reg%d: %s) ", block->data[i],
block->data[i] - 0x50,
get_regname(block->data[i] - 0x50));
break;
case 0x70 ... 0x8f:
sarg = read_sleb128(block->data + i + 1, &n);
pos += snprintf(result + pos, size - pos,
"%02x ", block->data[i]);
for (k = 0; k < n; k++)
pos += snprintf(result + pos, size - pos,
"%02x ", block->data[i + k + 1]);
pos += snprintf(result + pos, size - pos,
"(%s%+ld) ", get_regname(block->data[i] - 0x70), sarg);
i += n;
break;
case 0x91:
sarg = read_sleb128(block->data + i + 1, &n);
pos += snprintf(result + pos, size - pos, "91 ");
for (k = 0; k < n; k++)
pos += snprintf(result + pos, size - pos,
"%02x ", block->data[i + k + 1]);
pos += snprintf(result + pos, size - pos,
"(fbreg%+ld) ", sarg);
i += n;
break;
case 0x96:
pos += snprintf(result + pos, size - pos,
"96 (nop) ");
break;
case 0x9c:
pos += snprintf(result + pos, size - pos,
"9c (cfa) ");
break;
default:
pos += snprintf(result + pos, size - pos,
"%02x ", block->data[i]);
break;
}
}
return result;
}
static char *print_file_name(Dwarf_Die *die, int idx)
{
Dwarf_Files *files;
const gchar *dir_prefix = NULL;
Dwarf_Word file_idx;
Dwarf_Attribute attr;
Dwarf_Die cudie;
dwarf_diecu(die, &cudie, NULL, NULL);
if (dwarf_attr(&cudie, DW_AT_comp_dir, &attr))
dir_prefix = dwarf_formstring(&attr);
if (dwarf_getsrcfiles(&cudie, &files, NULL) == 0) {
const gchar *short_name = NULL;
const gchar *full_path = dwarf_filesrc(files, idx, NULL, NULL);
if (g_str_has_prefix(full_path, dir_prefix)) {
short_name = full_path + strlen(dir_prefix);
while (*short_name == '/')
short_name++;
}
return g_strdup(short_name ?: full_path);
}
else
return g_strdup_printf("Unknown file: %d\n", idx);
}
static char *print_addr_ranges(Dwarf_Die *die)
{
ptrdiff_t offset = 0;
Dwarf_Addr base, start, end;
char *result = NULL;
while ((offset = dwarf_ranges(die, offset, &base, &start, &end)) > 0) {
result = g_strdup_printf("%s%s[%lx,%lx)", result ?: "",
result ? ", " : "", start, end);
}
return result;
}
static char *die_location(Dwarf_Die *die)
{
gchar *file = NULL;
gint line = 0;
if (dwarf_hasattr(die, DW_AT_decl_file) || dwarf_hasattr(die, DW_AT_call_file)) {
Dwarf_Die cudie;
Dwarf_Word file_idx;
Dwarf_Attribute attr;
dwarf_diecu(die, &cudie, NULL, NULL);
if (dwarf_attr(die, DW_AT_decl_file, &attr) == NULL)
dwarf_attr(die, DW_AT_call_file, &attr);
dwarf_formudata(&attr, &file_idx);
file = print_file_name(&cudie, file_idx);
}
if (dwarf_hasattr(die, DW_AT_decl_line) || dwarf_hasattr(die, DW_AT_call_line)) {
Dwarf_Word lineno;
Dwarf_Attribute attr;
if (dwarf_attr(die, DW_AT_decl_line, &attr) == NULL)
dwarf_attr(die, DW_AT_call_line, &attr);
dwarf_formudata(&attr, &lineno);
line = lineno;
}
return g_strdup_printf(" in %s:%d", file ?: "(unknown)", line);
}
static char *type_name(Dwarf_Die *die)
{
char *type = NULL;
char *name = NULL;
int tag = dwarf_tag(die);
Dwarf_Off off;
Dwarf_Die ref;
Dwarf_Attribute attr;
if (dwarf_hasattr(die, DW_AT_name))
name = (char *)dwarf_diename(die);
switch (tag) {
case DW_TAG_structure_type:
type = "struct";
break;
case DW_TAG_union_type:
type = "union";
break;
case DW_TAG_enumeration_type:
type = "enum";
break;
case DW_TAG_class_type:
type = "class";
break;
case DW_TAG_interface_type:
type = "interface";
break;
case DW_TAG_subroutine_type:
type = "function";
break;
default:
break;
}
if (type || name)
return g_strdup_printf("%s%s%s", type ?: "",
(type && name) ? " " : "", name ?: "");
if (!dwarf_hasattr(die, DW_AT_type))
return g_strdup_printf("no type");
dwarf_attr(die, DW_AT_type, &attr);
switch (dwarf_whatform(&attr)) {
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
case DW_FORM_ref8:
case DW_FORM_ref_udata:
case DW_FORM_ref_addr:
case DW_FORM_ref_sig8:
case DW_FORM_GNU_ref_alt:
dwarf_formref_die(&attr, &ref);
name = type_name(&ref);
break;
default:
name = strdup("");
break;
}
switch (tag) {
case DW_TAG_const_type:
type = g_strdup_printf("const %s", name);
break;
case DW_TAG_volatile_type:
type = g_strdup_printf("volatile %s", name);
break;
case DW_TAG_restrict_type:
type = g_strdup_printf("restrict %s", name);
break;
case DW_TAG_pointer_type:
case DW_TAG_ptr_to_member_type:
type = g_strdup_printf("pointer to %s", name);
break;
case DW_TAG_reference_type:
case DW_TAG_rvalue_reference_type:
type = g_strdup_printf("reference to %s", name);
break;
case DW_TAG_array_type:
type = g_strdup_printf("array of %s", name);
break;
default:
type = g_strdup_printf("unknown type (%d)", tag);
break;
}
free(name);
return type;
}
struct attr_arg {
GtkTreeStore *store;
Dwarf_Die *diep;
};
static int attr_callback(Dwarf_Attribute *attr, void *_arg)
{
struct attr_arg *arg = _arg;
GtkTreeStore *store = arg->store;
GtkTreeIter iter;
unsigned name = dwarf_whatattr(attr);
unsigned form = dwarf_whatform(attr);
unsigned long raw_value = 0;
gpointer val_str = NULL;
Dwarf_Block block;
Dwarf_Word data;
Dwarf_Addr addr;
Dwarf_Off off;
Dwarf_Die die;
switch (form) {
case DW_FORM_flag:
raw_value = *attr->valp;
val_str = g_strdup_printf("%s", raw_value ? "True" : "False");
break;
case DW_FORM_flag_present:
raw_value = 1;
val_str = g_strdup("True");
break;
case DW_FORM_string:
val_str = g_strdup(attr->valp);
break;
case DW_FORM_strp:
case DW_FORM_GNU_strp_alt:
val_str = g_strdup(dwarf_formstring(attr));
break;
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
case DW_FORM_sdata:
case DW_FORM_udata:
case DW_FORM_sec_offset:
dwarf_formudata(attr, &data);
raw_value = data;
if (name == DW_AT_decl_file || name == DW_AT_call_file)
val_str = print_file_name(arg->diep, raw_value);
else if (name == DW_AT_decl_line || name == DW_AT_call_line)
val_str = g_strdup_printf("Line %lu", raw_value);
else if (name == DW_AT_inline)
val_str = g_strdup(dwarview_inline_name(raw_value));
else if (name == DW_AT_ranges)
val_str = print_addr_ranges(arg->diep);
else if (name == DW_AT_language)
val_str = g_strdup(dwarview_language_name(raw_value));
else
val_str = g_strdup_printf("%#lx", raw_value);
break;
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
case DW_FORM_block:
case DW_FORM_exprloc:
dwarf_formblock(attr, &block);
raw_value = block.length;
if (form == DW_FORM_exprloc)
val_str = print_exprloc(&block);
else
val_str = print_block(&block);
break;
case DW_FORM_addr:
dwarf_formaddr(attr, &addr);
raw_value = addr;
val_str = g_strdup_printf("%#lx", raw_value);
break;
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
case DW_FORM_ref8:
case DW_FORM_ref_udata:
case DW_FORM_ref_addr:
case DW_FORM_ref_sig8:
case DW_FORM_GNU_ref_alt:
dwarf_formref(attr, &off);
raw_value = off;
dwarf_formref_die(attr, &die);
if (name == DW_AT_type) {
char *type = type_name(&die);
val_str = g_strdup_printf("%#lx (%s)", raw_value, type);
free(type);
}
else if (dwarf_diename(&die)) {
val_str = g_strdup_printf("%#lx (%s)", raw_value,
dwarf_diename(&die));
}
else
val_str = g_strdup_printf("%#lx", raw_value);
break;
}
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, dwarview_attr_name(name),
1, dwarview_form_name(form),
2, raw_value, 3, val_str ?: "", -1);
g_free(val_str);
return DWARF_CB_OK;
}
static void on_cursor_changed(GtkTreeView *view, gpointer data)
{
GtkTreeView *attr_view = data;
GtkTreeModel *main_model = gtk_tree_view_get_model(view);
GtkTreeModel *attr_model = gtk_tree_view_get_model(attr_view);
GtkTreeIter iter;
GValue val = G_VALUE_INIT;
Dwarf_Off off;
Dwarf_Die die;
struct attr_arg arg = {
.diep = &die,
.store = GTK_TREE_STORE(attr_model),
};
char buf[32];
GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
gtk_tree_selection_get_selected(selection, NULL, &iter);
gtk_tree_model_get_value(main_model, &iter, 0, &val);
off = strtoul(g_value_get_string(&val), NULL, 0);
g_value_unset(&val);
if (dwarf_offdie(dwarf, off, &die) == NULL) {
if ((int)off != -1)
printf("bug?? %lx\n", off);
return;
}
gtk_tree_store_clear(arg.store);
dwarf_getattrs(&die, attr_callback, &arg, 0);
}
static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *column, gpointer user_data)
{
bool expanded = gtk_tree_view_row_expanded(view, path);
if (expanded)
gtk_tree_view_collapse_row(view, path);
else
gtk_tree_view_expand_row(view, path, FALSE);
}
#define MAX_SEARCH_COUNT 1000
static void stop_search(struct search_status *search, const gchar *msg);
static int do_search(struct search_status *search, struct search_item *item)
{
GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model(search->result));
GtkTreeModel *main_model = gtk_tree_view_get_model(search->main_view);
GtkTreeIter iter, main_iter;
GValue val = G_VALUE_INIT;
Dwarf_Off off;
Dwarf_Die die;
gchar *location;
if (!g_pattern_match_string(search->patt, item->name))
return 0;
gtk_tree_model_get_iter(main_model, &main_iter, item->path);
gtk_tree_model_get_value(main_model, &main_iter, 0, &val);
off = strtoul(g_value_get_string(&val), NULL, 0);
g_value_unset(&val);
if (dwarf_offdie(dwarf, off, &die) == NULL)
return -1;
if (dwarf_hasattr(&die, DW_AT_declaration) && !search->with_decl)
return 0;
location = die_location(&die);
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, item->name, 1, location, 2, item->path, -1);
g_free(location);
search->found++;
g_snprintf(search->msgbuf, sizeof(search->msgbuf), "Searching '%s' ... (found %d)",
search->text, search->found);
gtk_statusbar_pop(search->status, search->ctx_id);
gtk_statusbar_push(search->status, search->ctx_id, search->msgbuf);
return 0;
}
static guint search_handler(void *arg)
{
struct search_status *search = arg;
int count = 0;
GList *curr = search->curr;
if (!search->on_going)
return FALSE;
while (curr) {
struct search_item *item = curr->data;
if (do_search(search, item) < 0) {
char tmp[1024];
err:
g_snprintf(tmp, sizeof(tmp), "Failed (at %s).", item->name);
stop_search(search, tmp);
return FALSE;
}
curr = g_list_previous(curr);
if (++count == MAX_SEARCH_COUNT)
goto out;
}
if (search->try_var) {
curr = var_first;
search->try_var = FALSE;
}
while (curr) {
struct search_item *item = curr->data;
if (do_search(search, item) < 0)
goto err;
curr = g_list_previous(curr);
if (++count == MAX_SEARCH_COUNT)
break;
}
out:
search->curr = curr;
if (curr == NULL) {
char tmp[1024];
g_snprintf(tmp, sizeof(tmp), "Done (%d found).", search->found);
stop_search(search, tmp);
}
return curr != NULL;
}
static void start_search(struct search_status *search, const gchar *text)
{
/* delete previous result */
gtk_tree_store_clear(GTK_TREE_STORE(gtk_tree_view_get_model(search->result)));
search->found = 0;
g_free(search->text);
if (search->patt)
g_pattern_spec_free(search->patt);
search->text = g_strdup(text);
search->patt = g_pattern_spec_new(text);
search->try_var = FALSE;
if (gtk_toggle_button_get_active(search->func)) {
search->curr = func_first;
if (gtk_toggle_button_get_active(search->var))
search->try_var = TRUE;
}
else
search->curr = var_first;
search->with_decl = gtk_toggle_button_get_active(search->decl);
search->on_going = TRUE;
g_idle_add((GSourceFunc)search_handler, search);
}
static void stop_search(struct search_status *search, const gchar *msg)
{
search->on_going = FALSE;
g_object_set(search->entry, "editable", TRUE, NULL);
gtk_button_set_label(search->button, "Search");
g_snprintf(search->msgbuf, sizeof(search->msgbuf), "Searching '%s' ... %s",
search->text, msg);
gtk_statusbar_pop(search->status, search->ctx_id);
gtk_statusbar_push(search->status, search->ctx_id, search->msgbuf);
}
static void on_search_activated(GtkEntry *entry, gpointer data)
{
gtk_button_clicked(GTK_BUTTON(data));
}
static void on_search_button(GtkButton *button, gpointer data)
{
struct search_status *search = data;
GtkEntry *entry = GTK_ENTRY(search->entry);
const gchar *text;
if (!search->on_going) {
/* at least one of the check boxes should be set */
if (!gtk_toggle_button_get_active(search->func) &&
!gtk_toggle_button_get_active(search->var))
return;
text = gtk_entry_get_text(entry);
if (*text && g_strcmp0(text, search->text)) {
start_search(search, text);
g_object_set(entry, "editable", FALSE, NULL);
gtk_button_set_label(button, "Stop");
}
}
else {
stop_search(search, "Canceled");
}
}
static void on_search_result(GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *col, gpointer data)
{
GtkTreeView *main_view = data;
GtkTreeModel *model = gtk_tree_view_get_model(view);
GtkTreeIter iter;
GtkTreePath *main_path;
GValue val = G_VALUE_INIT;
gboolean expanded;
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_model_get_value(model, &iter, 2, &val);
main_path = g_value_get_pointer(&val);
g_value_unset(&val);
expanded = gtk_tree_view_row_expanded(main_view, main_path);
gtk_tree_view_expand_to_path(main_view, main_path);
gtk_tree_view_scroll_to_cell(main_view, main_path, NULL, TRUE, 0.5, 0); /* center align */
gtk_tree_view_set_cursor(main_view, main_path, NULL, FALSE);
gtk_tree_view_row_activated(main_view, main_path, NULL);
/* do not change 'expanded' status */
if (!expanded)
gtk_tree_view_collapse_row(main_view, main_path);
}
static void setup_search_status(GtkBuilder *builder)
{
search = g_malloc(sizeof(*search));
search->on_going = FALSE;
search->text = NULL;
search->patt = NULL;
search->entry = GTK_SEARCH_ENTRY(gtk_builder_get_object(builder, "search_entry"));
search->button = GTK_BUTTON(gtk_builder_get_object(builder, "search_btn"));
search->func = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "search_func"));
search->var = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "search_var"));
search->decl = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "search_decl"));
search->result = GTK_TREE_VIEW(gtk_builder_get_object(builder, "search_view"));
search->main_view = GTK_TREE_VIEW(gtk_builder_get_object(builder, "main_view"));
search->status = GTK_STATUSBAR(gtk_builder_get_object(builder, "status"));
search->ctx_id = gtk_statusbar_get_context_id(search->status, "search context");
g_snprintf(search->msgbuf, sizeof(search->msgbuf), "...");
gtk_statusbar_push(search->status, search->ctx_id, search->msgbuf);
g_signal_connect(G_OBJECT(search->button), "clicked", (GCallback)on_search_button, search);
}
static void on_file_open(GtkMenuItem *menu, gpointer *window)
{
int res;
gchar *filename;
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
GtkFileChooser *chooser;
dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW(window), action,
"_Cancel", GTK_RESPONSE_CANCEL,
"_Open", GTK_RESPONSE_ACCEPT,
NULL);
res = gtk_dialog_run (GTK_DIALOG (dialog));
if (res != GTK_RESPONSE_ACCEPT) {
gtk_widget_destroy (dialog);
return;
}
chooser = GTK_FILE_CHOOSER(dialog);
filename = gtk_file_chooser_get_filename(chooser);
gtk_widget_destroy (dialog);
close_dwarf_file();
res = open_dwarf_file(filename);
if (res != 0)
show_warning(GTK_WIDGET(window), "Error: %s: %s\n",
filename, dwarf_errmsg(res));
else
add_contents(builder, filename);
}
static void on_file_close(GtkMenuItem *menu, gpointer *unused)
{
close_dwarf_file();
}
static gboolean on_attr_press(GtkWidget *widget, GdkEvent *event, gpointer data)
{
GtkTreeView *view = GTK_TREE_VIEW(widget);
GtkTreePath *path = NULL;
GtkTreeModel *model = gtk_tree_view_get_model(view);
GtkTreeView *main_view = data;
GtkTreeIter iter;
GValue val = G_VALUE_INIT;
const char *type;
unsigned long off;
bool ref = FALSE;
bool expanded;
/* double-click to follow reference (jump to offset) */
if (event->button.type != GDK_2BUTTON_PRESS)
return FALSE;
gtk_tree_view_get_cursor(view, &path, NULL);
if (path == NULL)
return FALSE;
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_model_get_value(model, &iter, 1, &val);
type = g_value_get_string(&val);
if (!strncmp(type, "ref", 3))
ref = TRUE;
g_value_unset(&val);
if (!ref)
return FALSE;
gtk_tree_model_get_value(model, &iter, 2, &val);
off = g_value_get_ulong(&val);
g_value_unset(&val);
gtk_tree_path_free(path);
path = g_hash_table_lookup(die_map, (void *)off);
if (path == NULL)
return FALSE;
expanded = gtk_tree_view_row_expanded(main_view, path);
gtk_tree_view_expand_to_path(main_view, path);
gtk_tree_view_scroll_to_cell(main_view, path, NULL, TRUE, 0.5, 0); /* center align */
gtk_tree_view_set_cursor(main_view, path, NULL, FALSE);
gtk_tree_view_row_activated(main_view, path, NULL);
/* do not change 'expanded' status */