OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [gdb/] [varobj.c] - Blame information for rev 827

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Implementation of the GDB variable objects API.
2
 
3
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4
   Free Software Foundation, Inc.
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
 
19
#include "defs.h"
20
#include "exceptions.h"
21
#include "value.h"
22
#include "expression.h"
23
#include "frame.h"
24
#include "language.h"
25
#include "wrapper.h"
26
#include "gdbcmd.h"
27
#include "block.h"
28
 
29
#include "gdb_assert.h"
30
#include "gdb_string.h"
31
 
32
#include "varobj.h"
33
#include "vec.h"
34
 
35
/* Non-zero if we want to see trace of varobj level stuff.  */
36
 
37
int varobjdebug = 0;
38
static void
39
show_varobjdebug (struct ui_file *file, int from_tty,
40
                  struct cmd_list_element *c, const char *value)
41
{
42
  fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
43
}
44
 
45
/* String representations of gdb's format codes */
46
char *varobj_format_string[] =
47
  { "natural", "binary", "decimal", "hexadecimal", "octal" };
48
 
49
/* String representations of gdb's known languages */
50
char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
51
 
52
/* Data structures */
53
 
54
/* Every root variable has one of these structures saved in its
55
   varobj. Members which must be free'd are noted. */
56
struct varobj_root
57
{
58
 
59
  /* Alloc'd expression for this parent. */
60
  struct expression *exp;
61
 
62
  /* Block for which this expression is valid */
63
  struct block *valid_block;
64
 
65
  /* The frame for this expression */
66
  struct frame_id frame;
67
 
68
  /* If 1, "update" always recomputes the frame & valid block
69
     using the currently selected frame. */
70
  int use_selected_frame;
71
 
72
  /* Flag that indicates validity: set to 0 when this varobj_root refers
73
     to symbols that do not exist anymore.  */
74
  int is_valid;
75
 
76
  /* Language info for this variable and its children */
77
  struct language_specific *lang;
78
 
79
  /* The varobj for this root node. */
80
  struct varobj *rootvar;
81
 
82
  /* Next root variable */
83
  struct varobj_root *next;
84
};
85
 
86
/* Every variable in the system has a structure of this type defined
87
   for it. This structure holds all information necessary to manipulate
88
   a particular object variable. Members which must be freed are noted. */
89
struct varobj
90
{
91
 
92
  /* Alloc'd name of the variable for this object.. If this variable is a
93
     child, then this name will be the child's source name.
94
     (bar, not foo.bar) */
95
  /* NOTE: This is the "expression" */
96
  char *name;
97
 
98
  /* Alloc'd expression for this child.  Can be used to create a
99
     root variable corresponding to this child.  */
100
  char *path_expr;
101
 
102
  /* The alloc'd name for this variable's object. This is here for
103
     convenience when constructing this object's children. */
104
  char *obj_name;
105
 
106
  /* Index of this variable in its parent or -1 */
107
  int index;
108
 
109
  /* The type of this variable.  This can be NULL
110
     for artifial variable objects -- currently, the "accessibility"
111
     variable objects in C++.  */
112
  struct type *type;
113
 
114
  /* The value of this expression or subexpression.  A NULL value
115
     indicates there was an error getting this value.
116
     Invariant: if varobj_value_is_changeable_p (this) is non-zero,
117
     the value is either NULL, or not lazy.  */
118
  struct value *value;
119
 
120
  /* The number of (immediate) children this variable has */
121
  int num_children;
122
 
123
  /* If this object is a child, this points to its immediate parent. */
124
  struct varobj *parent;
125
 
126
  /* Children of this object.  */
127
  VEC (varobj_p) *children;
128
 
129
  /* Description of the root variable. Points to root variable for children. */
130
  struct varobj_root *root;
131
 
132
  /* The format of the output for this object */
133
  enum varobj_display_formats format;
134
 
135
  /* Was this variable updated via a varobj_set_value operation */
136
  int updated;
137
 
138
  /* Last print value.  */
139
  char *print_value;
140
 
141
  /* Is this variable frozen.  Frozen variables are never implicitly
142
     updated by -var-update *
143
     or -var-update <direct-or-indirect-parent>.  */
144
  int frozen;
145
 
146
  /* Is the value of this variable intentionally not fetched?  It is
147
     not fetched if either the variable is frozen, or any parents is
148
     frozen.  */
149
  int not_fetched;
150
};
151
 
152
struct cpstack
153
{
154
  char *name;
155
  struct cpstack *next;
156
};
157
 
158
/* A list of varobjs */
159
 
160
struct vlist
161
{
162
  struct varobj *var;
163
  struct vlist *next;
164
};
165
 
166
/* Private function prototypes */
167
 
168
/* Helper functions for the above subcommands. */
169
 
170
static int delete_variable (struct cpstack **, struct varobj *, int);
171
 
172
static void delete_variable_1 (struct cpstack **, int *,
173
                               struct varobj *, int, int);
174
 
175
static int install_variable (struct varobj *);
176
 
177
static void uninstall_variable (struct varobj *);
178
 
179
static struct varobj *create_child (struct varobj *, int, char *);
180
 
181
/* Utility routines */
182
 
183
static struct varobj *new_variable (void);
184
 
185
static struct varobj *new_root_variable (void);
186
 
187
static void free_variable (struct varobj *var);
188
 
189
static struct cleanup *make_cleanup_free_variable (struct varobj *var);
190
 
191
static struct type *get_type (struct varobj *var);
192
 
193
static struct type *get_value_type (struct varobj *var);
194
 
195
static struct type *get_target_type (struct type *);
196
 
197
static enum varobj_display_formats variable_default_display (struct varobj *);
198
 
199
static void cppush (struct cpstack **pstack, char *name);
200
 
201
static char *cppop (struct cpstack **pstack);
202
 
203
static int install_new_value (struct varobj *var, struct value *value,
204
                              int initial);
205
 
206
/* Language-specific routines. */
207
 
208
static enum varobj_languages variable_language (struct varobj *var);
209
 
210
static int number_of_children (struct varobj *);
211
 
212
static char *name_of_variable (struct varobj *);
213
 
214
static char *name_of_child (struct varobj *, int);
215
 
216
static struct value *value_of_root (struct varobj **var_handle, int *);
217
 
218
static struct value *value_of_child (struct varobj *parent, int index);
219
 
220
static char *my_value_of_variable (struct varobj *var);
221
 
222
static char *value_get_print_value (struct value *value,
223
                                    enum varobj_display_formats format);
224
 
225
static int varobj_value_is_changeable_p (struct varobj *var);
226
 
227
static int is_root_p (struct varobj *var);
228
 
229
/* C implementation */
230
 
231
static int c_number_of_children (struct varobj *var);
232
 
233
static char *c_name_of_variable (struct varobj *parent);
234
 
235
static char *c_name_of_child (struct varobj *parent, int index);
236
 
237
static char *c_path_expr_of_child (struct varobj *child);
238
 
239
static struct value *c_value_of_root (struct varobj **var_handle);
240
 
241
static struct value *c_value_of_child (struct varobj *parent, int index);
242
 
243
static struct type *c_type_of_child (struct varobj *parent, int index);
244
 
245
static char *c_value_of_variable (struct varobj *var);
246
 
247
/* C++ implementation */
248
 
249
static int cplus_number_of_children (struct varobj *var);
250
 
251
static void cplus_class_num_children (struct type *type, int children[3]);
252
 
253
static char *cplus_name_of_variable (struct varobj *parent);
254
 
255
static char *cplus_name_of_child (struct varobj *parent, int index);
256
 
257
static char *cplus_path_expr_of_child (struct varobj *child);
258
 
259
static struct value *cplus_value_of_root (struct varobj **var_handle);
260
 
261
static struct value *cplus_value_of_child (struct varobj *parent, int index);
262
 
263
static struct type *cplus_type_of_child (struct varobj *parent, int index);
264
 
265
static char *cplus_value_of_variable (struct varobj *var);
266
 
267
/* Java implementation */
268
 
269
static int java_number_of_children (struct varobj *var);
270
 
271
static char *java_name_of_variable (struct varobj *parent);
272
 
273
static char *java_name_of_child (struct varobj *parent, int index);
274
 
275
static char *java_path_expr_of_child (struct varobj *child);
276
 
277
static struct value *java_value_of_root (struct varobj **var_handle);
278
 
279
static struct value *java_value_of_child (struct varobj *parent, int index);
280
 
281
static struct type *java_type_of_child (struct varobj *parent, int index);
282
 
283
static char *java_value_of_variable (struct varobj *var);
284
 
285
/* The language specific vector */
286
 
287
struct language_specific
288
{
289
 
290
  /* The language of this variable */
291
  enum varobj_languages language;
292
 
293
  /* The number of children of PARENT. */
294
  int (*number_of_children) (struct varobj * parent);
295
 
296
  /* The name (expression) of a root varobj. */
297
  char *(*name_of_variable) (struct varobj * parent);
298
 
299
  /* The name of the INDEX'th child of PARENT. */
300
  char *(*name_of_child) (struct varobj * parent, int index);
301
 
302
  /* Returns the rooted expression of CHILD, which is a variable
303
     obtain that has some parent.  */
304
  char *(*path_expr_of_child) (struct varobj * child);
305
 
306
  /* The ``struct value *'' of the root variable ROOT. */
307
  struct value *(*value_of_root) (struct varobj ** root_handle);
308
 
309
  /* The ``struct value *'' of the INDEX'th child of PARENT. */
310
  struct value *(*value_of_child) (struct varobj * parent, int index);
311
 
312
  /* The type of the INDEX'th child of PARENT. */
313
  struct type *(*type_of_child) (struct varobj * parent, int index);
314
 
315
  /* The current value of VAR. */
316
  char *(*value_of_variable) (struct varobj * var);
317
};
318
 
319
/* Array of known source language routines. */
320
static struct language_specific languages[vlang_end] = {
321
  /* Unknown (try treating as C */
322
  {
323
   vlang_unknown,
324
   c_number_of_children,
325
   c_name_of_variable,
326
   c_name_of_child,
327
   c_path_expr_of_child,
328
   c_value_of_root,
329
   c_value_of_child,
330
   c_type_of_child,
331
   c_value_of_variable}
332
  ,
333
  /* C */
334
  {
335
   vlang_c,
336
   c_number_of_children,
337
   c_name_of_variable,
338
   c_name_of_child,
339
   c_path_expr_of_child,
340
   c_value_of_root,
341
   c_value_of_child,
342
   c_type_of_child,
343
   c_value_of_variable}
344
  ,
345
  /* C++ */
346
  {
347
   vlang_cplus,
348
   cplus_number_of_children,
349
   cplus_name_of_variable,
350
   cplus_name_of_child,
351
   cplus_path_expr_of_child,
352
   cplus_value_of_root,
353
   cplus_value_of_child,
354
   cplus_type_of_child,
355
   cplus_value_of_variable}
356
  ,
357
  /* Java */
358
  {
359
   vlang_java,
360
   java_number_of_children,
361
   java_name_of_variable,
362
   java_name_of_child,
363
   java_path_expr_of_child,
364
   java_value_of_root,
365
   java_value_of_child,
366
   java_type_of_child,
367
   java_value_of_variable}
368
};
369
 
