forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarobj.c
2803 lines (2310 loc) · 73.3 KB
/
varobj.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
/* Implementation of the GDB variable objects API.
Copyright (C) 1999-2015 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "value.h"
#include "expression.h"
#include "frame.h"
#include "language.h"
#include "gdbcmd.h"
#include "block.h"
#include "valprint.h"
#include "gdb_regex.h"
#include "varobj.h"
#include "vec.h"
#include "gdbthread.h"
#include "inferior.h"
#include "varobj-iter.h"
#if HAVE_PYTHON
#include "python/python.h"
#include "python/python-internal.h"
#else
typedef int PyObject;
#endif
/* Non-zero if we want to see trace of varobj level stuff. */
unsigned int varobjdebug = 0;
static void
show_varobjdebug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
}
/* String representations of gdb's format codes. */
char *varobj_format_string[] =
{ "natural", "binary", "decimal", "hexadecimal", "octal" };
/* True if we want to allow Python-based pretty-printing. */
static int pretty_printing = 0;
void
varobj_enable_pretty_printing (void)
{
pretty_printing = 1;
}
/* Data structures */
/* Every root variable has one of these structures saved in its
varobj. Members which must be free'd are noted. */
struct varobj_root
{
/* Alloc'd expression for this parent. */
struct expression *exp;
/* Block for which this expression is valid. */
const struct block *valid_block;
/* The frame for this expression. This field is set iff valid_block is
not NULL. */
struct frame_id frame;
/* The thread ID that this varobj_root belong to. This field
is only valid if valid_block is not NULL.
When not 0, indicates which thread 'frame' belongs to.
When 0, indicates that the thread list was empty when the varobj_root
was created. */
int thread_id;
/* If 1, the -var-update always recomputes the value in the
current thread and frame. Otherwise, variable object is
always updated in the specific scope/thread/frame. */
int floating;
/* Flag that indicates validity: set to 0 when this varobj_root refers
to symbols that do not exist anymore. */
int is_valid;
/* Language-related operations for this variable and its
children. */
const struct lang_varobj_ops *lang_ops;
/* The varobj for this root node. */
struct varobj *rootvar;
/* Next root variable */
struct varobj_root *next;
};
/* Dynamic part of varobj. */
struct varobj_dynamic
{
/* Whether the children of this varobj were requested. This field is
used to decide if dynamic varobj should recompute their children.
In the event that the frontend never asked for the children, we
can avoid that. */
int children_requested;
/* The pretty-printer constructor. If NULL, then the default
pretty-printer will be looked up. If None, then no
pretty-printer will be installed. */
PyObject *constructor;
/* The pretty-printer that has been constructed. If NULL, then a
new printer object is needed, and one will be constructed. */
PyObject *pretty_printer;
/* The iterator returned by the printer's 'children' method, or NULL
if not available. */
struct varobj_iter *child_iter;
/* We request one extra item from the iterator, so that we can
report to the caller whether there are more items than we have
already reported. However, we don't want to install this value
when we read it, because that will mess up future updates. So,
we stash it here instead. */
varobj_item *saved_item;
};
struct cpstack
{
char *name;
struct cpstack *next;
};
/* A list of varobjs */
struct vlist
{
struct varobj *var;
struct vlist *next;
};
/* Private function prototypes */
/* Helper functions for the above subcommands. */
static int delete_variable (struct cpstack **, struct varobj *, int);
static void delete_variable_1 (struct cpstack **, int *,
struct varobj *, int, int);
static int install_variable (struct varobj *);
static void uninstall_variable (struct varobj *);
static struct varobj *create_child (struct varobj *, int, char *);
static struct varobj *
create_child_with_value (struct varobj *parent, int index,
struct varobj_item *item);
/* Utility routines */
static struct varobj *new_variable (void);
static struct varobj *new_root_variable (void);
static void free_variable (struct varobj *var);
static struct cleanup *make_cleanup_free_variable (struct varobj *var);
static enum varobj_display_formats variable_default_display (struct varobj *);
static void cppush (struct cpstack **pstack, char *name);
static char *cppop (struct cpstack **pstack);
static int update_type_if_necessary (struct varobj *var,
struct value *new_value);
static int install_new_value (struct varobj *var, struct value *value,
int initial);
/* Language-specific routines. */
static int number_of_children (const struct varobj *);
static char *name_of_variable (const struct varobj *);
static char *name_of_child (struct varobj *, int);
static struct value *value_of_root (struct varobj **var_handle, int *);
static struct value *value_of_child (const struct varobj *parent, int index);
static char *my_value_of_variable (struct varobj *var,
enum varobj_display_formats format);
static int is_root_p (const struct varobj *var);
static struct varobj *varobj_add_child (struct varobj *var,
struct varobj_item *item);
/* Private data */
/* Mappings of varobj_display_formats enums to gdb's format codes. */
static int format_code[] = { 0, 't', 'd', 'x', 'o' };
/* Header of the list of root variable objects. */
static struct varobj_root *rootlist;
/* Prime number indicating the number of buckets in the hash table. */
/* A prime large enough to avoid too many colisions. */
#define VAROBJ_TABLE_SIZE 227
/* Pointer to the varobj hash table (built at run time). */
static struct vlist **varobj_table;
/* API Implementation */
static int
is_root_p (const struct varobj *var)
{
return (var->root->rootvar == var);
}
#ifdef HAVE_PYTHON
/* Helper function to install a Python environment suitable for
use during operations on VAR. */
struct cleanup *
varobj_ensure_python_env (const struct varobj *var)
{
return ensure_python_env (var->root->exp->gdbarch,
var->root->exp->language_defn);
}
#endif
/* Creates a varobj (not its children). */
/* Return the full FRAME which corresponds to the given CORE_ADDR
or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
static struct frame_info *
find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
{
struct frame_info *frame = NULL;
if (frame_addr == (CORE_ADDR) 0)
return NULL;
for (frame = get_current_frame ();
frame != NULL;
frame = get_prev_frame (frame))
{
/* The CORE_ADDR we get as argument was parsed from a string GDB
output as $fp. This output got truncated to gdbarch_addr_bit.
Truncate the frame base address in the same manner before
comparing it against our argument. */
CORE_ADDR frame_base = get_frame_base_address (frame);
int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
if (frame_base == frame_addr)
return frame;
}
return NULL;
}
struct varobj *
varobj_create (char *objname,
char *expression, CORE_ADDR frame, enum varobj_type type)
{
struct varobj *var;
struct cleanup *old_chain;
/* Fill out a varobj structure for the (root) variable being constructed. */
var = new_root_variable ();
old_chain = make_cleanup_free_variable (var);
if (expression != NULL)
{
struct frame_info *fi;
struct frame_id old_id = null_frame_id;
const struct block *block;
const char *p;
struct value *value = NULL;
CORE_ADDR pc;
/* Parse and evaluate the expression, filling in as much of the
variable's data as possible. */
if (has_stack_frames ())
{
/* Allow creator to specify context of variable. */
if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
fi = get_selected_frame (NULL);
else
/* FIXME: cagney/2002-11-23: This code should be doing a
lookup using the frame ID and not just the frame's
``address''. This, of course, means an interface
change. However, with out that interface change ISAs,
such as the ia64 with its two stacks, won't work.
Similar goes for the case where there is a frameless
function. */
fi = find_frame_addr_in_frame_chain (frame);
}
else
fi = NULL;
/* frame = -2 means always use selected frame. */
if (type == USE_SELECTED_FRAME)
var->root->floating = 1;
pc = 0;
block = NULL;
if (fi != NULL)
{
block = get_frame_block (fi, 0);
pc = get_frame_pc (fi);
}
p = expression;
innermost_block = NULL;
/* Wrap the call to parse expression, so we can
return a sensible error. */
TRY
{
var->root->exp = parse_exp_1 (&p, pc, block, 0);
}
CATCH (except, RETURN_MASK_ERROR)
{
do_cleanups (old_chain);
return NULL;
}
END_CATCH
/* Don't allow variables to be created for types. */
if (var->root->exp->elts[0].opcode == OP_TYPE
|| var->root->exp->elts[0].opcode == OP_TYPEOF
|| var->root->exp->elts[0].opcode == OP_DECLTYPE)
{
do_cleanups (old_chain);
fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
" as an expression.\n");
return NULL;
}
var->format = variable_default_display (var);
var->root->valid_block = innermost_block;
var->name = xstrdup (expression);
/* For a root var, the name and the expr are the same. */
var->path_expr = xstrdup (expression);
/* When the frame is different from the current frame,
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. */
if (innermost_block)
{
/* User could specify explicit FRAME-ADDR which was not found but
EXPRESSION is frame specific and we would not be able to evaluate
it correctly next time. With VALID_BLOCK set we must also set
FRAME and THREAD_ID. */
if (fi == NULL)
error (_("Failed to find the specified frame"));
var->root->frame = get_frame_id (fi);
var->root->thread_id = pid_to_thread_id (inferior_ptid);
old_id = get_frame_id (get_selected_frame (NULL));
select_frame (fi);
}
/* We definitely need to catch errors here.
If evaluate_expression succeeds we got the value we wanted.
But if it fails, we still go on with a call to evaluate_type(). */
TRY
{
value = evaluate_expression (var->root->exp);
}
CATCH (except, RETURN_MASK_ERROR)
{
/* Error getting the value. Try to at least get the
right type. */
struct value *type_only_value = evaluate_type (var->root->exp);
var->type = value_type (type_only_value);
}
END_CATCH
if (value != NULL)
{
int real_type_found = 0;
var->type = value_actual_type (value, 0, &real_type_found);
if (real_type_found)
value = value_cast (var->type, value);
}
/* Set language info */
var->root->lang_ops = var->root->exp->language_defn->la_varobj_ops;
install_new_value (var, value, 1 /* Initial assignment */);
/* Set ourselves as our root. */
var->root->rootvar = var;
/* Reset the selected frame. */
if (frame_id_p (old_id))
select_frame (frame_find_by_id (old_id));
}
/* If the variable object name is null, that means this
is a temporary variable, so don't install it. */
if ((var != NULL) && (objname != NULL))
{
var->obj_name = xstrdup (objname);
/* If a varobj name is duplicated, the install will fail so
we must cleanup. */
if (!install_variable (var))
{
do_cleanups (old_chain);
return NULL;
}
}
discard_cleanups (old_chain);
return var;
}
/* Generates an unique name that can be used for a varobj. */
char *
varobj_gen_name (void)
{
static int id = 0;
char *obj_name;
/* Generate a name for this object. */
id++;
obj_name = xstrprintf ("var%d", id);
return obj_name;
}
/* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
error if OBJNAME cannot be found. */
struct varobj *
varobj_get_handle (char *objname)
{
struct vlist *cv;
const char *chp;
unsigned int index = 0;
unsigned int i = 1;
for (chp = objname; *chp; chp++)
{
index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
}
cv = *(varobj_table + index);
while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
cv = cv->next;
if (cv == NULL)
error (_("Variable object not found"));
return cv->var;
}
/* Given the handle, return the name of the object. */
char *
varobj_get_objname (const struct varobj *var)
{
return var->obj_name;
}
/* Given the handle, return the expression represented by the object. The
result must be freed by the caller. */
char *
varobj_get_expression (const struct varobj *var)
{
return name_of_variable (var);
}
/* Deletes a varobj and all its children if only_children == 0,
otherwise deletes only the children. If DELLIST is non-NULL, it is
assigned a malloc'ed list of all the (malloc'ed) names of the variables
that have been deleted (NULL terminated). Returns the number of deleted
variables. */
int
varobj_delete (struct varobj *var, char ***dellist, int only_children)
{
int delcount;
int mycount;
struct cpstack *result = NULL;
char **cp;
/* Initialize a stack for temporary results. */
cppush (&result, NULL);
if (only_children)
/* Delete only the variable children. */
delcount = delete_variable (&result, var, 1 /* only the children */ );
else
/* Delete the variable and all its children. */
delcount = delete_variable (&result, var, 0 /* parent+children */ );
/* We may have been asked to return a list of what has been deleted. */
if (dellist != NULL)
{
*dellist = xmalloc ((delcount + 1) * sizeof (char *));
cp = *dellist;
mycount = delcount;
*cp = cppop (&result);
while ((*cp != NULL) && (mycount > 0))
{
mycount--;
cp++;
*cp = cppop (&result);
}
if (mycount || (*cp != NULL))
warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
mycount);
}
return delcount;
}
#if HAVE_PYTHON
/* Convenience function for varobj_set_visualizer. Instantiate a
pretty-printer for a given value. */
static PyObject *
instantiate_pretty_printer (PyObject *constructor, struct value *value)
{
PyObject *val_obj = NULL;
PyObject *printer;
val_obj = value_to_value_object (value);
if (! val_obj)
return NULL;
printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
Py_DECREF (val_obj);
return printer;
}
#endif
/* Set/Get variable object display format. */
enum varobj_display_formats
varobj_set_display_format (struct varobj *var,
enum varobj_display_formats format)
{
switch (format)
{
case FORMAT_NATURAL:
case FORMAT_BINARY:
case FORMAT_DECIMAL:
case FORMAT_HEXADECIMAL:
case FORMAT_OCTAL:
var->format = format;
break;
default:
var->format = variable_default_display (var);
}
if (varobj_value_is_changeable_p (var)
&& var->value && !value_lazy (var->value))
{
xfree (var->print_value);
var->print_value = varobj_value_get_print_value (var->value,
var->format, var);
}
return var->format;
}
enum varobj_display_formats
varobj_get_display_format (const struct varobj *var)
{
return var->format;
}
char *
varobj_get_display_hint (const struct varobj *var)
{
char *result = NULL;
#if HAVE_PYTHON
struct cleanup *back_to;
if (!gdb_python_initialized)
return NULL;
back_to = varobj_ensure_python_env (var);
if (var->dynamic->pretty_printer != NULL)
result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
do_cleanups (back_to);
#endif
return result;
}
/* Return true if the varobj has items after TO, false otherwise. */
int
varobj_has_more (const struct varobj *var, int to)
{
if (VEC_length (varobj_p, var->children) > to)
return 1;
return ((to == -1 || VEC_length (varobj_p, var->children) == to)
&& (var->dynamic->saved_item != NULL));
}
/* If the variable object is bound to a specific thread, that
is its evaluation can always be done in context of a frame
inside that thread, returns GDB id of the thread -- which
is always positive. Otherwise, returns -1. */
int
varobj_get_thread_id (const struct varobj *var)
{
if (var->root->valid_block && var->root->thread_id > 0)
return var->root->thread_id;
else
return -1;
}
void
varobj_set_frozen (struct varobj *var, int frozen)
{
/* When a variable is unfrozen, we don't fetch its value.
The 'not_fetched' flag remains set, so next -var-update
won't complain.
We don't fetch the value, because for structures the client
should do -var-update anyway. It would be bad to have different
client-size logic for structure and other types. */
var->frozen = frozen;
}
int
varobj_get_frozen (const struct varobj *var)
{
return var->frozen;
}
/* A helper function that restricts a range to what is actually
available in a VEC. This follows the usual rules for the meaning
of FROM and TO -- if either is negative, the entire range is
used. */
void
varobj_restrict_range (VEC (varobj_p) *children, int *from, int *to)
{
if (*from < 0 || *to < 0)
{
*from = 0;
*to = VEC_length (varobj_p, children);
}
else
{
if (*from > VEC_length (varobj_p, children))
*from = VEC_length (varobj_p, children);
if (*to > VEC_length (varobj_p, children))
*to = VEC_length (varobj_p, children);
if (*from > *to)
*from = *to;
}
}
/* A helper for update_dynamic_varobj_children that installs a new
child when needed. */
static void
install_dynamic_child (struct varobj *var,
VEC (varobj_p) **changed,
VEC (varobj_p) **type_changed,
VEC (varobj_p) **newobj,
VEC (varobj_p) **unchanged,
int *cchanged,
int index,
struct varobj_item *item)
{
if (VEC_length (varobj_p, var->children) < index + 1)
{
/* There's no child yet. */
struct varobj *child = varobj_add_child (var, item);
if (newobj)
{
VEC_safe_push (varobj_p, *newobj, child);
*cchanged = 1;
}
}
else
{
varobj_p existing = VEC_index (varobj_p, var->children, index);
int type_updated = update_type_if_necessary (existing, item->value);
if (type_updated)
{
if (type_changed)
VEC_safe_push (varobj_p, *type_changed, existing);
}
if (install_new_value (existing, item->value, 0))
{
if (!type_updated && changed)
VEC_safe_push (varobj_p, *changed, existing);
}
else if (!type_updated && unchanged)
VEC_safe_push (varobj_p, *unchanged, existing);
}
}
#if HAVE_PYTHON
static int
dynamic_varobj_has_child_method (const struct varobj *var)
{
struct cleanup *back_to;
PyObject *printer = var->dynamic->pretty_printer;
int result;
if (!gdb_python_initialized)
return 0;
back_to = varobj_ensure_python_env (var);
result = PyObject_HasAttr (printer, gdbpy_children_cst);
do_cleanups (back_to);
return result;
}
#endif
/* A factory for creating dynamic varobj's iterators. Returns an
iterator object suitable for iterating over VAR's children. */
static struct varobj_iter *
varobj_get_iterator (struct varobj *var)
{
#if HAVE_PYTHON
if (var->dynamic->pretty_printer)
return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
#endif
gdb_assert_not_reached (_("\
requested an iterator from a non-dynamic varobj"));
}
/* Release and clear VAR's saved item, if any. */
static void
varobj_clear_saved_item (struct varobj_dynamic *var)
{
if (var->saved_item != NULL)
{
value_free (var->saved_item->value);
xfree (var->saved_item);
var->saved_item = NULL;
}
}
static int
update_dynamic_varobj_children (struct varobj *var,
VEC (varobj_p) **changed,
VEC (varobj_p) **type_changed,
VEC (varobj_p) **newobj,
VEC (varobj_p) **unchanged,
int *cchanged,
int update_children,
int from,
int to)
{
int i;
*cchanged = 0;
if (update_children || var->dynamic->child_iter == NULL)
{
varobj_iter_delete (var->dynamic->child_iter);
var->dynamic->child_iter = varobj_get_iterator (var);
varobj_clear_saved_item (var->dynamic);
i = 0;
if (var->dynamic->child_iter == NULL)
return 0;
}
else
i = VEC_length (varobj_p, var->children);
/* We ask for one extra child, so that MI can report whether there
are more children. */
for (; to < 0 || i < to + 1; ++i)
{
varobj_item *item;
/* See if there was a leftover from last time. */
if (var->dynamic->saved_item != NULL)
{
item = var->dynamic->saved_item;
var->dynamic->saved_item = NULL;
}
else
{
item = varobj_iter_next (var->dynamic->child_iter);
/* Release vitem->value so its lifetime is not bound to the
execution of a command. */
if (item != NULL && item->value != NULL)
release_value_or_incref (item->value);
}
if (item == NULL)
{
/* Iteration is done. Remove iterator from VAR. */
varobj_iter_delete (var->dynamic->child_iter);
var->dynamic->child_iter = NULL;
break;
}
/* We don't want to push the extra child on any report list. */
if (to < 0 || i < to)
{
int can_mention = from < 0 || i >= from;
install_dynamic_child (var, can_mention ? changed : NULL,
can_mention ? type_changed : NULL,
can_mention ? newobj : NULL,
can_mention ? unchanged : NULL,
can_mention ? cchanged : NULL, i,
item);
xfree (item);
}
else
{
var->dynamic->saved_item = item;
/* We want to truncate the child list just before this
element. */
break;
}
}
if (i < VEC_length (varobj_p, var->children))
{
int j;
*cchanged = 1;
for (j = i; j < VEC_length (varobj_p, var->children); ++j)
varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
VEC_truncate (varobj_p, var->children, i);
}
/* If there are fewer children than requested, note that the list of
children changed. */
if (to >= 0 && VEC_length (varobj_p, var->children) < to)
*cchanged = 1;
var->num_children = VEC_length (varobj_p, var->children);
return 1;
}
int
varobj_get_num_children (struct varobj *var)
{
if (var->num_children == -1)
{
if (varobj_is_dynamic_p (var))
{
int dummy;
/* If we have a dynamic varobj, don't report -1 children.
So, try to fetch some children first. */
update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
0, 0, 0);
}
else
var->num_children = number_of_children (var);
}
return var->num_children >= 0 ? var->num_children : 0;
}
/* Creates a list of the immediate children of a variable object;
the return code is the number of such children or -1 on error. */
VEC (varobj_p)*
varobj_list_children (struct varobj *var, int *from, int *to)
{
char *name;
int i, children_changed;
var->dynamic->children_requested = 1;
if (varobj_is_dynamic_p (var))
{
/* This, in theory, can result in the number of children changing without
frontend noticing. But well, calling -var-list-children on the same
varobj twice is not something a sane frontend would do. */
update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
&children_changed, 0, 0, *to);
varobj_restrict_range (var->children, from, to);
return var->children;
}
if (var->num_children == -1)
var->num_children = number_of_children (var);
/* If that failed, give up. */
if (var->num_children == -1)
return var->children;
/* If we're called when the list of children is not yet initialized,
allocate enough elements in it. */
while (VEC_length (varobj_p, var->children) < var->num_children)
VEC_safe_push (varobj_p, var->children, NULL);
for (i = 0; i < var->num_children; i++)
{
varobj_p existing = VEC_index (varobj_p, var->children, i);
if (existing == NULL)
{
/* Either it's the first call to varobj_list_children for
this variable object, and the child was never created,
or it was explicitly deleted by the client. */
name = name_of_child (var, i);
existing = create_child (var, i, name);
VEC_replace (varobj_p, var->children, i, existing);
}
}
varobj_restrict_range (var->children, from, to);
return var->children;
}
static struct varobj *
varobj_add_child (struct varobj *var, struct varobj_item *item)
{
varobj_p v = create_child_with_value (var,
VEC_length (varobj_p, var->children),
item);
VEC_safe_push (varobj_p, var->children, v);
return v;
}
/* Obtain the type of an object Variable as a string similar to the one gdb
prints on the console. The caller is responsible for freeing the string.
*/
char *
varobj_get_type (struct varobj *var)
{
/* For the "fake" variables, do not return a type. (Its type is
NULL, too.)
Do not return a type for invalid variables as well. */
if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
return NULL;
return type_to_string (var->type);
}
/* Obtain the type of an object variable. */
struct type *
varobj_get_gdb_type (const struct varobj *var)
{
return var->type;
}