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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
|
/* Variable user interface for GDB, the GNU debugger.
Copyright 1999 Free Software Foundation, Inc.
This file is part of GDB.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include "defs.h"
#include "value.h"
#include "expression.h"
#include "frame.h"
#include "valprint.h"
#include <tcl.h>
#include <tk.h>
#include "gdbtk.h"
#include "gdbtk-wrapper.h"
#include <math.h>
/* Enumeration type defining the return values for valueChanged */
enum value_changed
{
VALUE_UNCHANGED, /* the variable's value is unchanged */
VALUE_CHANGED, /* the variable's value has changed */
VALUE_OUT_OF_SCOPE /* the variable is no longer in scope */
};
/* String representations of the value_changed enums */
static char *value_changed_string[] = {
"VARIABLE_UNCHANGED",
"VARIABLE_CHANGED",
"VARIABLE_OUT_OF_SCOPE",
NULL
};
/* Enumeration for the format types */
enum display_format
{
FORMAT_NATURAL, /* What gdb actually calls 'natural' */
FORMAT_BINARY, /* Binary display */
FORMAT_DECIMAL, /* Decimal display */
FORMAT_HEXADECIMAL, /* Hex display */
FORMAT_OCTAL /* Octal display */
};
/* Mappings of display_format enums to gdb's format codes */
int format_code[] = {0, 't', 'd', 'x', 'o'};
/* String representations of the format codes */
char *format_string[] = {"natural", "binary", "decimal", "hexadecimal", "octal"};
/* Every parent variable keeps a linked list of its children, described
by the following structure. */
struct variable_child {
/* Pointer to the child's data */
struct _gdb_variable *child;
/* Pointer to the next child */
struct variable_child *next;
};
/* Every variable in the system has a structure of this type defined
for it. This structure holds all information necessary to manipulate
a particular object variable. Members which must be freed are noted. */
struct _gdb_variable {
/* Alloc'd name of the variable for this object.. If this variable is a
child, then this name will be the child's source name.
(bar, not foo.bar) */
char *name;
/* The alloc'd real name of this variable. This is used to construct the
variable's children. It is always a valid expression. */
char *real_name;
/* The alloc'd name for this variable's object. This is here for
convenience when constructing this object's children. */
char *obj_name;
/* Alloc'd expression for this variable */
struct expression *exp;
/* Block for which this expression is valid */
struct block *valid_block;
/* The frame for this expression */
CORE_ADDR frame;
/* The value of this expression */
value_ptr value;
/* Did an error occur evaluating the expression or getting its value? */
int error;
/* The number of (immediate) children this variable has */
int num_children;
/* If this object is a child, this points to its parent. */
struct _gdb_variable *parent;
/* A list of this object's children */
struct variable_child *children;
/* The format of the output for this object */
enum display_format format;
};
typedef struct _gdb_variable gdb_variable;
/* This variable will hold the value of the output from gdb
for commands executed through call_gdb_* */
static Tcl_Obj *fputs_obj;
/*
* Public functions defined in this file
*/
int gdb_variable_init PARAMS ((Tcl_Interp *));
/*
* Private functions defined in this file
*/
/* Entries into this file */
static int gdb_variable_command PARAMS ((ClientData, Tcl_Interp *, int,
Tcl_Obj *CONST[]));
static int variable_create PARAMS ((Tcl_Interp *, int, Tcl_Obj *CONST[]));
static void variable_delete PARAMS ((Tcl_Interp *, gdb_variable *));
static void variable_debug PARAMS ((gdb_variable *));
static int variable_obj_command PARAMS ((ClientData, Tcl_Interp *, int,
Tcl_Obj *CONST[]));
static Tcl_Obj *variable_children PARAMS ((Tcl_Interp *, gdb_variable *));
static enum value_changed variable_value_changed PARAMS ((gdb_variable *));
static int variable_format PARAMS ((Tcl_Interp *, int, Tcl_Obj *CONST[],
gdb_variable *));
static int variable_type PARAMS ((Tcl_Interp *, int, Tcl_Obj *CONST[],
gdb_variable *));
static int variable_value PARAMS ((Tcl_Interp *, int, Tcl_Obj *CONST[],
gdb_variable *));
static int variable_editable PARAMS ((gdb_variable *));
/* Helper functions for the above functions. */
static gdb_variable *create_variable PARAMS ((char *, char *, CORE_ADDR));
static void delete_children PARAMS ((Tcl_Interp *, gdb_variable *, int));
static void install_variable PARAMS ((Tcl_Interp *, char *, gdb_variable *));
static void uninstall_variable PARAMS ((Tcl_Interp *, gdb_variable *));
static gdb_variable *child_exists PARAMS ((gdb_variable *, char *));
static gdb_variable *create_child PARAMS ((Tcl_Interp *, gdb_variable *,
char *, int));
static char *name_of_child PARAMS ((gdb_variable *, int));
static int number_of_children PARAMS ((gdb_variable *));
static enum display_format variable_default_display PARAMS ((gdb_variable *));
static void save_child_in_parent PARAMS ((gdb_variable *, gdb_variable *));
static void remove_child_from_parent PARAMS ((gdb_variable *, gdb_variable *));
static struct type *get_type PARAMS ((value_ptr));
static struct type *get_target_type PARAMS ((struct type *));
static Tcl_Obj *get_call_output PARAMS ((void));
static void clear_gdb_output PARAMS ((void));
static int call_gdb_type_print PARAMS ((value_ptr));
static int call_gdb_val_print PARAMS ((value_ptr, int));
static void variable_fputs PARAMS ((const char *, FILE *));
static void null_fputs PARAMS ((const char *, FILE *));
static int my_value_equal PARAMS ((gdb_variable *, value_ptr));
#define INIT_VARIABLE(x) { \
(x)->name = NULL; \
(x)->real_name = NULL; \
(x)->obj_name = NULL; \
(x)->exp = NULL; \
(x)->valid_block = NULL; \
(x)->frame = (CORE_ADDR) 0; \
(x)->value = NULL; \
(x)->error = 0; \
(x)->num_children = 0; \
(x)->parent = NULL; \
(x)->children = NULL; \
(x)->format = FORMAT_NATURAL; \
}
#if defined(FREEIF)
# undef FREEIF
#endif
#define FREEIF(x) if (x != NULL) free((char *) (x))
/* Initialize the variable code. This function should be called once
to install and initialize the variable code into the interpreter. */
int
gdb_variable_init (interp)
Tcl_Interp *interp;
{
Tcl_Command result;
result = Tcl_CreateObjCommand (interp, "gdb_variable", call_wrapper,
(ClientData) gdb_variable_command, NULL);
if (result == NULL)
return TCL_ERROR;
return TCL_OK;
}
/* This function defines the "gdb_variable" command which is used to
create variable objects. Its syntax includes:
gdb_variable create
gdb_variable create NAME
gdb_variable create -expr EXPR
gdb_variable create NAME -expr EXPR
NAME = name of object to create. If no NAME, then automatically create
a name
EXPR = the gdb expression for which to create a variable. This will
be the most common usage.
*/
static int
gdb_variable_command (clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
static char *commands[] = { "create", NULL };
enum commands_enum { VARIABLE_CREATE };
int index, result;
if (objc < 2)
{
Tcl_WrongNumArgs (interp, 1, objv, "option ?arg...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj (interp, objv[1], commands, "options", 0,
&index) != TCL_OK)
{
return TCL_ERROR;
}
switch ((enum commands_enum) index)
{
case VARIABLE_CREATE:
result = variable_create (interp, objc - 2, objv + 2);
break;
default:
return TCL_ERROR;
}
return result;
}
/* This function implements the actual object command for each
variable object that is created (and each of its children).
Currently the following commands are implemented:
- delete delete this object and its children
- valueChanged has the value of this object changed since the last check?
- numChildren how many children does this object have
- children create the children and return a list of their objects
- debug print out a little debug info for the object
- name print out the name of this variable
*/
static int
variable_obj_command (clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
enum commands_enum {
VARIABLE_DELETE,
VARIABLE_VALUE_CHANGED,
VARIABLE_NUM_CHILDREN,
VARIABLE_CHILDREN,
VARIABLE_DEBUG,
VARIABLE_FORMAT,
VARIABLE_TYPE,
VARIABLE_VALUE,
VARIABLE_NAME,
VARIABLE_EDITABLE
};
static char *commands[] = {
"delete",
"valueChanged",
"numChildren",
"children",
"debug",
"format",
"type",
"value",
"name",
"editable",
NULL
};
gdb_variable *var = (gdb_variable *) clientData;
int index, result;
if (objc < 2)
{
Tcl_WrongNumArgs (interp, 1, objv, "option ?arg...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj (interp, objv[1], commands, "options", 0,
&index) != TCL_OK)
return TCL_ERROR;
result = TCL_OK;
switch ((enum commands_enum) index)
{
case VARIABLE_DELETE:
if (objc > 2)
{
int len;
char *s = Tcl_GetStringFromObj (objv[2], &len);
if (*s == 'c' && strncmp (s, "children", len) == 0)
{
delete_children (interp, var, 1);
break;
}
}
variable_delete (interp, var);
break;
case VARIABLE_VALUE_CHANGED:
{
enum value_changed vc = variable_value_changed (var);
Tcl_SetObjResult (interp, Tcl_NewStringObj (value_changed_string[vc], -1));
}
break;
case VARIABLE_NUM_CHILDREN:
Tcl_SetObjResult (interp, Tcl_NewIntObj (var->num_children));
break;
case VARIABLE_CHILDREN:
{
Tcl_Obj *children = variable_children (interp, var);
Tcl_SetObjResult (interp, children);
}
break;
case VARIABLE_DEBUG:
variable_debug (var);
break;
case VARIABLE_FORMAT:
result = variable_format (interp, objc, objv, var);
break;
case VARIABLE_TYPE:
result = variable_type (interp, objc, objv, var);
break;
case VARIABLE_VALUE:
result = variable_value (interp, objc, objv, var);
break;
case VARIABLE_NAME:
Tcl_SetObjResult (interp, Tcl_NewStringObj (var->name, -1));
break;
case VARIABLE_EDITABLE:
Tcl_SetObjResult (interp, Tcl_NewIntObj (variable_editable (var)));
break;
default:
return TCL_ERROR;
}
return result;
}
/*
* Variable object construction/destruction
*/
/* This function is responsible for processing the user's specifications
and constructing a variable object. */
static int
variable_create (interp, objc, objv)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
enum create_opts { CREATE_EXPR, CREATE_PC };
static char *create_options[] = { "-expr", "-pc", NULL };
gdb_variable *var;
char *name;
char obj_name[31];
int index;
static int id = 0;
CORE_ADDR pc = (CORE_ADDR) -1;
/* REMINDER: This command may be invoked in the following ways:
gdb_variable create
gdb_variable create NAME
gdb_variable create -expr EXPR
gdb_variable create NAME -expr EXPR
NAME = name of object to create. If no NAME, then automatically create
a name
EXPR = the gdb expression for which to create a variable. This will
be the most common usage.
*/
name = NULL;
if (objc)
name = Tcl_GetStringFromObj (objv[0], NULL);
if (name == NULL || *name == '-')
{
/* generate a name for this object */
id++;
sprintf (obj_name, "var%d", id);
}
else
{
/* specified name for object */
strncpy (obj_name, name, 30);
objv++;
objc--;
}
/* Run through all the possible options for this command */
name = NULL;
while (objc > 0)
{
if (Tcl_GetIndexFromObj (interp, objv[0], create_options, "options",
0, &index) != TCL_OK)
{
result_ptr->flags |= GDBTK_IN_TCL_RESULT;
return TCL_ERROR;
}
switch ((enum create_opts) index)
{
case CREATE_EXPR:
name = Tcl_GetStringFromObj (objv[1], NULL);
objc--;
objv++;
break;
case CREATE_PC:
{
char *str;
str = Tcl_GetStringFromObj (objv[1], NULL);
pc = parse_and_eval_address (str);
objc--;
objv++;
}
break;
default:
break;
}
objc--;
objv++;
}
/* Create the variable */
var = create_variable (name, name, pc);
if (var != NULL)
{
/* Install a command into the interpreter that represents this
object */
install_variable (interp, obj_name, var);
Tcl_SetObjResult (interp, Tcl_NewStringObj (obj_name, -1));
result_ptr->flags |= GDBTK_IN_TCL_RESULT;
return TCL_OK;
}
return TCL_ERROR;
}
/* Fill out a gdb_variable structure for the variable being constructed.
This function should never fail if real_name is a valid expression.
(That means no longjmp'ing!) */
static gdb_variable *
create_variable (name, real_name, pc)
char *name;
char *real_name;
CORE_ADDR pc;
{
gdb_variable *var;
value_ptr mark;
struct frame_info *fi, *old_fi;
struct block *block;
void (*old_fputs) PARAMS ((const char *, FILE *));
gdb_result r;
var = (gdb_variable *) xmalloc (sizeof (gdb_variable));
INIT_VARIABLE (var);
if (name != NULL)
{
char *p;
/* Parse and evaluate the expression, filling in as much
of the variable's data as possible */
/* Allow creator to specify context of variable */
if (pc == (CORE_ADDR) -1)
block = 0;
else
{
r = GDB_block_for_pc (pc, &block);
if (r != GDB_OK)
block = 0;
}
p = real_name;
innermost_block = NULL;
r = GDB_parse_exp_1 (&p, block, 0, &(var->exp));
if (r != GDB_OK)
{
FREEIF ((char *) var);
return NULL;
}
/* Don't allow variables to be created for types. */
if (var->exp->elts[0].opcode == OP_TYPE)
{
free_current_contents ((char **) &(var->exp));
FREEIF (var);
printf_unfiltered ("Attempt to use a type name as an expression.");
return NULL;
}
var->valid_block = innermost_block;
var->name = savestring (name, strlen (name));
var->real_name = savestring (real_name, strlen (real_name));
/* Several of the GDB_* calls can cause messages to be displayed. We swallow
those here, because we don't need them (the "value" command will
show them). */
old_fputs = fputs_unfiltered_hook;
fputs_unfiltered_hook = null_fputs;
/* When the PC is different from the current PC (pc == -1),
then we must select the appropriate frame before parsing
the expression, otherwise the value will not be current.
Since select_frame is so benign, just call it for all cases. */
r = GDB_block_innermost_frame (var->valid_block, &fi);
if (r != GDB_OK)
fi = NULL;
if (fi)
var->frame = FRAME_FP (fi);
old_fi = selected_frame;
GDB_select_frame (fi, -1);
mark = value_mark ();
if (GDB_evaluate_expression (var->exp, &var->value) == GDB_OK)
{
release_value (var->value);
if (VALUE_LAZY (var->value))
{
if (GDB_value_fetch_lazy (var->value) != GDB_OK)
var->error = 1;
else
var->error = 0;
}
}
else
var->error = 1;
value_free_to_mark (mark);
/* Reset the selected frame */
GDB_select_frame (old_fi, -1);
/* Restore the output hook to normal */
fputs_unfiltered_hook = old_fputs;
var->num_children = number_of_children (var);
var->format = variable_default_display (var);
}
return var;
}
/* Install the given variable VAR into the tcl interpreter with
the object name NAME. */
static void
install_variable (interp, name, var)
Tcl_Interp *interp;
char *name;
gdb_variable *var;
{
var->obj_name = savestring (name, strlen (name));
Tcl_CreateObjCommand (interp, name, variable_obj_command,
(ClientData) var, NULL);
}
/* Unistall the object VAR in the tcl interpreter. */
static void
uninstall_variable (interp, var)
Tcl_Interp *interp;
gdb_variable *var;
{
Tcl_DeleteCommand (interp, var->obj_name);
}
/* Delete the variable object VAR and its children */
static void
variable_delete (interp, var)
Tcl_Interp *interp;
gdb_variable *var;
{
/* Delete any children of this variable, too. */
delete_children (interp, var, 0);
/* If this variable has a parent, remove it from its parent's list */
if (var->parent != NULL)
{
remove_child_from_parent (var->parent, var);
}
uninstall_variable (interp, var);
/* Free memory associated with this variable */
FREEIF (var->name);
FREEIF (var->real_name);
FREEIF (var->obj_name);
if (var->exp != NULL)
free_current_contents ((char **) &var->exp);
FREEIF (var);
}
/* Silly debugging info */
static void
variable_debug (var)
gdb_variable *var;
{
Tcl_Obj *str;
str = Tcl_NewStringObj ("name=", -1);
Tcl_AppendStringsToObj (str, var->name, "\nreal_name=", var->real_name,
"\nobj_name=", var->obj_name, NULL);
Tcl_SetObjResult (gdbtk_interp, str);
}
/*
* Child construction/destruction
*/
/* Delete the children associated with the object VAR. If NOTIFY is set,
notify the parent object that this child was deleted. This is used as
a small optimization when deleting variables and their children. If the
parent is also being deleted, don't bother notifying it that its children
are being deleted. */
static void
delete_children (interp, var, notify)
Tcl_Interp *interp;
gdb_variable *var;
int notify;
{
struct variable_child *vc;
struct variable_child *next;
for (vc = var->children; vc != NULL; vc = next)
{
if (!notify)
vc->child->parent = NULL;
variable_delete (interp, vc->child);
next = vc->next;
free (vc);
}
}
/* Return the number of children for a given variable.
This can get a little complicated, since we would like to make
certain assumptions about certain types of variables.
- struct/union *: dereference first
- (*)(): do not allow derefencing
- arrays:
- declared size = num of children or
- -1 if we don't know, i.e., int foo [];
- if there was an error reported constructing this object,
assume it has no children (and try this again later)
- void * and char * have no children
*/
static int
number_of_children (var)
gdb_variable *var;
{
struct type *type;
struct type *target;
int children;
if (var->value != NULL)
{
type = get_type (var->value);
target = get_target_type (type);
children = 0;
switch (TYPE_CODE (type))
{
case TYPE_CODE_ARRAY:
if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
&& TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
else
children = -1;
break;
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
/* If we have a virtual table pointer, omit it. */
if (TYPE_VPTR_BASETYPE (type) == type
&& !(TYPE_VPTR_FIELDNO (type) < 0))
children = TYPE_NFIELDS (type) - 1;
else
children = TYPE_NFIELDS (type);
break;
case TYPE_CODE_PTR:
/* This is where things get compilcated. All pointers have one child.
Except, of course, for struct and union ptr, which we automagically
dereference for the user and function ptrs, which have no children. */
switch (TYPE_CODE (target))
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
/* If we have a virtual table pointer, omit it. */
if (TYPE_VPTR_BASETYPE (target) == target
&& !(TYPE_VPTR_FIELDNO (target) < 0))
children = TYPE_NFIELDS (target) - 1;
else
children = TYPE_NFIELDS (target);
break;
case TYPE_CODE_FUNC:
children = 0;
break;
default:
/* Don't dereference char* or void*. */
if (TYPE_NAME (target) != NULL
&& (STREQ (TYPE_NAME (target), "char")
|| STREQ (TYPE_NAME (target), "void")))
children = 0;
else
children = 1;
}
break;
default:
break;
}
}
else
{
/* var->value can be null if we tried to access non-existent or
protected memory. In this case, we simply do not allow any
children. This will be checked again when we check if its
value has changed. */
children = 0;
}
return children;
}
/* Return a list of all the children of VAR, creating them if necessary. */
static Tcl_Obj *
variable_children (interp, var)
Tcl_Interp *interp;
gdb_variable *var;
{
Tcl_Obj *list;
gdb_variable *child;
char *name;
int i;
list = Tcl_NewListObj (0, NULL);
for (i = 0; i < var->num_children; i++)
{
/* check if child exists */
name = name_of_child (var, i);
child = child_exists (var, name);
if (child == NULL)
{
child = create_child (interp, var, name, i);
/* name_of_child returns a malloc'd string */
free (name);
}
Tcl_ListObjAppendElement (NULL, list, Tcl_NewStringObj (child->obj_name, -1));
}
return list;
}
/* Does a child with the name NAME exist in VAR? If so, return its data.
If not, return NULL. */
static gdb_variable *
child_exists (var, name)
gdb_variable *var; /* Parent */
char *name; /* name of child */
{
struct variable_child *vc;
for (vc = var->children; vc != NULL; vc = vc->next)
{
if (STREQ (vc->child->name, name))
return vc->child;
}
return NULL;
}
/* Create and install a child of the parent of the given name */
static gdb_variable *
create_child (interp, parent, name, index)
Tcl_Interp *interp;
gdb_variable *parent;
char *name;
int index;
{
struct type *type;
struct type *target;
gdb_variable *child;
char separator[10], prefix[2048], suffix[20];
char *childs_name;
char *save_name;
int deref = 0;
int len;
/* name should never be null. For pointer derefs, it should contain "*name".
For arrays of a known size, the name will simply contain the index into
the array. */
separator[0] = '\0';
prefix[0] = '\0';
suffix[0] = '\0';;
save_name = name;
/* This code must contain a lot of the logic for children based on the parent's
type. */
type = get_type (parent->value);
target = get_target_type (type);
switch (TYPE_CODE (type))
{
case TYPE_CODE_ARRAY:
sprintf (suffix, "[%s]", name);
name = "";
break;
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
if (index < TYPE_N_BASECLASSES (type))
{
strcpy (prefix, "((");
strcat (prefix, name);
strcat (prefix, ")");
strcpy (suffix, ") ");
name = "";
}
else
strcpy (separator, ".");
break;
case TYPE_CODE_PTR:
switch (TYPE_CODE (target))
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
if (index < TYPE_N_BASECLASSES (target))
{
strcpy (prefix, "(*(");
strcat (prefix, name);
strcat (prefix, " *)");
strcpy (suffix, ")");
name = "";
}
else
strcpy (separator, "->");
break;
default:
deref = 1;
break;
}
default:
break;
}
/* When we get here, we should know how to construct a legal
expression for the child's name */
len = strlen (prefix);
len += strlen (parent->real_name);
len += strlen (separator);
len += strlen (name);
len += strlen (suffix);
if (deref)
len += 3;
childs_name = (char *) xmalloc ((len + 1) * sizeof (char));
if (deref)
{
strcpy (childs_name, "(*");
strcat (childs_name, parent->real_name);
strcat (childs_name, suffix);
strcat (childs_name, ")");
}
else
{
strcpy (childs_name, prefix);
strcat (childs_name, parent->real_name);
strcat (childs_name, separator);
strcat (childs_name, name);
strcat (childs_name, suffix);
}
/* childs_name now contains a valid expression for the child */
child = create_variable (save_name, childs_name, (CORE_ADDR) -1);
child->parent = parent;
free (childs_name);
childs_name = (char *) xmalloc ((strlen (parent->obj_name) + strlen (save_name) + 2)
* sizeof (char));
sprintf (childs_name, "%s.%s", parent->obj_name, save_name);
install_variable (interp, childs_name, child);
free (childs_name);
/* Save a pointer to this child in the parent */
save_child_in_parent (parent, child);
return child;
}
/* Save CHILD in the PARENT's data. */
static void
save_child_in_parent (parent, child)
gdb_variable *parent;
gdb_variable *child;
{
struct variable_child *vc;
/* Insert the child at the top */
vc = parent->children;
parent->children =
(struct variable_child *) xmalloc (sizeof (struct variable_child));
parent->children->next = vc;
parent->children->child = child;
}
/* Remove the CHILD from the PARENT's list of children. */
static void
remove_child_from_parent (parent, child)
gdb_variable *parent;
gdb_variable *child;
{
struct variable_child *vc, *prev;
/* Find the child in the parent's list */
prev = NULL;
for (vc = parent->children; vc != NULL; )
{
if (vc->child == child)
break;
prev = vc;
vc = vc->next;
}
if (prev == NULL)
parent->children = vc->next;
else
prev->next = vc->next;
}
/* What is the name of the INDEX'th child of VAR? */
static char *
name_of_child (var, index)
gdb_variable *var;
int index;
{
struct type *type;
struct type *target;
char *name;
char *string;
type = get_type (var->value);
target = get_target_type (type);
switch (TYPE_CODE (type))
{
case TYPE_CODE_ARRAY:
{
/* We never get here unless var->num_children is greater than 0... */
int len = 1;
while ((int) pow ((double) 10, (double) len) < index)
len++;
name = (char *) xmalloc (1 + len * sizeof (char));
sprintf (name, "%d", index);
}
break;
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
string = TYPE_FIELD_NAME (type, index);
name = savestring (string, strlen (string));
break;
case TYPE_CODE_PTR:
switch (TYPE_CODE (target))
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
string = TYPE_FIELD_NAME (target, index);
name = savestring (string, strlen (string));
break;
default:
name = (char *) xmalloc ((strlen (var->name) + 2) * sizeof (char));
sprintf (name, "*%s", var->name);
break;
}
}
return name;
}
/* Has the value of this object changed since the last time we looked?
There are some special cases:
- structs/unions/arrays. The "value" of these never changes.
Only their children's values change.
- if an error occurred with evaluate_expression or fetch_value_lazy,
then we need to be a little more elaborate with our determination
of "value changed". Specifically, the value does not change when
both the previous evaluate fails and the one done here also fails.
*/
static enum value_changed
variable_value_changed (var)
gdb_variable *var;
{
value_ptr mark, new_val;
struct frame_info *fi, *old_fi;
int within_scope;
enum value_changed result;
gdb_result r;
/* Save the selected stack frame, since we will need to change it
in order to evaluate expressions. */
old_fi = selected_frame;
/* Determine whether the variable is still around. */
if (var->valid_block == NULL)
within_scope = 1;
else
{
GDB_reinit_frame_cache ();
r = GDB_find_frame_addr_in_frame_chain (var->frame, &fi);
if (r != GDB_OK)
fi = NULL;
within_scope = fi != NULL;
/* FIXME: GDB_select_frame could fail */
if (within_scope)
GDB_select_frame (fi, -1);
}
result = VALUE_OUT_OF_SCOPE;
if (within_scope)
{
struct type *type = get_type (var->value);
/* Arrays, struct, classes, unions never change value */
if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_STRUCT
|| TYPE_CODE (type) == TYPE_CODE_UNION
|| TYPE_CODE (type) == TYPE_CODE_ARRAY))
result = VALUE_UNCHANGED;
else
{
mark = value_mark ();
if (GDB_evaluate_expression (var->exp, &new_val) == GDB_OK)
{
if (!my_value_equal (var, new_val))
{
/* value changed */
release_value (new_val);
if (var->value == NULL)
{
/* This can happen if there was an error
evaluating the expression (like deref NULL) */
var->num_children = number_of_children (var);
}
value_free (var->value);
var->value = new_val;
result = VALUE_CHANGED;
}
else
result = VALUE_UNCHANGED;
}
else
{
/* evaluate expression failed. If we failed before, then
the value of this variable has not changed. If we
succeed before, then the value did change. */
if (var->value == NULL)
result = VALUE_UNCHANGED;
else
{
var->value = NULL;
var->error = 1;
result = VALUE_CHANGED;
}
}
value_free_to_mark (mark);
}
}
/* Restore selected frame */
GDB_select_frame (old_fi, -1);
return result;
}
static int
variable_format (interp, objc, objv, var)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
gdb_variable *var;
{
if (objc > 2)
{
/* Set the format of VAR to given format */
int len;
char *fmt = Tcl_GetStringFromObj (objv[2], &len);
if (STREQN (fmt, "natural", len))
var->format = FORMAT_NATURAL;
else if (STREQN (fmt, "binary", len))
var->format = FORMAT_NATURAL;
else if (STREQN (fmt, "decimal", len))
var->format = FORMAT_DECIMAL;
else if (STREQN (fmt, "hexadecimal", len))
var->format = FORMAT_HEXADECIMAL;
else if (STREQN (fmt, "octal", len))
var->format = FORMAT_OCTAL;
else
{
Tcl_Obj *obj = Tcl_NewStringObj (NULL, 0);
Tcl_AppendStringsToObj (obj, "unknown display format \"",
fmt, "\": must be: \"natural\", \"binary\""
", \"decimal\", \"hexadecimal\", or \"octal\"",
NULL);
Tcl_SetObjResult (interp, obj);
return TCL_ERROR;
}
}
else
{
/* Report the current format */
Tcl_Obj *fmt;
fmt = Tcl_NewStringObj (format_string [(int) var->format], -1);
Tcl_SetObjResult (interp, fmt);
}
return TCL_OK;
}
/* What is the default display for this variable? We assume that
everything is "natural". Any exceptions? */
static enum display_format
variable_default_display (var)
gdb_variable *var;
{
return FORMAT_NATURAL;
}
/* This function returns the type of a variable in the interpreter (or an error)
and returns either TCL_OK or TCL_ERROR as appropriate. */
static int
variable_type (interp, objc, objv, var)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
gdb_variable *var;
{
int result;
value_ptr val;
char *first, *last, *string;
Tcl_RegExp regexp;
gdb_result r;
if (var->value != NULL)
val = var->value;
else
{
r = GDB_evaluate_type (var->exp, &val);
if (r != GDB_OK)
return TCL_ERROR;
}
result = call_gdb_type_print (val);
if (result == TCL_OK)
{
string = strdup (Tcl_GetStringFromObj (get_call_output (), NULL));
first = string;
/* gdb will print things out like "struct {...}" for anonymous structs.
In gui-land, we don't want the {...}, so we strip it here. */
regexp = Tcl_RegExpCompile (interp, "{...}");
if (Tcl_RegExpExec (interp, regexp, string, first))
{
/* We have an anonymous struct/union/class/enum */
Tcl_RegExpRange (regexp, 0, &first, &last);
if (*(first - 1) == ' ')
first--;
*first = '\0';
}
Tcl_SetObjResult (interp, Tcl_NewStringObj (string, -1));
FREEIF (string);
return TCL_OK;
}
Tcl_SetObjResult (interp, get_call_output ());
return result;
}
/* This function returns the value of a variable in the interpreter (or an error)
and returns either TCL_OK or TCL_ERROR as appropriate. */
static int
variable_value (interp, objc, objv, var)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
gdb_variable *var;
{
int result;
struct type *type;
value_ptr val;
Tcl_Obj *str;
gdb_result r;
int real_addressprint;
/* If we set the value of the variable, objv[2] will contain the
variable's new value. We need to first construct a legal expression
for this -- ugh! */
if (objc > 2)
{
/* Does this cover all the bases? */
struct expression *exp;
value_ptr value;
int saved_input_radix = input_radix;
if (VALUE_LVAL (var->value) != not_lval && var->value->modifiable)
{
char *s;
input_radix = 10; /* ALWAYS reset to decimal temporarily */
s = Tcl_GetStringFromObj (objv[2], NULL);
r = GDB_parse_exp_1 (&s, 0, 0, &exp);
if (r != GDB_OK)
return TCL_ERROR;
if (GDB_evaluate_expression (exp, &value) != GDB_OK)
return TCL_ERROR;
val = value_assign (var->value, value);
value_free (var->value);
release_value (val);
var->value = val;
input_radix = saved_input_radix;
}
return TCL_OK;
}
if (var->value != NULL)
val = var->value;
else
{
/* This can happen if we attempt to get the value of a struct
member when the parent is an invalid pointer.
GDB reports the error as the error derived from accessing the
parent, but we don't have access to that here... */
Tcl_SetObjResult (interp, Tcl_NewStringObj ("???", -1));
return TCL_ERROR;
}
/* C++: addressprint causes val_print to print the
address of the reference, too. So clear it to get
the real value -- BUT ONLY FOR C++ REFERENCE TYPES! */
real_addressprint = addressprint;
/* BOGUS: if val_print sees a struct/class, it will print out its
children instead of "{...}" */
type = get_type (val);
switch (TYPE_CODE (type))
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
str = Tcl_NewStringObj ("{...}", -1);
break;
case TYPE_CODE_ARRAY:
{
char number[256];
str = Tcl_NewStringObj (NULL, 0);
sprintf (number, "%d", var->num_children);
Tcl_AppendStringsToObj (str, "[", number, "]", NULL);
}
break;
case TYPE_CODE_REF:
/* Clear addressprint so that the actual value is printed */
addressprint = 0;
/* fall through */
default:
result = call_gdb_val_print (val, format_code[(int) var->format]);
Tcl_SetObjResult (interp, get_call_output ());
/* Restore addressprint */
addressprint = real_addressprint;
return result;
}
/* We only get here if we encountered one of the "special types" above */
/* Restore addressprint */
addressprint = real_addressprint;
Tcl_SetObjResult (interp, str);
return TCL_OK;
}
/* Is this variable editable? Use the variable's type to make
this determination. */
static int
variable_editable (var)
gdb_variable *var;
{
struct type *type;
int result;
gdb_result r;
type = get_type (var->value);
if (type == NULL)
{
value_ptr val;
r = GDB_evaluate_type (var->exp, &val);
if (r != GDB_OK)
return 0;
type = get_type (val);
}
switch (TYPE_CODE (type))
{
case TYPE_CODE_STRUCT:
case TYPE_CODE_UNION:
case TYPE_CODE_ARRAY:
case TYPE_CODE_FUNC:
case TYPE_CODE_MEMBER:
case TYPE_CODE_METHOD:
result = 0;
break;
default:
result = 1;
break;
}
return result;
}
/*
* Call stuff. These functions are used to capture the output of gdb commands
* without going through the tcl interpreter.
*/
/* Retrieve gdb output in the buffer since last call. */
static Tcl_Obj *
get_call_output ()
{
/* Clear the error flags, in case we errored. */
if (result_ptr != NULL)
result_ptr->flags &= ~GDBTK_ERROR_ONLY;
return fputs_obj;
}
/* Clear the output of the buffer. */
static void
clear_gdb_output ()
{
if (fputs_obj != NULL)
Tcl_DecrRefCount (fputs_obj);
fputs_obj = Tcl_NewStringObj (NULL, -1);
Tcl_IncrRefCount (fputs_obj);
}
/* Call the gdb command "type_print", retaining its output in the buffer. */
static int
call_gdb_type_print (val)
value_ptr val;
{
void (*old_hook) PARAMS ((const char *, FILE *));
int result;
/* Save the old hook and install new hook */
old_hook = fputs_unfiltered_hook;
fputs_unfiltered_hook = variable_fputs;
/* Call our command with our args */
clear_gdb_output ();
if (GDB_type_print (val, "", gdb_stdout, -1) == GDB_OK)
result = TCL_OK;
else
result = TCL_ERROR;
/* Restore fputs hook */
fputs_unfiltered_hook = old_hook;
return result;
}
/* Call the gdb command "val_print", retaining its output in the buffer. */
static int
call_gdb_val_print (val, format)
value_ptr val;
int format;
{
void (*old_hook) PARAMS ((const char *, FILE *));
gdb_result r;
int result;
/* Save the old hook and install new hook */
old_hook = fputs_unfiltered_hook;
fputs_unfiltered_hook = variable_fputs;
/* Call our command with our args */
clear_gdb_output ();
if (VALUE_LAZY (val))
{
r = GDB_value_fetch_lazy (val);
if (r != GDB_OK)
{
fputs_unfiltered_hook = old_hook;
return TCL_ERROR;
}
}
r = GDB_val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), VALUE_ADDRESS (val),
gdb_stdout, format, 1, 0, 0);
if (r == GDB_OK)
result = TCL_OK;
else
result = TCL_ERROR;
/* Restore fputs hook */
fputs_unfiltered_hook = old_hook;
return result;
}
/* The fputs_unfiltered_hook function used to save the output from one of the
call commands in this file. */
static void
variable_fputs (text, stream)
const char *text;
FILE *stream;
{
/* Just append everything to the fputs_obj... Issues with stderr/stdout? */
Tcl_AppendToObj (fputs_obj, (char *) text, -1);
}
/* Empty handler for the fputs_unfiltered_hook. Set the hook to this function
whenever the output is irrelevent. */
static void
null_fputs (text, stream)
const char *text;
FILE *stream;
{
return;
}
/*
* Special wrapper-like stuff to supplement the generic wrappers
*/
/* This returns the type of the variable. This skips past typedefs
and returns the real type of the variable. */
static struct type *
get_type (val)
value_ptr val;
{
struct type *type = NULL;
if (val != NULL)
{
type = VALUE_TYPE (val);
while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
type = TYPE_TARGET_TYPE (type);
}
return type;
}
/* This returns the target type (or NULL) of TYPE, also skipping
past typedefs, just like get_type (). */
static struct type *
get_target_type (type)
struct type *type;
{
if (type != NULL)
{
type = TYPE_TARGET_TYPE (type);
while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
type = TYPE_TARGET_TYPE (type);
}
return type;
}
/* This function is a special wrap. This call never "fails".*/
static int
my_value_equal (var, val2)
gdb_variable *var;
value_ptr val2;
{
int err1, err2, r;
/* This is bogus, but unfortunately necessary. We must know
exactly what caused an error -- reading var->val (which we
get from var->error and/or val2, so that we can really determine
if we think that something has changed. */
err1 = var->error;
err2 = 0;
if (VALUE_LAZY (val2) && GDB_value_fetch_lazy (val2) != GDB_OK)
err2 = 1;
/* Another special case: NULL values. If both are null, say
they're equal. */
if (var->value == NULL && val2 == NULL)
return 1;
else if (var->value == NULL || val2 == NULL)
return 0;
if (GDB_value_equal (var->value, val2, &r) != GDB_OK)
{
/* An error occurred, this could have happened if
either val1 or val2 errored. ERR1 and ERR2 tell
us which of these it is. If both errored, then
we assume nothing has changed. If one of them is
valid, though, then something has changed. */
if (err1 == err2)
{
/* both the old and new values caused errors, so
we say the value did not change */
/* This is indeterminate, though. Perhaps we should
be safe and say, yes, it changed anyway?? */
return 1;
}
else
{
/* err2 replaces var->error since this new value
WILL replace the old one. */
var->error = err2;
return 0;
}
}
return r;
}
/* Local variables: */
/* change-log-default-name: "ChangeLog-gdbtk" */
/* End: */
|