370
/* A little convenience enum for dealing with C++/Java */
371
enum vsections
372
{
373
  v_public = 0, v_private, v_protected
374
};
375
 
376
/* Private data */
377
 
378
/* Mappings of varobj_display_formats enums to gdb's format codes */
379
static int format_code[] = { 0, 't', 'd', 'x', 'o' };
380
 
381
/* Header of the list of root variable objects */
382
static struct varobj_root *rootlist;
383
static int rootcount = 0;        /* number of root varobjs in the list */
384
 
385
/* Prime number indicating the number of buckets in the hash table */
386
/* A prime large enough to avoid too many colisions */
387
#define VAROBJ_TABLE_SIZE 227
388
 
389
/* Pointer to the varobj hash table (built at run time) */
390
static struct vlist **varobj_table;
391
 
392
/* Is the variable X one of our "fake" children? */
393
#define CPLUS_FAKE_CHILD(x) \
394
((x) != NULL && (x)->type == NULL && (x)->value == NULL)
395
 
396
 
397
/* API Implementation */
398
static int
399
is_root_p (struct varobj *var)
400
{
401
  return (var->root->rootvar == var);
402
}
403
 
404
/* Creates a varobj (not its children) */
405
 
406
/* Return the full FRAME which corresponds to the given CORE_ADDR
407
   or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
408
 
409
static struct frame_info *
410
find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
411
{
412
  struct frame_info *frame = NULL;
413
 
414
  if (frame_addr == (CORE_ADDR) 0)
415
    return NULL;
416
 
417
  while (1)
418
    {
419
      frame = get_prev_frame (frame);
420
      if (frame == NULL)
421
        return NULL;
422
      if (get_frame_base_address (frame) == frame_addr)
423
        return frame;
424
    }
425
}
426
 
427
struct varobj *
428
varobj_create (char *objname,
429
               char *expression, CORE_ADDR frame, enum varobj_type type)
430
{
431
  struct varobj *var;
432
  struct frame_info *fi;
433
  struct frame_info *old_fi = NULL;
434
  struct block *block;
435
  struct cleanup *old_chain;
436
 
437
  /* Fill out a varobj structure for the (root) variable being constructed. */
438
  var = new_root_variable ();
439
  old_chain = make_cleanup_free_variable (var);
440
 
441
  if (expression != NULL)
442
    {
443
      char *p;
444
      enum varobj_languages lang;
445
      struct value *value = NULL;
446
      int expr_len;
447
 
448
      /* Parse and evaluate the expression, filling in as much
449
         of the variable's data as possible */
450
 
451
      /* Allow creator to specify context of variable */
452
      if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
453
        fi = deprecated_safe_get_selected_frame ();
454
      else
455
        /* FIXME: cagney/2002-11-23: This code should be doing a
456
           lookup using the frame ID and not just the frame's
457
           ``address''.  This, of course, means an interface change.
458
           However, with out that interface change ISAs, such as the
459
           ia64 with its two stacks, won't work.  Similar goes for the
460
           case where there is a frameless function.  */
461
        fi = find_frame_addr_in_frame_chain (frame);
462
 
463
      /* frame = -2 means always use selected frame */
464
      if (type == USE_SELECTED_FRAME)
465
        var->root->use_selected_frame = 1;
466
 
467
      block = NULL;
468
      if (fi != NULL)
469
        block = get_frame_block (fi, 0);
470
 
471
      p = expression;
472
      innermost_block = NULL;
473
      /* Wrap the call to parse expression, so we can
474
         return a sensible error. */
475
      if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
476
        {
477
          return NULL;
478
        }
479
 
480
      /* Don't allow variables to be created for types. */
481
      if (var->root->exp->elts[0].opcode == OP_TYPE)
482
        {
483
          do_cleanups (old_chain);
484
          fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
485
                              " as an expression.\n");
486
          return NULL;
487
        }
488
 
489
      var->format = variable_default_display (var);
490
      var->root->valid_block = innermost_block;
491
      expr_len = strlen (expression);
492
      var->name = savestring (expression, expr_len);
493
      /* For a root var, the name and the expr are the same.  */
494
      var->path_expr = savestring (expression, expr_len);
495
 
496
      /* When the frame is different from the current frame,
497
         we must select the appropriate frame before parsing
498
         the expression, otherwise the value will not be current.
499
         Since select_frame is so benign, just call it for all cases. */
500
      if (fi != NULL)
501
        {
502
          var->root->frame = get_frame_id (fi);
503
          old_fi = get_selected_frame (NULL);
504
          select_frame (fi);
505
        }
506
 
507
      /* We definitely need to catch errors here.
508
         If evaluate_expression succeeds we got the value we wanted.
509
         But if it fails, we still go on with a call to evaluate_type()  */
510
      if (!gdb_evaluate_expression (var->root->exp, &value))
511
        {
512
          /* Error getting the value.  Try to at least get the
513
             right type.  */
514
          struct value *type_only_value = evaluate_type (var->root->exp);
515
          var->type = value_type (type_only_value);
516
        }
517
      else
518
        var->type = value_type (value);
519
 
520
      install_new_value (var, value, 1 /* Initial assignment */);
521
 
522
      /* Set language info */
523
      lang = variable_language (var);
524
      var->root->lang = &languages[lang];
525
 
526
      /* Set ourselves as our root */
527
      var->root->rootvar = var;
528
 
529
      /* Reset the selected frame */
530
      if (fi != NULL)
531
        select_frame (old_fi);
532
    }
533
 
534
  /* If the variable object name is null, that means this
535
     is a temporary variable, so don't install it. */
536
 
537
  if ((var != NULL) && (objname != NULL))
538
    {
539
      var->obj_name = savestring (objname, strlen (objname));
540
 
541
      /* If a varobj name is duplicated, the install will fail so
542
         we must clenup */
543
      if (!install_variable (var))
544
        {
545
          do_cleanups (old_chain);
546
          return NULL;
547
        }
548
    }
549
 
550
  discard_cleanups (old_chain);
551
  return var;
552
}
553
 
554
/* Generates an unique name that can be used for a varobj */
555
 
556
char *
557
varobj_gen_name (void)
558
{
559
  static int id = 0;
560
  char *obj_name;
561
 
562
  /* generate a name for this object */
563
  id++;
564
  obj_name = xstrprintf ("var%d", id);
565
 
566
  return obj_name;
567
}
568
 
569
/* Given an "objname", returns the pointer to the corresponding varobj
570
   or NULL if not found */
571
 
572
struct varobj *
573
varobj_get_handle (char *objname)
574
{
575
  struct vlist *cv;
576
  const char *chp;
577
  unsigned int index = 0;
578
  unsigned int i = 1;
579
 
580
  for (chp = objname; *chp; chp++)
581
    {
582
      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
583
    }
584
 
585
  cv = *(varobj_table + index);
586
  while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
587
    cv = cv->next;
588
 
589
  if (cv == NULL)
590
    error (_("Variable object not found"));
591
 
592
  return cv->var;
593
}
594
 
595
/* Given the handle, return the name of the object */
596
 
597
char *
598
varobj_get_objname (struct varobj *var)
599
{
600
  return var->obj_name;
601
}
602
 
603
/* Given the handle, return the expression represented by the object */
604
 
605
char *
606
varobj_get_expression (struct varobj *var)
607
{
608
  return name_of_variable (var);
609
}
610
 
611
/* Deletes a varobj and all its children if only_children == 0,
612
   otherwise deletes only the children; returns a malloc'ed list of all the
613
   (malloc'ed) names of the variables that have been deleted (NULL terminated) */
614
 
615
int
616
varobj_delete (struct varobj *var, char ***dellist, int only_children)
617
{
618
  int delcount;
619
  int mycount;
620
  struct cpstack *result = NULL;
621
  char **cp;
622
 
623
  /* Initialize a stack for temporary results */
624
  cppush (&result, NULL);
625
 
626
  if (only_children)
627
    /* Delete only the variable children */
628
    delcount = delete_variable (&result, var, 1 /* only the children */ );
629
  else
630
    /* Delete the variable and all its children */
631
    delcount = delete_variable (&result, var, 0 /* parent+children */ );
632
 
633
  /* We may have been asked to return a list of what has been deleted */
634
  if (dellist != NULL)
635
    {
636
      *dellist = xmalloc ((delcount + 1) * sizeof (char *));
637
 
638
      cp = *dellist;
639
      mycount = delcount;
640
      *cp = cppop (&result);
641
      while ((*cp != NULL) && (mycount > 0))
642
        {
643
          mycount--;
644
          cp++;
645
          *cp = cppop (&result);
646
        }
647
 
648
      if (mycount || (*cp != NULL))
649
        warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
650
                 mycount);
651
    }
652
 
653
  return delcount;
654
}
655
 
656
/* Set/Get variable object display format */
657
 
658
enum varobj_display_formats
659
varobj_set_display_format (struct varobj *var,
660
                           enum varobj_display_formats format)
661
{
662
  switch (format)
663
    {
664
    case FORMAT_NATURAL:
665
    case FORMAT_BINARY:
666
    case FORMAT_DECIMAL:
667
    case FORMAT_HEXADECIMAL:
668
    case FORMAT_OCTAL:
669
      var->format = format;
670
      break;
671
 
672
    default:
673
      var->format = variable_default_display (var);
674
    }
675
 
676
  if (varobj_value_is_changeable_p (var)
677
      && var->value && !value_lazy (var->value))
678
    {
679
      free (var->print_value);
680
      var->print_value = value_get_print_value (var->value, var->format);
681
    }
682
 
683
  return var->format;
684
}
685
 
686
enum varobj_display_formats
687
varobj_get_display_format (struct varobj *var)
688
{
689
  return var->format;
690
}
691
 
692
void
693
varobj_set_frozen (struct varobj *var, int frozen)
694
{
695
  /* When a variable is unfrozen, we don't fetch its value.
696
     The 'not_fetched' flag remains set, so next -var-update
697
     won't complain.
698
 
699
     We don't fetch the value, because for structures the client
700
     should do -var-update anyway.  It would be bad to have different
701
     client-size logic for structure and other types.  */
702
  var->frozen = frozen;
703
}
704
 
705
int
706
varobj_get_frozen (struct varobj *var)
707
{
708
  return var->frozen;
709
}
710
 
711
 
712
int
713
varobj_get_num_children (struct varobj *var)
714
{
715
  if (var->num_children == -1)
716
    var->num_children = number_of_children (var);
717
 
718
  return var->num_children;
719
}
720
 
721
/* Creates a list of the immediate children of a variable object;
722
   the return code is the number of such children or -1 on error */
723
 
724
VEC (varobj_p)*
725
varobj_list_children (struct varobj *var)
726
{
727
  struct varobj *child;
728
  char *name;
729
  int i;
730
 
731
  if (var->num_children == -1)
732
    var->num_children = number_of_children (var);
733
 
734
  /* If that failed, give up.  */
735
  if (var->num_children == -1)
736
    return var->children;
737
 
738
  /* If we're called when the list of children is not yet initialized,
739
     allocate enough elements in it.  */
740
  while (VEC_length (varobj_p, var->children) < var->num_children)
741
    VEC_safe_push (varobj_p, var->children, NULL);
742
 
743
  for (i = 0; i < var->num_children; i++)
744
    {
745
      varobj_p existing = VEC_index (varobj_p, var->children, i);
746
 
747
      if (existing == NULL)
748
        {
749
          /* Either it's the first call to varobj_list_children for
750
             this variable object, and the child was never created,
751
             or it was explicitly deleted by the client.  */
752
          name = name_of_child (var, i);
753
          existing = create_child (var, i, name);
754
          VEC_replace (varobj_p, var->children, i, existing);
755
        }
756
    }
757
 
758
  return var->children;
759
}
760
 
761
/* Obtain the type of an object Variable as a string similar to the one gdb
762
   prints on the console */
763
 
764
char *
765
varobj_get_type (struct varobj *var)
766
{
767
  struct value *val;
768
  struct cleanup *old_chain;
769
  struct ui_file *stb;
770
  char *thetype;
771
  long length;
772
 
773
  /* For the "fake" variables, do not return a type. (It's type is
774
     NULL, too.)
775
     Do not return a type for invalid variables as well.  */
776
  if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
777
    return NULL;
778
 
779
  stb = mem_fileopen ();
780
  old_chain = make_cleanup_ui_file_delete (stb);
781
 
782
  /* To print the type, we simply create a zero ``struct value *'' and
783
     cast it to our type. We then typeprint this variable. */
784
  val = value_zero (var->type, not_lval);
785
  type_print (value_type (val), "", stb, -1);
786
 
787
  thetype = ui_file_xstrdup (stb, &length);
788
  do_cleanups (old_chain);
789
  return thetype;
790
}
791
 
792
/* Obtain the type of an object variable.  */
793
 
794
struct type *
795
varobj_get_gdb_type (struct varobj *var)
796
{
797
  return var->type;
798
}
799
 
800
/* Return a pointer to the full rooted expression of varobj VAR.
801
   If it has not been computed yet, compute it.  */
802
char *
803
varobj_get_path_expr (struct varobj *var)
804
{
805
  if (var->path_expr != NULL)
806
    return var->path_expr;
807
  else
808
    {
809
      /* For root varobjs, we initialize path_expr
810
         when creating varobj, so here it should be
811
         child varobj.  */
812
      gdb_assert (!is_root_p (var));
813
      return (*var->root->lang->path_expr_of_child) (var);
814
    }
815
}
816
 
817
enum varobj_languages
818
varobj_get_language (struct varobj *var)
819
{
820
  return variable_language (var);
821
}
822
 
823
int
824
varobj_get_attributes (struct varobj *var)
825
{
826
  int attributes = 0;
827
 
828
  if (varobj_editable_p (var))
829
    /* FIXME: define masks for attributes */
830
    attributes |= 0x00000001;   /* Editable */
831
 
832
  return attributes;
833
}
834
 
835
char *
836
varobj_get_value (struct varobj *var)
837
{
838
  return my_value_of_variable (var);
839
}
840
 
841
/* Set the value of an object variable (if it is editable) to the
842
   value of the given expression */
843
/* Note: Invokes functions that can call error() */
844
 
845
int
846
varobj_set_value (struct varobj *var, char *expression)
847
{
848
  struct value *val;
849
  int offset = 0;
850
  int error = 0;
851
 
852
  /* The argument "expression" contains the variable's new value.
853
     We need to first construct a legal expression for this -- ugh! */
854
  /* Does this cover all the bases? */
855
  struct expression *exp;
856
  struct value *value;
857
  int saved_input_radix = input_radix;
858
  char *s = expression;
859
  int i;
860
 
861
  gdb_assert (varobj_editable_p (var));
862
 
863
  input_radix = 10;             /* ALWAYS reset to decimal temporarily */
864
  exp = parse_exp_1 (&s, 0, 0);
865
  if (!gdb_evaluate_expression (exp, &value))
866
    {
867
      /* We cannot proceed without a valid expression. */
868
      xfree (exp);
869
      return 0;
870
    }
871
 
872
  /* All types that are editable must also be changeable.  */
873
  gdb_assert (varobj_value_is_changeable_p (var));
874
 
875
  /* The value of a changeable variable object must not be lazy.  */
876
  gdb_assert (!value_lazy (var->value));
877
 
878
  /* Need to coerce the input.  We want to check if the
879
     value of the variable object will be different
880
     after assignment, and the first thing value_assign
881
     does is coerce the input.
882
     For example, if we are assigning an array to a pointer variable we
883
     should compare the pointer with the the array's address, not with the
884
     array's content.  */
885
  value = coerce_array (value);
886
 
887
  /* The new value may be lazy.  gdb_value_assign, or
888
     rather value_contents, will take care of this.
889
     If fetching of the new value will fail, gdb_value_assign
890
     with catch the exception.  */
891
  if (!gdb_value_assign (var->value, value, &val))
892
    return 0;
893
 
894
  /* If the value has changed, record it, so that next -var-update can
895
     report this change.  If a variable had a value of '1', we've set it
896
     to '333' and then set again to '1', when -var-update will report this
897
     variable as changed -- because the first assignment has set the
898
     'updated' flag.  There's no need to optimize that, because return value
899
     of -var-update should be considered an approximation.  */
900
  var->updated = install_new_value (var, val, 0 /* Compare values. */);
901
  input_radix = saved_input_radix;
902
  return 1;
903
}
904
 
905
/* Returns a malloc'ed list with all root variable objects */
906
int
907
varobj_list (struct varobj ***varlist)
908
{
909
  struct varobj **cv;
910
  struct varobj_root *croot;
911
  int mycount = rootcount;
912
 
913
  /* Alloc (rootcount + 1) entries for the result */
914
  *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
915
 
916
  cv = *varlist;
917
  croot = rootlist;
918
  while ((croot != NULL) && (mycount > 0))
919
    {
920
      *cv = croot->rootvar;
921
      mycount--;
922
      cv++;
923
      croot = croot->next;
924
    }
925
  /* Mark the end of the list */
926
  *cv = NULL;
927
 
928
  if (mycount || (croot != NULL))
929
    warning
930
      ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
931
       rootcount, mycount);
932
 
933
  return rootcount;
934
}
935
 
936
/* Assign a new value to a variable object.  If INITIAL is non-zero,
937
   this is the first assignement after the variable object was just
938
   created, or changed type.  In that case, just assign the value
939
   and return 0.
940
   Otherwise, assign the value and if type_changeable returns non-zero,
941
   find if the new value is different from the current value.
942
   Return 1 if so, and 0 if the values are equal.
943
 
944
   The VALUE parameter should not be released -- the function will
945
   take care of releasing it when needed.  */
946
static int
947
install_new_value (struct varobj *var, struct value *value, int initial)
948
{
949
  int changeable;
950
  int need_to_fetch;
951
  int changed = 0;
952
  int intentionally_not_fetched = 0;
953
  char *print_value = NULL;
954
 
955
  /* We need to know the varobj's type to decide if the value should
956
     be fetched or not.  C++ fake children (public/protected/private) don't have
957
     a type. */
958
  gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
959
  changeable = varobj_value_is_changeable_p (var);
960
  need_to_fetch = changeable;
961
 
962
  /* We are not interested in the address of references, and given
963
     that in C++ a reference is not rebindable, it cannot
964
     meaningfully change.  So, get hold of the real value.  */
965
  if (value)
966
    {
967
      value = coerce_ref (value);
968
      release_value (value);
969
    }
970
 
971
  if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
972
    /* For unions, we need to fetch the value implicitly because
973
       of implementation of union member fetch.  When gdb
974
       creates a value for a field and the value of the enclosing
975
       structure is not lazy,  it immediately copies the necessary
976
       bytes from the enclosing values.  If the enclosing value is
977
       lazy, the call to value_fetch_lazy on the field will read
978
       the data from memory.  For unions, that means we'll read the
979
       same memory more than once, which is not desirable.  So
980
       fetch now.  */
981
    need_to_fetch = 1;
982
 
983
  /* The new value might be lazy.  If the type is changeable,
984
     that is we'll be comparing values of this type, fetch the
985
     value now.  Otherwise, on the next update the old value
986
     will be lazy, which means we've lost that old value.  */
987
  if (need_to_fetch && value && value_lazy (value))
988
    {
989
      struct varobj *parent = var->parent;
990
      int frozen = var->frozen;
991
      for (; !frozen && parent; parent = parent->parent)
992
        frozen |= parent->frozen;
993
 
994
      if (frozen && initial)
995
        {
996
          /* For variables that are frozen, or are children of frozen
997
             variables, we don't do fetch on initial assignment.
998
             For non-initial assignemnt we do the fetch, since it means we're
999
             explicitly asked to compare the new value with the old one.  */
1000
          intentionally_not_fetched = 1;
1001
        }
1002
      else if (!gdb_value_fetch_lazy (value))
1003
        {
1004
          /* Set the value to NULL, so that for the next -var-update,
1005
             we don't try to compare the new value with this value,
1006
             that we couldn't even read.  */
1007
          value = NULL;
1008
        }
1009
    }
1010
 
1011
  /* Below, we'll be comparing string rendering of old and new
1012
     values.  Don't get string rendering if the value is
1013
     lazy -- if it is, the code above has decided that the value
1014
     should not be fetched.  */
1015
  if (value && !value_lazy (value))
1016
      print_value = value_get_print_value (value, var->format);
1017
 
1018
  /* If the type is changeable, compare the old and the new values.
1019
     If this is the initial assignment, we don't have any old value
1020
     to compare with.  */
1021
  if (!initial && changeable)
1022
    {
1023
      /* If the value of the varobj was changed by -var-set-value, then the
1024
         value in the varobj and in the target is the same.  However, that value
1025
         is different from the value that the varobj had after the previous
1026
         -var-update. So need to the varobj as changed.  */
1027
      if (var->updated)
1028
        {
1029
          changed = 1;
1030
        }
1031
      else
1032
        {
1033
          /* Try to compare the values.  That requires that both
1034
             values are non-lazy.  */
1035
          if (var->not_fetched && value_lazy (var->value))
1036
            {
1037
              /* This is a frozen varobj and the value was never read.
1038
                 Presumably, UI shows some "never read" indicator.
1039
                 Now that we've fetched the real value, we need to report
1040
                 this varobj as changed so that UI can show the real
1041
                 value.  */
1042
              changed = 1;
1043
            }
1044
          else  if (var->value == NULL && value == NULL)
1045
            /* Equal. */
1046
            ;
1047
          else if (var->value == NULL || value == NULL)
1048
            {
1049
              changed = 1;
1050
            }
1051
          else
1052
            {
1053
              gdb_assert (!value_lazy (var->value));
1054
              gdb_assert (!value_lazy (value));
1055
 
1056
              gdb_assert (var->print_value != NULL && print_value != NULL);
1057
              if (strcmp (var->print_value, print_value) != 0)
1058
                changed = 1;
1059
            }
1060
        }
1061
    }
1062
 
1063
  /* We must always keep the new value, since children depend on it.  */
1064
  if (var->value != NULL && var->value != value)
1065
    value_free (var->value);
1066
  var->value = value;
1067
  if (var->print_value)
1068
    xfree (var->print_value);
1069
  var->print_value = print_value;
1070
  if (value && value_lazy (value) && intentionally_not_fetched)
1071
    var->not_fetched = 1;
1072
  else
1073
    var->not_fetched = 0;
1074
  var->updated = 0;
1075
 
1076
  gdb_assert (!var->value || value_type (var->value));
1077
 
1078
  return changed;
1079
}
1080
 
1081
/* Update the values for a variable and its children.  This is a
1082
   two-pronged attack.  First, re-parse the value for the root's
1083
   expression to see if it's changed.  Then go all the way
1084
   through its children, reconstructing them and noting if they've
1085
   changed.
1086
   Return value:
1087
    < 0 for error values, see varobj.h.
1088
    Otherwise it is the number of children + parent changed.
1089
 
1090
   The EXPLICIT parameter specifies if this call is result
1091
   of MI request to update this specific variable, or
1092
   result of implicit -var-update *. For implicit request, we don't
1093
   update frozen variables.
1094
 
1095
   NOTE: This function may delete the caller's varobj. If it
1096
   returns TYPE_CHANGED, then it has done this and VARP will be modified
1097
   to point to the new varobj.  */
1098
 
1099
int
1100
varobj_update (struct varobj **varp, struct varobj ***changelist,
1101
               int explicit)
1102
{
1103
  int changed = 0;
1104
  int type_changed = 0;
1105
  int i;
1106
  int vleft;
1107
  struct varobj *v;
1108
  struct varobj **cv;
1109
  struct varobj **templist = NULL;
1110
  struct value *new;
1111
  VEC (varobj_p) *stack = NULL;
1112
  VEC (varobj_p) *result = NULL;
1113
  struct frame_id old_fid;
1114
  struct frame_info *fi;
1115
 
1116
  /* sanity check: have we been passed a pointer?  */
1117
  gdb_assert (changelist);
1118
 
1119
  /* Frozen means frozen -- we don't check for any change in
1120
     this varobj, including its going out of scope, or
1121
     changing type.  One use case for frozen varobjs is
1122
     retaining previously evaluated expressions, and we don't
1123
     want them to be reevaluated at all.  */
1124
  if (!explicit && (*varp)->frozen)
1125
    return 0;
1126
 
1127
  if (!(*varp)->root->is_valid)
1128
    return INVALID;
1129
 
1130
  if ((*varp)->root->rootvar == *varp)
1131
    {
1132
      /* Save the selected stack frame, since we will need to change it
1133
         in order to evaluate expressions.  */
1134
      old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
1135
 
1136
      /* Update the root variable. value_of_root can return NULL
1137
         if the variable is no longer around, i.e. we stepped out of
1138
         the frame in which a local existed. We are letting the
1139
         value_of_root variable dispose of the varobj if the type
1140
         has changed.  */
1141
      type_changed = 1;
1142
      new = value_of_root (varp, &type_changed);
1143
 
1144
      /* Restore selected frame.  */
1145
      fi = frame_find_by_id (old_fid);
1146
      if (fi)
1147
        select_frame (fi);
1148
 
1149
      /* If this is a "use_selected_frame" varobj, and its type has changed,
1150
         them note that it's changed.  */
1151
      if (type_changed)
1152
        VEC_safe_push (varobj_p, result, *varp);
1153
 
1154
        if (install_new_value ((*varp), new, type_changed))
1155
          {
1156
            /* If type_changed is 1, install_new_value will never return
1157
               non-zero, so we'll never report the same variable twice.  */
1158
            gdb_assert (!type_changed);
1159
            VEC_safe_push (varobj_p, result, *varp);
1160
          }
1161
 
1162
      if (new == NULL)
1163
        {
1164
          /* This means the varobj itself is out of scope.
1165
             Report it.  */
1166
          VEC_free (varobj_p, result);
1167
          return NOT_IN_SCOPE;
1168
        }
1169
    }
1170
 
1171
  VEC_safe_push (varobj_p, stack, *varp);
1172
 
1173
  /* Walk through the children, reconstructing them all.  */
1174
  while (!VEC_empty (varobj_p, stack))
1175
    {
1176
      v = VEC_pop (varobj_p, stack);
1177
 
1178
      /* Push any children.  Use reverse order so that the first
1179
         child is popped from the work stack first, and so
1180
         will be added to result first.  This does not
1181
         affect correctness, just "nicer".  */
1182
      for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1183
        {
1184
          varobj_p c = VEC_index (varobj_p, v->children, i);
1185
          /* Child may be NULL if explicitly deleted by -var-delete.  */
1186
          if (c != NULL && !c->frozen)
1187
            VEC_safe_push (varobj_p, stack, c);
1188
        }
1189
 
1190
      /* Update this variable, unless it's a root, which is already
1191
         updated.  */
1192
      if (v->root->rootvar != v)
1193
        {
1194
          new = value_of_child (v->parent, v->index);
1195
          if (install_new_value (v, new, 0 /* type not changed */))
1196
            {
1197
              /* Note that it's changed */
1198
              VEC_safe_push (varobj_p, result, v);
1199
              v->updated = 0;
1200
            }
1201
        }
1202
    }
1203
 
1204
  /* Alloc (changed + 1) list entries.  */
1205
  changed = VEC_length (varobj_p, result);
1206
  *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1207
  cv = *changelist;
1208
 
1209
  for (i = 0; i < changed; ++i)
1210
    {
1211
      *cv = VEC_index (varobj_p, result, i);
1212
      gdb_assert (*cv != NULL);
1213
      ++cv;
1214
    }
1215
  *cv = 0;
1216
 
1217
  VEC_free (varobj_p, stack);
1218
  VEC_free (varobj_p, result);
1219
 
1220
  if (type_changed)
1221
    return TYPE_CHANGED;
1222
  else
1223
    return changed;
1224
}
1225
 
1226
 
1227
/* Helper functions */
1228
 
1229
/*
1230
 * Variable object construction/destruction
1231
 */
1232
 
1233
static int
1234
delete_variable (struct cpstack **resultp, struct varobj *var,
1235
                 int only_children_p)
1236
{
1237
  int delcount = 0;
1238
 
1239
  delete_variable_1 (resultp, &delcount, var,
1240
                     only_children_p, 1 /* remove_from_parent_p */ );
1241
 
1242
  return delcount;
1243
}
1244
 
1245
/* Delete the variable object VAR and its children */
1246
/* IMPORTANT NOTE: If we delete a variable which is a child
1247
   and the parent is not removed we dump core.  It must be always
1248
   initially called with remove_from_parent_p set */
1249
static void
1250
delete_variable_1 (struct cpstack **resultp, int *delcountp,
1251
                   struct varobj *var, int only_children_p,
1252
                   int remove_from_parent_p)
1253
{
1254
  int i;
1255
 
1256
  /* Delete any children of this variable, too. */
1257
  for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1258
    {
1259
      varobj_p child = VEC_index (varobj_p, var->children, i);
1260
      if (!child)
1261
        continue;
1262
      if (!remove_from_parent_p)
1263
        child->parent = NULL;
1264
      delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1265
    }
1266
  VEC_free (varobj_p, var->children);
1267
 
1268
  /* if we were called to delete only the children we are done here */
1269
  if (only_children_p)
1270
    return;
1271
 
1272
  /* Otherwise, add it to the list of deleted ones and proceed to do so */
1273
  /* If the name is null, this is a temporary variable, that has not
1274
     yet been installed, don't report it, it belongs to the caller... */
1275
  if (var->obj_name != NULL)
1276
    {
1277
      cppush (resultp, xstrdup (var->obj_name));
1278
      *delcountp = *delcountp + 1;
1279
    }
1280
 
1281
  /* If this variable has a parent, remove it from its parent's list */
1282
  /* OPTIMIZATION: if the parent of this variable is also being deleted,
1283
     (as indicated by remove_from_parent_p) we don't bother doing an
1284
     expensive list search to find the element to remove when we are
1285
     discarding the list afterwards */
1286
  if ((remove_from_parent_p) && (var->parent != NULL))
1287
    {
1288
      VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1289
    }
1290
 
1291
  if (var->obj_name != NULL)
1292
    uninstall_variable (var);
1293
 
1294
  /* Free memory associated with this variable */
1295
  free_variable (var);
1296
}
1297
 
1298
/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1299
static int
1300
install_variable (struct varobj *var)
1301
{
1302
  struct vlist *cv;
1303
  struct vlist *newvl;
1304
  const char *chp;
1305
  unsigned int index = 0;
1306
  unsigned int i = 1;
1307
 
1308
  for (chp = var->obj_name; *chp; chp++)
1309
    {
1310
      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1311
    }
1312
 
1313
  cv = *(varobj_table + index);
1314
  while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1315
    cv = cv->next;
1316
 
1317
  if (cv != NULL)
1318
    error (_("Duplicate variable object name"));
1319
 
1320
  /* Add varobj to hash table */
1321
  newvl = xmalloc (sizeof (struct vlist));
1322
  newvl->next = *(varobj_table + index);
1323
  newvl->var = var;
1324
  *(varobj_table + index) = newvl;
1325
 
1326
  /* If root, add varobj to root list */
1327
  if (is_root_p (var))
1328
    {
1329
      /* Add to list of root variables */
1330
      if (rootlist == NULL)
1331
        var->root->next = NULL;
1332
      else
1333
        var->root->next = rootlist;
1334
      rootlist = var->root;
1335
      rootcount++;
1336
    }
1337
 
1338
  return 1;                     /* OK */
1339
}
1340
 
1341
/* Unistall the object VAR. */
1342
static void
1343
uninstall_variable (struct varobj *var)
1344
{
1345
  struct vlist *cv;
1346
  struct vlist *prev;
1347
  struct varobj_root *cr;
1348
  struct varobj_root *prer;
1349
  const char *chp;
1350
  unsigned int index = 0;
1351
  unsigned int i = 1;
1352
 
1353
  /* Remove varobj from hash table */
1354
  for (chp = var->obj_name; *chp; chp++)
1355
    {
1356
      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1357
    }
1358
 
1359
  cv = *(varobj_table + index);
1360
  prev = NULL;
1361
  while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1362
    {
1363
      prev = cv;
1364
      cv = cv->next;
1365
    }
1366
 
1367
  if (varobjdebug)
1368
    fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1369
 
1370
  if (cv == NULL)
1371
    {
1372
      warning
1373
        ("Assertion failed: Could not find variable object \"%s\" to delete",
1374
         var->obj_name);
1375
      return;
1376
    }
1377
 
1378
  if (prev == NULL)
1379
    *(varobj_table + index) = cv->next;
1380
  else
1381
    prev->next = cv->next;
1382
 
1383
  xfree (cv);
1384
 
1385
  /* If root, remove varobj from root list */
1386
  if (is_root_p (var))
1387
    {
1388
      /* Remove from list of root variables */
1389
      if (rootlist == var->root)
1390
        rootlist = var->root->next;
1391
      else
1392
        {
1393
          prer = NULL;
1394
          cr = rootlist;
1395
          while ((cr != NULL) && (cr->rootvar != var))
1396
            {
1397
              prer = cr;
1398
              cr = cr->next;
1399
            }
1400
          if (cr == NULL)
1401
            {
1402
              warning
1403
                ("Assertion failed: Could not find varobj \"%s\" in root list",
1404
                 var->obj_name);
1405
              return;
1406
            }
1407
          if (prer == NULL)
1408
            rootlist = NULL;
1409
          else
1410
            prer->next = cr->next;
1411
        }
1412
      rootcount--;
1413
    }
1414
 
1415
}
1416
 
1417
/* Create and install a child of the parent of the given name */
1418
static struct varobj *
1419
create_child (struct varobj *parent, int index, char *name)
1420
{
1421
  struct varobj *child;
1422
  char *childs_name;
1423
  struct value *value;
1424
 
1425
  child = new_variable ();
1426
 
1427
  /* name is allocated by name_of_child */
1428
  child->name = name;
1429
  child->index = index;
1430
  value = value_of_child (parent, index);
1431
  child->parent = parent;
1432
  child->root = parent->root;
1433
  childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1434
  child->obj_name = childs_name;
1435
  install_variable (child);
1436
 
1437
  /* Compute the type of the child.  Must do this before
1438
     calling install_new_value.  */
1439
  if (value != NULL)
1440
    /* If the child had no evaluation errors, var->value
1441
       will be non-NULL and contain a valid type. */
1442
    child->type = value_type (value);
1443
  else
1444
    /* Otherwise, we must compute the type. */
1445
    child->type = (*child->root->lang->type_of_child) (child->parent,
1446
                                                       child->index);
1447
  install_new_value (child, value, 1);
1448
 
1449
  return child;
1450
}
1451
 
1452
 
1453
/*
1454
 * Miscellaneous utility functions.
1455
 */
1456
 
1457
/* Allocate memory and initialize a new variable */
1458
static struct varobj *
1459
new_variable (void)
1460
{
1461
  struct varobj *var;
1462
 
1463
  var = (struct varobj *) xmalloc (sizeof (struct varobj));
1464
  var->name = NULL;
1465
  var->path_expr = NULL;
1466
  var->obj_name = NULL;
1467
  var->index = -1;
1468
  var->type = NULL;
1469
  var->value = NULL;
1470
  var->num_children = -1;
1471
  var->parent = NULL;
1472
  var->children = NULL;
1473
  var->format = 0;
1474
  var->root = NULL;
1475
  var->updated = 0;
1476
  var->print_value = NULL;
1477
  var->frozen = 0;
1478
  var->not_fetched = 0;
1479
 
1480
  return var;
1481
}
1482
 
1483
/* Allocate memory and initialize a new root variable */
1484
static struct varobj *
1485
new_root_variable (void)
1486
{
1487
  struct varobj *var = new_variable ();
1488
  var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1489
  var->root->lang = NULL;
1490
  var->root->exp = NULL;
1491
  var->root->valid_block = NULL;
1492
  var->root->frame = null_frame_id;
1493
  var->root->use_selected_frame = 0;
1494
  var->root->rootvar = NULL;
1495
  var->root->is_valid = 1;
1496
 
1497
  return var;
1498
}
1499
 
1500
/* Free any allocated memory associated with VAR. */
1501
static void
1502
free_variable (struct varobj *var)
1503
{
1504
  /* Free the expression if this is a root variable. */
1505
  if (is_root_p (var))
1506
    {
1507
      free_current_contents (&var->root->exp);
1508
      xfree (var->root);
1509
    }
1510
 
1511
  xfree (var->name);
1512
  xfree (var->obj_name);
1513
  xfree (var->print_value);
1514
  xfree (var->path_expr);
1515
  xfree (var);
1516
}
1517
 
1518
static void
1519
do_free_variable_cleanup (void *var)
1520
{
1521
  free_variable (var);
1522
}
1523
 
1524
static struct cleanup *
1525
make_cleanup_free_variable (struct varobj *var)
1526
{
1527
  return make_cleanup (do_free_variable_cleanup, var);
1528
}
1529
 
1530
/* This returns the type of the variable. It also skips past typedefs
1531
   to return the real type of the variable.
1532
 
1533
   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1534
   except within get_target_type and get_type. */
1535
static struct type *
1536
get_type (struct varobj *var)
1537
{
1538
  struct type *type;
1539
  type = var->type;
1540
 
1541
  if (type != NULL)
1542
    type = check_typedef (type);
1543
 
1544
  return type;
1545
}
1546
 
1547
/* Return the type of the value that's stored in VAR,
1548
   or that would have being stored there if the
1549
   value were accessible.
1550
 
1551
   This differs from VAR->type in that VAR->type is always
1552
   the true type of the expession in the source language.
1553
   The return value of this function is the type we're
1554
   actually storing in varobj, and using for displaying
1555
   the values and for comparing previous and new values.
1556
 
1557
   For example, top-level references are always stripped.  */
1558
static struct type *
1559
get_value_type (struct varobj *var)
1560
{
1561
  struct type *type;
1562
 
1563
  if (var->value)
1564
    type = value_type (var->value);
1565
  else
1566
    type = var->type;
1567
 
1568
  type = check_typedef (type);
1569
 
1570
  if (TYPE_CODE (type) == TYPE_CODE_REF)
1571
    type = get_target_type (type);
1572
 
1573
  type = check_typedef (type);
1574
 
1575
  return type;
1576
}
1577
 
1578
/* This returns the target type (or NULL) of TYPE, also skipping
1579
   past typedefs, just like get_type ().
1580
 
1581
   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1582
   except within get_target_type and get_type. */
1583
static struct type *
1584
get_target_type (struct type *type)
1585
{
1586
  if (type != NULL)
1587
    {
1588
      type = TYPE_TARGET_TYPE (type);
1589
      if (type != NULL)
1590
        type = check_typedef (type);
1591
    }
1592
 
1593
  return type;
1594
}
1595
 
1596
/* What is the default display for this variable? We assume that
1597
   everything is "natural". Any exceptions? */
1598
static enum varobj_display_formats
1599
variable_default_display (struct varobj *var)
1600
{
1601
  return FORMAT_NATURAL;
1602
}
1603
 
1604
/* FIXME: The following should be generic for any pointer */
1605
static void
1606
cppush (struct cpstack **pstack, char *name)
1607
{
1608
  struct cpstack *s;
1609
 
1610
  s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1611
  s->name = name;
1612
  s->next = *pstack;
1613
  *pstack = s;
1614
}
1615
 
1616
/* FIXME: The following should be generic for any pointer */
1617
static char *
1618
cppop (struct cpstack **pstack)
1619
{
1620
  struct cpstack *s;
1621
  char *v;
1622
 
1623
  if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1624
    return NULL;
1625
 
1626
  s = *pstack;
1627
  v = s->name;
1628
  *pstack = (*pstack)->next;
1629
  xfree (s);
1630
 
1631
  return v;
1632
}
1633
 
1634
/*
1635
 * Language-dependencies
1636
 */
1637
 
1638
/* Common entry points */
1639
 
1640
/* Get the language of variable VAR. */
1641
static enum varobj_languages
1642
variable_language (struct varobj *var)
1643
{
1644
  enum varobj_languages lang;
1645
 
1646
  switch (var->root->exp->language_defn->la_language)
1647
    {
1648
    default:
1649
    case language_c:
1650
      lang = vlang_c;
1651
      break;
1652
    case language_cplus:
1653
      lang = vlang_cplus;
1654
      break;
1655
    case language_java:
1656
      lang = vlang_java;
1657
      break;
1658
    }
1659
 
1660
  return lang;
1661
}
1662
 
1663
/* Return the number of children for a given variable.
1664
   The result of this function is defined by the language
1665
   implementation. The number of children returned by this function
1666
   is the number of children that the user will see in the variable
1667
   display. */
1668
static int
1669
number_of_children (struct varobj *var)
1670
{
1671
  return (*var->root->lang->number_of_children) (var);;
1672
}
1673
 
1674
/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1675
static char *
1676
name_of_variable (struct varobj *var)
1677
{
1678
  return (*var->root->lang->name_of_variable) (var);
1679
}
1680
 
1681
/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1682
static char *
1683
name_of_child (struct varobj *var, int index)
1684
{
1685
  return (*var->root->lang->name_of_child) (var, index);
1686
}
1687
 
1688
/* What is the ``struct value *'' of the root variable VAR?
1689
   TYPE_CHANGED controls what to do if the type of a
1690
   use_selected_frame = 1 variable changes.  On input,
1691
   TYPE_CHANGED = 1 means discard the old varobj, and replace
1692
   it with this one.  TYPE_CHANGED = 0 means leave it around.
1693
   NB: In both cases, var_handle will point to the new varobj,
1694
   so if you use TYPE_CHANGED = 0, you will have to stash the
1695
   old varobj pointer away somewhere before calling this.
1696
   On return, TYPE_CHANGED will be 1 if the type has changed, and
1697
 
1698
static struct value *
1699
value_of_root (struct varobj **var_handle, int *type_changed)
1700
{
1701
  struct varobj *var;
1702
 
1703
  if (var_handle == NULL)
1704
    return NULL;
1705
 
1706
  var = *var_handle;
1707
 
1708
  /* This should really be an exception, since this should
1709
     only get called with a root variable. */
1710
 
1711
  if (!is_root_p (var))
1712
    return NULL;
1713
 
1714
  if (var->root->use_selected_frame)
1715
    {
1716
      struct varobj *tmp_var;
1717
      char *old_type, *new_type;
1718
 
1719
      tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1720
                               USE_SELECTED_FRAME);
1721
      if (tmp_var == NULL)
1722
        {
1723
          return NULL;
1724
        }
1725
      old_type = varobj_get_type (var);
1726
      new_type = varobj_get_type (tmp_var);
1727
      if (strcmp (old_type, new_type) == 0)
1728
        {
1729
          varobj_delete (tmp_var, NULL, 0);
1730
          *type_changed = 0;
1731
        }
1732
      else
1733
        {
1734
          if (*type_changed)
1735
            {
1736
              tmp_var->obj_name =
1737
                savestring (var->obj_name, strlen (var->obj_name));
1738
              varobj_delete (var, NULL, 0);
1739
            }
1740
          else
1741
            {
1742
              tmp_var->obj_name = varobj_gen_name ();
1743
            }
1744
          install_variable (tmp_var);
1745
          *var_handle = tmp_var;
1746
          var = *var_handle;
1747
          *type_changed = 1;
1748
        }
1749
      xfree (old_type);
1750
      xfree (new_type);
1751
    }
1752
  else
1753
    {
1754
      *type_changed = 0;
1755
    }
1756
 
1757
  return (*var->root->lang->value_of_root) (var_handle);
1758
}
1759
 
1760
/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1761
static struct value *
1762
value_of_child (struct varobj *parent, int index)
1763
{
1764
  struct value *value;
1765
 
1766
  value = (*parent->root->lang->value_of_child) (parent, index);
1767
 
1768
  return value;
1769
}
1770
 
1771
/* GDB already has a command called "value_of_variable". Sigh. */
1772
static char *
1773
my_value_of_variable (struct varobj *var)
1774
{
1775
  if (var->root->is_valid)
1776
    return (*var->root->lang->value_of_variable) (var);
1777
  else
1778
    return NULL;
1779
}
1780
 
1781
static char *
1782
value_get_print_value (struct value *value, enum varobj_display_formats format)
1783
{
1784
  long dummy;
1785
  struct ui_file *stb;
1786
  struct cleanup *old_chain;
1787
  char *thevalue;
1788
 
1789
  if (value == NULL)
1790
    return NULL;
1791
 
1792
  stb = mem_fileopen ();
1793
  old_chain = make_cleanup_ui_file_delete (stb);
1794
 
1795
  common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1796
  thevalue = ui_file_xstrdup (stb, &dummy);
1797
 
1798
  do_cleanups (old_chain);
1799
  return thevalue;
1800
}
1801
 
1802
int
1803
varobj_editable_p (struct varobj *var)
1804
{
1805
  struct type *type;
1806
  struct value *value;
1807
 
1808
  if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
1809
    return 0;
1810
 
1811
  type = get_value_type (var);
1812
 
1813
  switch (TYPE_CODE (type))
1814
    {
1815
    case TYPE_CODE_STRUCT:
1816
    case TYPE_CODE_UNION:
1817
    case TYPE_CODE_ARRAY:
1818
    case TYPE_CODE_FUNC:
1819
    case TYPE_CODE_METHOD:
1820
      return 0;
1821
      break;
1822
 
1823
    default:
1824
      return 1;
1825
      break;
1826
    }
1827
}
1828
 
1829
/* Return non-zero if changes in value of VAR
1830
   must be detected and reported by -var-update.
1831
   Return zero is -var-update should never report
1832
   changes of such values.  This makes sense for structures
1833
   (since the changes in children values will be reported separately),
1834
   or for artifical objects (like 'public' pseudo-field in C++).
1835
 
1836
   Return value of 0 means that gdb need not call value_fetch_lazy
1837
   for the value of this variable object.  */
1838
static int
1839
varobj_value_is_changeable_p (struct varobj *var)
1840
{
1841
  int r;
1842
  struct type *type;
1843
 
1844
  if (CPLUS_FAKE_CHILD (var))
1845
    return 0;
1846
 
1847
  type = get_value_type (var);
1848
 
1849
  switch (TYPE_CODE (type))
1850
    {
1851
    case TYPE_CODE_STRUCT:
1852
    case TYPE_CODE_UNION:
1853
    case TYPE_CODE_ARRAY:
1854
      r = 0;
1855
      break;
1856
 
1857
    default:
1858
      r = 1;
1859
    }
1860
 
1861
  return r;
1862
}
1863
 
1864
/* Given the value and the type of a variable object,
1865
   adjust the value and type to those necessary
1866
   for getting children of the variable object.
1867
   This includes dereferencing top-level references
1868
   to all types and dereferencing pointers to
1869
   structures.
1870
 
1871
   Both TYPE and *TYPE should be non-null. VALUE
1872
   can be null if we want to only translate type.
1873
   *VALUE can be null as well -- if the parent
1874
   value is not known.
1875
 
1876
   If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1877
   depending on whether pointer was deferenced
1878
   in this function.  */
1879
static void
1880
adjust_value_for_child_access (struct value **value,
1881
                                  struct type **type,
1882
                                  int *was_ptr)
1883
{
1884
  gdb_assert (type && *type);
1885
 
1886
  if (was_ptr)
1887
    *was_ptr = 0;
1888
 
1889
  *type = check_typedef (*type);
1890
 
1891
  /* The type of value stored in varobj, that is passed
1892
     to us, is already supposed to be
1893
     reference-stripped.  */
1894
 
1895
  gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1896
 
1897
  /* Pointers to structures are treated just like
1898
     structures when accessing children.  Don't
1899
     dererences pointers to other types.  */
1900
  if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1901
    {
1902
      struct type *target_type = get_target_type (*type);
1903
      if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1904
          || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1905
        {
1906
          if (value && *value)
1907
            {
1908
              int success = gdb_value_ind (*value, value);
1909
              if (!success)
1910
                *value = NULL;
1911
            }
1912
          *type = target_type;
1913
          if (was_ptr)
1914
            *was_ptr = 1;
1915
        }
1916
    }
1917
 
1918
  /* The 'get_target_type' function calls check_typedef on
1919
     result, so we can immediately check type code.  No
1920
     need to call check_typedef here.  */
1921
}
1922
 
1923
/* C */
1924
static int
1925
c_number_of_children (struct varobj *var)
1926
{
1927
  struct type *type = get_value_type (var);
1928
  int children = 0;
1929
  struct type *target;
1930
 
1931
  adjust_value_for_child_access (NULL, &type, NULL);
1932
  target = get_target_type (type);
1933
 
1934
  switch (TYPE_CODE (type))
1935
    {
1936
    case TYPE_CODE_ARRAY:
1937
      if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1938
          && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1939
        children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1940
      else
1941
        /* If we don't know how many elements there are, don't display
1942
           any.  */
1943
        children = 0;
1944
      break;
1945
 
1946
    case TYPE_CODE_STRUCT:
1947
    case TYPE_CODE_UNION:
1948
      children = TYPE_NFIELDS (type);
1949
      break;
1950
 
1951
    case TYPE_CODE_PTR:
1952
      /* The type here is a pointer to non-struct. Typically, pointers
1953
         have one child, except for function ptrs, which have no children,
1954
         and except for void*, as we don't know what to show.
1955
 
1956
         We can show char* so we allow it to be dereferenced.  If you decide
1957
         to test for it, please mind that a little magic is necessary to
1958
         properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1959
         TYPE_NAME == "char" */
1960
      if (TYPE_CODE (target) == TYPE_CODE_FUNC
1961
          || TYPE_CODE (target) == TYPE_CODE_VOID)
1962
        children = 0;
1963
      else
1964
        children = 1;
1965
      break;
1966
 
1967
    default:
1968
      /* Other types have no children */
1969
      break;
1970
    }
1971
 
1972
  return children;
1973
}
1974
 
1975
static char *
1976
c_name_of_variable (struct varobj *parent)
1977
{
1978
  return savestring (parent->name, strlen (parent->name));
1979
}
1980
 
1981
/* Return the value of element TYPE_INDEX of a structure
1982
   value VALUE.  VALUE's type should be a structure,
1983
   or union, or a typedef to struct/union.
1984
 
1985
   Returns NULL if getting the value fails.  Never throws.  */
1986
static struct value *
1987
value_struct_element_index (struct value *value, int type_index)
1988
{
1989
  struct value *result = NULL;
1990
  volatile struct gdb_exception e;
1991
 
1992
  struct type *type = value_type (value);
1993
  type = check_typedef (type);
1994
 
1995
  gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1996
              || TYPE_CODE (type) == TYPE_CODE_UNION);
1997
 
1998
  TRY_CATCH (e, RETURN_MASK_ERROR)
1999
    {
2000
      if (TYPE_FIELD_STATIC (type, type_index))
2001
        result = value_static_field (type, type_index);
2002
      else
2003
        result = value_primitive_field (value, 0, type_index, type);
2004
    }
2005
  if (e.reason < 0)
2006
    {
2007
      return NULL;
2008
    }
2009
  else
2010
    {
2011
      return result;
2012
    }
2013
}
2014
 
2015
/* Obtain the information about child INDEX of the variable
2016
   object PARENT.
2017
   If CNAME is not null, sets *CNAME to the name of the child relative
2018
   to the parent.
2019
   If CVALUE is not null, sets *CVALUE to the value of the child.
2020
   If CTYPE is not null, sets *CTYPE to the type of the child.
2021
 
2022
   If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2023
   information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2024
   to NULL.  */
2025
static void
2026
c_describe_child (struct varobj *parent, int index,
2027
                  char **cname, struct value **cvalue, struct type **ctype,
2028
                  char **cfull_expression)
2029
{
2030
  struct value *value = parent->value;
2031
  struct type *type = get_value_type (parent);
2032
  char *parent_expression = NULL;
2033
  int was_ptr;
2034
 
2035
  if (cname)
2036
    *cname = NULL;
2037
  if (cvalue)
2038
    *cvalue = NULL;
2039
  if (ctype)
2040
    *ctype = NULL;
2041
  if (cfull_expression)
2042
    {
2043
      *cfull_expression = NULL;
2044
      parent_expression = varobj_get_path_expr (parent);
2045
    }
2046
  adjust_value_for_child_access (&value, &type, &was_ptr);
2047
 
2048
  switch (TYPE_CODE (type))
2049
    {
2050
    case TYPE_CODE_ARRAY:
2051
      if (cname)
2052
        *cname = xstrprintf ("%d", index
2053
                             + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2054
 
2055
      if (cvalue && value)
2056
        {
2057
          int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2058
          struct value *indval =
2059
            value_from_longest (builtin_type_int, (LONGEST) real_index);
2060
          gdb_value_subscript (value, indval, cvalue);
2061
        }
2062
 
2063
      if (ctype)
2064
        *ctype = get_target_type (type);
2065
 
2066
      if (cfull_expression)
2067
        *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression,
2068
                                        index
2069
                                        + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2070
 
2071
 
2072
      break;
2073
 
2074
    case TYPE_CODE_STRUCT:
2075
    case TYPE_CODE_UNION:
2076
      if (cname)
2077
        {
2078
          char *string = TYPE_FIELD_NAME (type, index);
2079
          *cname = savestring (string, strlen (string));
2080
        }
2081
 
2082
      if (cvalue && value)
2083
        {
2084
          /* For C, varobj index is the same as type index.  */
2085
          *cvalue = value_struct_element_index (value, index);
2086
        }
2087
 
2088
      if (ctype)
2089
        *ctype = TYPE_FIELD_TYPE (type, index);
2090
 
2091
      if (cfull_expression)
2092
        {
2093
          char *join = was_ptr ? "->" : ".";
2094
          *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2095
                                          TYPE_FIELD_NAME (type, index));
2096
        }
2097
 
2098
      break;
2099
 
2100
    case TYPE_CODE_PTR:
2101
      if (cname)
2102
        *cname = xstrprintf ("*%s", parent->name);
2103
 
2104
      if (cvalue && value)
2105
        {
2106
          int success = gdb_value_ind (value, cvalue);
2107
          if (!success)
2108
            *cvalue = NULL;
2109
        }
2110
 
2111
      /* Don't use get_target_type because it calls
2112
         check_typedef and here, we want to show the true
2113
         declared type of the variable.  */
2114
      if (ctype)
2115
        *ctype = TYPE_TARGET_TYPE (type);
2116
 
2117
      if (cfull_expression)
2118
        *cfull_expression = xstrprintf ("*(%s)", parent_expression);
2119
 
2120
      break;
2121
 
2122
    default:
2123
      /* This should not happen */
2124
      if (cname)
2125
        *cname = xstrdup ("???");
2126
      if (cfull_expression)
2127
        *cfull_expression = xstrdup ("???");
2128
      /* Don't set value and type, we don't know then. */
2129
    }
2130
}
2131
 
2132
static char *
2133
c_name_of_child (struct varobj *parent, int index)
2134
{
2135
  char *name;
2136
  c_describe_child (parent, index, &name, NULL, NULL, NULL);
2137
  return name;
2138
}
2139
 
2140
static char *
2141
c_path_expr_of_child (struct varobj *child)
2142
{
2143
  c_describe_child (child->parent, child->index, NULL, NULL, NULL,
2144
                    &child->path_expr);
2145
  return child->path_expr;
2146
}
2147
 
2148
static struct value *
2149
c_value_of_root (struct varobj **var_handle)
2150
{
2151
  struct value *new_val = NULL;
2152
  struct varobj *var = *var_handle;
2153
  struct frame_info *fi;
2154
  int within_scope;
2155
 
2156
  /*  Only root variables can be updated... */
2157
  if (!is_root_p (var))
2158
    /* Not a root var */
2159
    return NULL;
2160
 
2161
 
2162
  /* Determine whether the variable is still around. */
2163
  if (var->root->valid_block == NULL || var->root->use_selected_frame)
2164
    within_scope = 1;
2165
  else
2166
    {
2167
      fi = frame_find_by_id (var->root->frame);
2168
      within_scope = fi != NULL;
2169
      /* FIXME: select_frame could fail */
2170
      if (fi)
2171
        {
2172
          CORE_ADDR pc = get_frame_pc (fi);
2173
          if (pc <  BLOCK_START (var->root->valid_block) ||
2174
              pc >= BLOCK_END (var->root->valid_block))
2175
            within_scope = 0;
2176
          else
2177
            select_frame (fi);
2178
        }
2179
    }
2180
 
2181
  if (within_scope)
2182
    {
2183
      /* We need to catch errors here, because if evaluate
2184
         expression fails we want to just return NULL.  */
2185
      gdb_evaluate_expression (var->root->exp, &new_val);
2186
      return new_val;
2187
    }
2188
 
2189
  return NULL;
2190
}
2191
 
2192
static struct value *
2193
c_value_of_child (struct varobj *parent, int index)
2194
{
2195
  struct value *value = NULL;
2196
  c_describe_child (parent, index, NULL, &value, NULL, NULL);
2197
 
2198
  return value;
2199
}
2200
 
2201
static struct type *
2202
c_type_of_child (struct varobj *parent, int index)
2203
{
2204
  struct type *type = NULL;
2205
  c_describe_child (parent, index, NULL, NULL, &type, NULL);
2206
  return type;
2207
}
2208
 
2209
static char *
2210
c_value_of_variable (struct varobj *var)
2211
{
2212
  /* BOGUS: if val_print sees a struct/class, or a reference to one,
2213
     it will print out its children instead of "{...}".  So we need to
2214
     catch that case explicitly.  */
2215
  struct type *type = get_type (var);
2216
 
2217
  /* Strip top-level references. */
2218
  while (TYPE_CODE (type) == TYPE_CODE_REF)
2219
    type = check_typedef (TYPE_TARGET_TYPE (type));
2220
 
2221
  switch (TYPE_CODE (type))
2222
    {
2223
    case TYPE_CODE_STRUCT:
2224
    case TYPE_CODE_UNION:
2225
      return xstrdup ("{...}");
2226
      /* break; */
2227
 
2228
    case TYPE_CODE_ARRAY:
2229
      {
2230
        char *number;
2231
        number = xstrprintf ("[%d]", var->num_children);
2232
        return (number);
2233
      }
2234
      /* break; */
2235
 
2236
    default:
2237
      {
2238
        if (var->value == NULL)
2239
          {
2240
            /* This can happen if we attempt to get the value of a struct
2241
               member when the parent is an invalid pointer. This is an
2242
               error condition, so we should tell the caller. */
2243
            return NULL;
2244
          }
2245
        else
2246
          {
2247
            if (var->not_fetched && value_lazy (var->value))
2248
              /* Frozen variable and no value yet.  We don't
2249
                 implicitly fetch the value.  MI response will
2250
                 use empty string for the value, which is OK.  */
2251
              return NULL;
2252
 
2253
            gdb_assert (varobj_value_is_changeable_p (var));
2254
            gdb_assert (!value_lazy (var->value));
2255
            return xstrdup (var->print_value);
2256
          }
2257
      }
2258
    }
2259
}
2260
 
2261
 
2262
/* C++ */
2263
 
2264
static int
2265
cplus_number_of_children (struct varobj *var)
2266
{
2267
  struct type *type;
2268
  int children, dont_know;
2269
 
2270
  dont_know = 1;
2271
  children = 0;
2272
 
2273
  if (!CPLUS_FAKE_CHILD (var))
2274
    {
2275
      type = get_value_type (var);
2276
      adjust_value_for_child_access (NULL, &type, NULL);
2277
 
2278
      if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2279
          ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2280
        {
2281
          int kids[3];
2282
 
2283
          cplus_class_num_children (type, kids);
2284
          if (kids[v_public] != 0)
2285
            children++;
2286
          if (kids[v_private] != 0)
2287
            children++;
2288
          if (kids[v_protected] != 0)
2289
            children++;
2290
 
2291
          /* Add any baseclasses */
2292
          children += TYPE_N_BASECLASSES (type);
2293
          dont_know = 0;
2294
 
2295
          /* FIXME: save children in var */
2296
        }
2297
    }
2298
  else
2299
    {
2300
      int kids[3];
2301
 
2302
      type = get_value_type (var->parent);
2303
      adjust_value_for_child_access (NULL, &type, NULL);
2304
 
2305
      cplus_class_num_children (type, kids);
2306
      if (strcmp (var->name, "public") == 0)
2307
        children = kids[v_public];
2308
      else if (strcmp (var->name, "private") == 0)
2309
        children = kids[v_private];
2310
      else
2311
        children = kids[v_protected];
2312
      dont_know = 0;
2313
    }
2314
 
2315
  if (dont_know)
2316
    children = c_number_of_children (var);
2317
 
2318
  return children;
2319
}
2320
 
2321
/* Compute # of public, private, and protected variables in this class.
2322
   That means we need to descend into all baseclasses and find out
2323
   how many are there, too. */
2324
static void
2325
cplus_class_num_children (struct type *type, int children[3])
2326
{
2327
  int i;
2328
 
2329
  children[v_public] = 0;
2330
  children[v_private] = 0;
2331
  children[v_protected] = 0;
2332
 
2333
  for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2334
    {
2335
      /* If we have a virtual table pointer, omit it. */
2336
      if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2337
        continue;
2338
 
2339
      if (TYPE_FIELD_PROTECTED (type, i))
2340
        children[v_protected]++;
2341
      else if (TYPE_FIELD_PRIVATE (type, i))
2342
        children[v_private]++;
2343
      else
2344
        children[v_public]++;
2345
    }
2346
}
2347
 
2348
static char *
2349
cplus_name_of_variable (struct varobj *parent)
2350
{
2351
  return c_name_of_variable (parent);
2352
}
2353
 
2354
enum accessibility { private_field, protected_field, public_field };
2355
 
2356
/* Check if field INDEX of TYPE has the specified accessibility.
2357
   Return 0 if so and 1 otherwise.  */
2358
static int
2359
match_accessibility (struct type *type, int index, enum accessibility acc)
2360
{
2361
  if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2362
    return 1;
2363
  else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2364
    return 1;
2365
  else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2366
           && !TYPE_FIELD_PROTECTED (type, index))
2367
    return 1;
2368
  else
2369
    return 0;
2370
}
2371
 
2372
static void
2373
cplus_describe_child (struct varobj *parent, int index,
2374
                      char **cname, struct value **cvalue, struct type **ctype,
2375
                      char **cfull_expression)
2376
{
2377
  char *name = NULL;
2378
  struct value *value;
2379
  struct type *type;
2380
  int was_ptr;
2381
  char *parent_expression = NULL;
2382
 
2383
  if (cname)
2384
    *cname = NULL;
2385
  if (cvalue)
2386
    *cvalue = NULL;
2387
  if (ctype)
2388
    *ctype = NULL;
2389
  if (cfull_expression)
2390
    *cfull_expression = NULL;
2391
 
2392
  if (CPLUS_FAKE_CHILD (parent))
2393
    {
2394
      value = parent->parent->value;
2395
      type = get_value_type (parent->parent);
2396
      if (cfull_expression)
2397
        parent_expression = varobj_get_path_expr (parent->parent);
2398
    }
2399
  else
2400
    {
2401
      value = parent->value;
2402
      type = get_value_type (parent);
2403
      if (cfull_expression)
2404
        parent_expression = varobj_get_path_expr (parent);
2405
    }
2406
 
2407
  adjust_value_for_child_access (&value, &type, &was_ptr);
2408
 
2409
  if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2410
      || TYPE_CODE (type) == TYPE_CODE_UNION)
2411
    {
2412
      char *join = was_ptr ? "->" : ".";
2413
      if (CPLUS_FAKE_CHILD (parent))
2414
        {
2415
          /* The fields of the class type are ordered as they
2416
             appear in the class.  We are given an index for a
2417
             particular access control type ("public","protected",
2418
             or "private").  We must skip over fields that don't
2419
             have the access control we are looking for to properly
2420
             find the indexed field. */
2421
          int type_index = TYPE_N_BASECLASSES (type);
2422
          enum accessibility acc = public_field;
2423
          if (strcmp (parent->name, "private") == 0)
2424
            acc = private_field;
2425
          else if (strcmp (parent->name, "protected") == 0)
2426
            acc = protected_field;
2427
 
2428
          while (index >= 0)
2429
            {
2430
              if (TYPE_VPTR_BASETYPE (type) == type
2431
                  && type_index == TYPE_VPTR_FIELDNO (type))
2432
                ; /* ignore vptr */
2433
              else if (match_accessibility (type, type_index, acc))
2434
                    --index;
2435
                  ++type_index;
2436
            }
2437
          --type_index;
2438
 
2439
          if (cname)
2440
            *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2441
 
2442
          if (cvalue && value)
2443
            *cvalue = value_struct_element_index (value, type_index);
2444
 
2445
          if (ctype)
2446
            *ctype = TYPE_FIELD_TYPE (type, type_index);
2447
 
2448
          if (cfull_expression)
2449
            *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2450
                                            join,
2451
                                            TYPE_FIELD_NAME (type, type_index));
2452
        }
2453
      else if (index < TYPE_N_BASECLASSES (type))
2454
        {
2455
          /* This is a baseclass.  */
2456
          if (cname)
2457
            *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2458
 
2459
          if (cvalue && value)
2460
            {
2461
              *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2462
              release_value (*cvalue);
2463
            }
2464
 
2465
          if (ctype)
2466
            {
2467
              *ctype = TYPE_FIELD_TYPE (type, index);
2468
            }
2469
 
2470
          if (cfull_expression)
2471
            {
2472
              char *ptr = was_ptr ? "*" : "";
2473
              /* Cast the parent to the base' type. Note that in gdb,
2474
                 expression like
2475
                         (Base1)d
2476
                 will create an lvalue, for all appearences, so we don't
2477
                 need to use more fancy:
2478
                         *(Base1*)(&d)
2479
                 construct.  */
2480
              *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
2481
                                              ptr,
2482
                                              TYPE_FIELD_NAME (type, index),
2483
                                              ptr,
2484
                                              parent_expression);
2485
            }
2486
        }
2487
      else
2488
        {
2489
          char *access = NULL;
2490
          int children[3];
2491
          cplus_class_num_children (type, children);
2492
 
2493
          /* Everything beyond the baseclasses can
2494
             only be "public", "private", or "protected"
2495
 
2496
             The special "fake" children are always output by varobj in
2497
             this order. So if INDEX == 2, it MUST be "protected". */
2498
          index -= TYPE_N_BASECLASSES (type);
2499
          switch (index)
2500
            {
2501
            case 0:
2502
              if (children[v_public] > 0)
2503
                access = "public";
2504
              else if (children[v_private] > 0)
2505
                access = "private";
2506
              else
2507
                access = "protected";
2508
              break;
2509
            case 1:
2510
              if (children[v_public] > 0)
2511
                {
2512
                  if (children[v_private] > 0)
2513
                    access = "private";
2514
                  else
2515
                    access = "protected";
2516
                }
2517
              else if (children[v_private] > 0)
2518
                access = "protected";
2519
              break;
2520
            case 2:
2521
              /* Must be protected */
2522
              access = "protected";
2523
              break;
2524
            default:
2525
              /* error! */
2526
              break;
2527
            }
2528
 
2529
          gdb_assert (access);
2530
          if (cname)
2531
            *cname = xstrdup (access);
2532
 
2533
          /* Value and type and full expression are null here.  */
2534
        }
2535
    }
2536
  else
2537
    {
2538
      c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2539
    }
2540
}
2541
 
2542
static char *
2543
cplus_name_of_child (struct varobj *parent, int index)
2544
{
2545
  char *name = NULL;
2546
  cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
2547
  return name;
2548
}
2549
 
2550
static char *
2551
cplus_path_expr_of_child (struct varobj *child)
2552
{
2553
  cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
2554
                        &child->path_expr);
2555
  return child->path_expr;
2556
}
2557
 
2558
static struct value *
2559
cplus_value_of_root (struct varobj **var_handle)
2560
{
2561
  return c_value_of_root (var_handle);
2562
}
2563
 
2564
static struct value *
2565
cplus_value_of_child (struct varobj *parent, int index)
2566
{
2567
  struct value *value = NULL;
2568
  cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
2569
  return value;
2570
}
2571
 
2572
static struct type *
2573
cplus_type_of_child (struct varobj *parent, int index)
2574
{
2575
  struct type *type = NULL;
2576
  cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
2577
  return type;
2578
}
2579
 
2580
static char *
2581
cplus_value_of_variable (struct varobj *var)
2582
{
2583
 
2584
  /* If we have one of our special types, don't print out
2585
     any value. */
2586
  if (CPLUS_FAKE_CHILD (var))
2587
    return xstrdup ("");
2588
 
2589
  return c_value_of_variable (var);
2590
}
2591
 
2592
/* Java */
2593
 
2594
static int
2595
java_number_of_children (struct varobj *var)
2596
{
2597
  return cplus_number_of_children (var);
2598
}
2599
 
2600
static char *
2601
java_name_of_variable (struct varobj *parent)
2602
{
2603
  char *p, *name;
2604
 
2605
  name = cplus_name_of_variable (parent);
2606
  /* If  the name has "-" in it, it is because we
2607
     needed to escape periods in the name... */
2608
  p = name;
2609
 
2610
  while (*p != '\000')
2611
    {
2612
      if (*p == '-')
2613
        *p = '.';
2614
      p++;
2615
    }
2616
 
2617
  return name;
2618
}
2619
 
2620
static char *
2621
java_name_of_child (struct varobj *parent, int index)
2622
{
2623
  char *name, *p;
2624
 
2625
  name = cplus_name_of_child (parent, index);
2626
  /* Escape any periods in the name... */
2627
  p = name;
2628
 
2629
  while (*p != '\000')
2630
    {
2631
      if (*p == '.')
2632
        *p = '-';
2633
      p++;
2634
    }
2635
 
2636
  return name;
2637
}
2638
 
2639
static char *
2640
java_path_expr_of_child (struct varobj *child)
2641
{
2642
  return NULL;
2643
}
2644
 
2645
static struct value *
2646
java_value_of_root (struct varobj **var_handle)
2647
{
2648
  return cplus_value_of_root (var_handle);
2649
}
2650
 
2651
static struct value *
2652
java_value_of_child (struct varobj *parent, int index)
2653
{
2654
  return cplus_value_of_child (parent, index);
2655
}
2656
 
2657
static struct type *
2658
java_type_of_child (struct varobj *parent, int index)
2659
{
2660
  return cplus_type_of_child (parent, index);
2661
}
2662
 
2663
static char *
2664
java_value_of_variable (struct varobj *var)
2665
{
2666
  return cplus_value_of_variable (var);
2667
}
2668
 
2669
extern void _initialize_varobj (void);
2670
void
2671
_initialize_varobj (void)
2672
{
2673
  int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2674
 
2675
  varobj_table = xmalloc (sizeof_table);
2676
  memset (varobj_table, 0, sizeof_table);
2677
 
2678
  add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2679
                            &varobjdebug, _("\
2680
Set varobj debugging."), _("\
2681
Show varobj debugging."), _("\
2682
When non-zero, varobj debugging is enabled."),
2683
                            NULL,
2684
                            show_varobjdebug,
2685
                            &setlist, &showlist);
2686
}
2687
 
2688
/* Invalidate the varobjs that are tied to locals and re-create the ones that
2689
   are defined on globals.
2690
   Invalidated varobjs will be always printed in_scope="invalid".  */
2691
void
2692
varobj_invalidate (void)
2693
{
2694
  struct varobj **all_rootvarobj;
2695
  struct varobj **varp;
2696
 
2697
  if (varobj_list (&all_rootvarobj) > 0)
2698
  {
2699
    varp = all_rootvarobj;
2700
    while (*varp != NULL)
2701
      {
2702
        /* global var must be re-evaluated.  */
2703
        if ((*varp)->root->valid_block == NULL)
2704
        {
2705
          struct varobj *tmp_var;
2706
 
2707
          /* Try to create a varobj with same expression.  If we succeed replace
2708
             the old varobj, otherwise invalidate it.  */
2709
          tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2710
          if (tmp_var != NULL)
2711
            {
2712
              tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2713
              varobj_delete (*varp, NULL, 0);
2714
              install_variable (tmp_var);
2715
            }
2716
          else
2717
              (*varp)->root->is_valid = 0;
2718
        }
2719
        else /* locals must be invalidated.  */
2720
          (*varp)->root->is_valid = 0;
2721
 
2722
        varp++;
2723
      }
2724
    xfree (all_rootvarobj);
2725
  }
2726
  return;
2727
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.