gdb/
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "value.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "regcache.h"
33 #include "objfiles.h"
34 #include "exceptions.h"
35 #include "block.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 static void
46 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
47                          gdb_byte **start, size_t *length);
48
49 /* A helper function for dealing with location lists.  Given a
50    symbol baton (BATON) and a pc value (PC), find the appropriate
51    location expression, set *LOCEXPR_LENGTH, and return a pointer
52    to the beginning of the expression.  Returns NULL on failure.
53
54    For now, only return the first matching location expression; there
55    can be more than one in the list.  */
56
57 static gdb_byte *
58 find_location_expression (struct dwarf2_loclist_baton *baton,
59                           size_t *locexpr_length, CORE_ADDR pc)
60 {
61   CORE_ADDR low, high;
62   gdb_byte *loc_ptr, *buf_end;
63   int length;
64   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
65   struct gdbarch *gdbarch = get_objfile_arch (objfile);
66   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
67   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
68   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
69   /* Adjust base_address for relocatable objects.  */
70   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
71                                     SECT_OFF_TEXT (objfile));
72   CORE_ADDR base_address = baton->base_address + base_offset;
73
74   loc_ptr = baton->data;
75   buf_end = baton->data + baton->size;
76
77   while (1)
78     {
79       if (buf_end - loc_ptr < 2 * addr_size)
80         error (_("find_location_expression: Corrupted DWARF expression."));
81
82       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
83       loc_ptr += addr_size;
84
85       /* A base-address-selection entry.  */
86       if (low == base_mask)
87         {
88           base_address = dwarf2_read_address (gdbarch,
89                                               loc_ptr, buf_end, addr_size);
90           loc_ptr += addr_size;
91           continue;
92         }
93
94       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
95       loc_ptr += addr_size;
96
97       /* An end-of-list entry.  */
98       if (low == 0 && high == 0)
99         return NULL;
100
101       /* Otherwise, a location expression entry.  */
102       low += base_address;
103       high += base_address;
104
105       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
106       loc_ptr += 2;
107
108       if (pc >= low && pc < high)
109         {
110           *locexpr_length = length;
111           return loc_ptr;
112         }
113
114       loc_ptr += length;
115     }
116 }
117
118 /* This is the baton used when performing dwarf2 expression
119    evaluation.  */
120 struct dwarf_expr_baton
121 {
122   struct frame_info *frame;
123   struct objfile *objfile;
124 };
125
126 /* Helper functions for dwarf2_evaluate_loc_desc.  */
127
128 /* Using the frame specified in BATON, return the value of register
129    REGNUM, treated as a pointer.  */
130 static CORE_ADDR
131 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
132 {
133   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
134   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
135   CORE_ADDR result;
136   int regnum;
137
138   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
139   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
140                                   regnum, debaton->frame);
141   return result;
142 }
143
144 /* Read memory at ADDR (length LEN) into BUF.  */
145
146 static void
147 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
148 {
149   read_memory (addr, buf, len);
150 }
151
152 /* Using the frame specified in BATON, find the location expression
153    describing the frame base.  Return a pointer to it in START and
154    its length in LENGTH.  */
155 static void
156 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
157 {
158   /* FIXME: cagney/2003-03-26: This code should be using
159      get_frame_base_address(), and then implement a dwarf2 specific
160      this_base method.  */
161   struct symbol *framefunc;
162   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
163
164   /* Use block_linkage_function, which returns a real (not inlined)
165      function, instead of get_frame_function, which may return an
166      inlined function.  */
167   framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
168
169   /* If we found a frame-relative symbol then it was certainly within
170      some function associated with a frame. If we can't find the frame,
171      something has gone wrong.  */
172   gdb_assert (framefunc != NULL);
173
174   dwarf_expr_frame_base_1 (framefunc,
175                            get_frame_address_in_block (debaton->frame),
176                            start, length);
177 }
178
179 static void
180 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
181                          gdb_byte **start, size_t *length)
182 {
183   if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
184     *start = NULL;
185   else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
186     {
187       struct dwarf2_loclist_baton *symbaton;
188
189       symbaton = SYMBOL_LOCATION_BATON (framefunc);
190       *start = find_location_expression (symbaton, length, pc);
191     }
192   else
193     {
194       struct dwarf2_locexpr_baton *symbaton;
195       symbaton = SYMBOL_LOCATION_BATON (framefunc);
196       if (symbaton != NULL)
197         {
198           *length = symbaton->size;
199           *start = symbaton->data;
200         }
201       else
202         *start = NULL;
203     }
204
205   if (*start == NULL)
206     error (_("Could not find the frame base for \"%s\"."),
207            SYMBOL_NATURAL_NAME (framefunc));
208 }
209
210 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
211    the frame in BATON.  */
212
213 static CORE_ADDR
214 dwarf_expr_frame_cfa (void *baton)
215 {
216   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
217   return dwarf2_frame_cfa (debaton->frame);
218 }
219
220 /* Using the objfile specified in BATON, find the address for the
221    current thread's thread-local storage with offset OFFSET.  */
222 static CORE_ADDR
223 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
224 {
225   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
226
227   return target_translate_tls_address (debaton->objfile, offset);
228 }
229
230 struct piece_closure
231 {
232   /* The number of pieces used to describe this variable.  */
233   int n_pieces;
234
235   /* The target address size, used only for DWARF_VALUE_STACK.  */
236   int addr_size;
237
238   /* The pieces themselves.  */
239   struct dwarf_expr_piece *pieces;
240 };
241
242 /* Allocate a closure for a value formed from separately-described
243    PIECES.  */
244
245 static struct piece_closure *
246 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
247                         int addr_size)
248 {
249   struct piece_closure *c = XZALLOC (struct piece_closure);
250
251   c->n_pieces = n_pieces;
252   c->addr_size = addr_size;
253   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
254
255   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
256
257   return c;
258 }
259
260 static void
261 read_pieced_value (struct value *v)
262 {
263   int i;
264   long offset = 0;
265   gdb_byte *contents;
266   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
267   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
268
269   contents = value_contents_raw (v);
270   for (i = 0; i < c->n_pieces; i++)
271     {
272       struct dwarf_expr_piece *p = &c->pieces[i];
273       switch (p->location)
274         {
275         case DWARF_VALUE_REGISTER:
276           {
277             struct gdbarch *arch = get_frame_arch (frame);
278             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
279                                                            p->v.expr.value);
280             int reg_offset = 0;
281
282             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
283                 && p->size < register_size (arch, gdb_regnum))
284               /* Big-endian, and we want less than full size.  */
285               reg_offset = register_size (arch, gdb_regnum) - p->size;
286
287             if (gdb_regnum != -1)
288               {
289                 get_frame_register_bytes (frame, gdb_regnum, reg_offset, 
290                                           p->size, contents + offset);
291               }
292             else
293               {
294                 error (_("Unable to access DWARF register number %s"),
295                        paddress (arch, p->v.expr.value));
296               }
297           }
298           break;
299
300         case DWARF_VALUE_MEMORY:
301           if (p->v.expr.in_stack_memory)
302             read_stack (p->v.expr.value, contents + offset, p->size);
303           else
304             read_memory (p->v.expr.value, contents + offset, p->size);
305           break;
306
307         case DWARF_VALUE_STACK:
308           {
309             struct gdbarch *gdbarch = get_type_arch (value_type (v));
310             size_t n = p->size;
311             if (n > c->addr_size)
312               n = c->addr_size;
313             store_unsigned_integer (contents + offset, n,
314                                     gdbarch_byte_order (gdbarch),
315                                     p->v.expr.value);
316           }
317           break;
318
319         case DWARF_VALUE_LITERAL:
320           {
321             size_t n = p->size;
322             if (n > p->v.literal.length)
323               n = p->v.literal.length;
324             memcpy (contents + offset, p->v.literal.data, n);
325           }
326           break;
327
328         default:
329           internal_error (__FILE__, __LINE__, _("invalid location type"));
330         }
331       offset += p->size;
332     }
333 }
334
335 static void
336 write_pieced_value (struct value *to, struct value *from)
337 {
338   int i;
339   long offset = 0;
340   gdb_byte *contents;
341   struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
342   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
343
344   if (frame == NULL)
345     {
346       set_value_optimized_out (to, 1);
347       return;
348     }
349
350   contents = value_contents_raw (from);
351   for (i = 0; i < c->n_pieces; i++)
352     {
353       struct dwarf_expr_piece *p = &c->pieces[i];
354       switch (p->location)
355         {
356         case DWARF_VALUE_REGISTER:
357           {
358             struct gdbarch *arch = get_frame_arch (frame);
359             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
360             int reg_offset = 0;
361
362             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
363                 && p->size < register_size (arch, gdb_regnum))
364               /* Big-endian, and we want less than full size.  */
365               reg_offset = register_size (arch, gdb_regnum) - p->size;
366
367             if (gdb_regnum != -1)
368               {
369                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
370                                           p->size, contents + offset);
371               }
372             else
373               {
374                 error (_("Unable to write to DWARF register number %s"),
375                        paddress (arch, p->v.expr.value));
376               }
377           }
378           break;
379         case DWARF_VALUE_MEMORY:
380           write_memory (p->v.expr.value, contents + offset, p->size);
381           break;
382         default:
383           set_value_optimized_out (to, 1);
384           return;
385         }
386       offset += p->size;
387     }
388 }
389
390 static void *
391 copy_pieced_value_closure (struct value *v)
392 {
393   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
394   
395   return allocate_piece_closure (c->n_pieces, c->pieces, c->addr_size);
396 }
397
398 static void
399 free_pieced_value_closure (struct value *v)
400 {
401   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
402
403   xfree (c->pieces);
404   xfree (c);
405 }
406
407 /* Functions for accessing a variable described by DW_OP_piece.  */
408 static struct lval_funcs pieced_value_funcs = {
409   read_pieced_value,
410   write_pieced_value,
411   copy_pieced_value_closure,
412   free_pieced_value_closure
413 };
414
415 /* Evaluate a location description, starting at DATA and with length
416    SIZE, to find the current location of variable of TYPE in the context
417    of FRAME.  */
418
419 static struct value *
420 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
421                           gdb_byte *data, unsigned short size,
422                           struct dwarf2_per_cu_data *per_cu)
423 {
424   struct value *retval;
425   struct dwarf_expr_baton baton;
426   struct dwarf_expr_context *ctx;
427   struct cleanup *old_chain;
428
429   if (size == 0)
430     {
431       retval = allocate_value (type);
432       VALUE_LVAL (retval) = not_lval;
433       set_value_optimized_out (retval, 1);
434       return retval;
435     }
436
437   baton.frame = frame;
438   baton.objfile = dwarf2_per_cu_objfile (per_cu);
439
440   ctx = new_dwarf_expr_context ();
441   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
442
443   ctx->gdbarch = get_objfile_arch (baton.objfile);
444   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
445   ctx->baton = &baton;
446   ctx->read_reg = dwarf_expr_read_reg;
447   ctx->read_mem = dwarf_expr_read_mem;
448   ctx->get_frame_base = dwarf_expr_frame_base;
449   ctx->get_frame_cfa = dwarf_expr_frame_cfa;
450   ctx->get_tls_address = dwarf_expr_tls_address;
451
452   dwarf_expr_eval (ctx, data, size);
453   if (ctx->num_pieces > 0)
454     {
455       struct piece_closure *c;
456       struct frame_id frame_id = get_frame_id (frame);
457
458       c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
459                                   ctx->addr_size);
460       retval = allocate_computed_value (type, &pieced_value_funcs, c);
461       VALUE_FRAME_ID (retval) = frame_id;
462     }
463   else
464     {
465       switch (ctx->location)
466         {
467         case DWARF_VALUE_REGISTER:
468           {
469             struct gdbarch *arch = get_frame_arch (frame);
470             CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
471             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
472             if (gdb_regnum != -1)
473               retval = value_from_register (type, gdb_regnum, frame);
474             else
475               error (_("Unable to access DWARF register number %s"),
476                      paddress (arch, dwarf_regnum));
477           }
478           break;
479
480         case DWARF_VALUE_MEMORY:
481           {
482             CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
483             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
484
485             retval = allocate_value (type);
486             VALUE_LVAL (retval) = lval_memory;
487             set_value_lazy (retval, 1);
488             if (in_stack_memory)
489               set_value_stack (retval, 1);
490             set_value_address (retval, address);
491           }
492           break;
493
494         case DWARF_VALUE_STACK:
495           {
496             ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
497             bfd_byte *contents;
498             size_t n = ctx->addr_size;
499
500             retval = allocate_value (type);
501             contents = value_contents_raw (retval);
502             if (n > TYPE_LENGTH (type))
503               n = TYPE_LENGTH (type);
504             store_unsigned_integer (contents, n,
505                                     gdbarch_byte_order (ctx->gdbarch),
506                                     value);
507           }
508           break;
509
510         case DWARF_VALUE_LITERAL:
511           {
512             bfd_byte *contents;
513             size_t n = ctx->len;
514
515             retval = allocate_value (type);
516             contents = value_contents_raw (retval);
517             if (n > TYPE_LENGTH (type))
518               n = TYPE_LENGTH (type);
519             memcpy (contents, ctx->data, n);
520           }
521           break;
522
523         default:
524           internal_error (__FILE__, __LINE__, _("invalid location type"));
525         }
526     }
527
528   set_value_initialized (retval, ctx->initialized);
529
530   do_cleanups (old_chain);
531
532   return retval;
533 }
534 \f
535 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
536
537 struct needs_frame_baton
538 {
539   int needs_frame;
540 };
541
542 /* Reads from registers do require a frame.  */
543 static CORE_ADDR
544 needs_frame_read_reg (void *baton, int regnum)
545 {
546   struct needs_frame_baton *nf_baton = baton;
547   nf_baton->needs_frame = 1;
548   return 1;
549 }
550
551 /* Reads from memory do not require a frame.  */
552 static void
553 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
554 {
555   memset (buf, 0, len);
556 }
557
558 /* Frame-relative accesses do require a frame.  */
559 static void
560 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
561 {
562   static gdb_byte lit0 = DW_OP_lit0;
563   struct needs_frame_baton *nf_baton = baton;
564
565   *start = &lit0;
566   *length = 1;
567
568   nf_baton->needs_frame = 1;
569 }
570
571 /* CFA accesses require a frame.  */
572
573 static CORE_ADDR
574 needs_frame_frame_cfa (void *baton)
575 {
576   struct needs_frame_baton *nf_baton = baton;
577   nf_baton->needs_frame = 1;
578   return 1;
579 }
580
581 /* Thread-local accesses do require a frame.  */
582 static CORE_ADDR
583 needs_frame_tls_address (void *baton, CORE_ADDR offset)
584 {
585   struct needs_frame_baton *nf_baton = baton;
586   nf_baton->needs_frame = 1;
587   return 1;
588 }
589
590 /* Return non-zero iff the location expression at DATA (length SIZE)
591    requires a frame to evaluate.  */
592
593 static int
594 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
595                              struct dwarf2_per_cu_data *per_cu)
596 {
597   struct needs_frame_baton baton;
598   struct dwarf_expr_context *ctx;
599   int in_reg;
600   struct cleanup *old_chain;
601
602   baton.needs_frame = 0;
603
604   ctx = new_dwarf_expr_context ();
605   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
606
607   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
608   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
609   ctx->baton = &baton;
610   ctx->read_reg = needs_frame_read_reg;
611   ctx->read_mem = needs_frame_read_mem;
612   ctx->get_frame_base = needs_frame_frame_base;
613   ctx->get_frame_cfa = needs_frame_frame_cfa;
614   ctx->get_tls_address = needs_frame_tls_address;
615
616   dwarf_expr_eval (ctx, data, size);
617
618   in_reg = ctx->location == DWARF_VALUE_REGISTER;
619
620   if (ctx->num_pieces > 0)
621     {
622       int i;
623
624       /* If the location has several pieces, and any of them are in
625          registers, then we will need a frame to fetch them from.  */
626       for (i = 0; i < ctx->num_pieces; i++)
627         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
628           in_reg = 1;
629     }
630
631   do_cleanups (old_chain);
632
633   return baton.needs_frame || in_reg;
634 }
635
636 /* This struct keeps track of the pieces that make up a multi-location
637    object, for use in agent expression generation.  It is
638    superficially similar to struct dwarf_expr_piece, but
639    dwarf_expr_piece is designed for use in immediate evaluation, and
640    does not, for example, have a way to record both base register and
641    offset.  */
642
643 struct axs_var_loc
644 {
645   /* Memory vs register, etc */
646   enum axs_lvalue_kind kind;
647
648   /* If non-zero, number of bytes in this fragment */
649   unsigned bytes;
650
651   /* (GDB-numbered) reg, or base reg if >= 0 */
652   int reg;
653
654   /* offset from reg */
655   LONGEST offset;
656 };
657
658 static gdb_byte *
659 dwarf2_tracepoint_var_loc (struct symbol *symbol,
660                            struct agent_expr *ax,
661                            struct axs_var_loc *loc,
662                            struct gdbarch *gdbarch,
663                            gdb_byte *data, gdb_byte *end)
664 {
665   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
666     {
667       loc->kind = axs_lvalue_register;
668       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
669       data += 1;
670     }
671   else if (data[0] == DW_OP_regx)
672     {
673       ULONGEST reg;
674       data = read_uleb128 (data + 1, end, &reg);
675       loc->kind = axs_lvalue_register;
676       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
677     }
678   else if (data[0] == DW_OP_fbreg)
679     {
680       struct block *b;
681       struct symbol *framefunc;
682       int frame_reg = 0;
683       LONGEST frame_offset;
684       gdb_byte *base_data;
685       size_t base_size;
686       LONGEST base_offset = 0;
687
688       b = block_for_pc (ax->scope);
689
690       if (!b)
691         error (_("No block found for address"));
692
693       framefunc = block_linkage_function (b);
694
695       if (!framefunc)
696         error (_("No function found for block"));
697
698       dwarf_expr_frame_base_1 (framefunc, ax->scope,
699                                &base_data, &base_size);
700
701       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
702         {
703           gdb_byte *buf_end;
704           
705           frame_reg = base_data[0] - DW_OP_breg0;
706           buf_end = read_sleb128 (base_data + 1,
707                                   base_data + base_size, &base_offset);
708           if (buf_end != base_data + base_size)
709             error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
710                    frame_reg, SYMBOL_PRINT_NAME (symbol));
711         }
712       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
713         {
714           /* The frame base is just the register, with no offset.  */
715           frame_reg = base_data[0] - DW_OP_reg0;
716           base_offset = 0;
717         }
718       else
719         {
720           /* We don't know what to do with the frame base expression,
721              so we can't trace this variable; give up.  */
722           error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
723                  SYMBOL_PRINT_NAME (symbol), base_data[0]);
724         }
725
726       data = read_sleb128 (data + 1, end, &frame_offset);
727
728       loc->kind = axs_lvalue_memory;
729       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
730       loc->offset = base_offset + frame_offset;
731     }
732   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
733     {
734       unsigned int reg;
735       LONGEST offset;
736
737       reg = data[0] - DW_OP_breg0;
738       data = read_sleb128 (data + 1, end, &offset);
739
740       loc->kind = axs_lvalue_memory;
741       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
742       loc->offset = offset;
743     }
744   else
745     error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
746            data[0], SYMBOL_PRINT_NAME (symbol));
747   
748   return data;
749 }
750
751 /* Given the location of a piece, issue bytecodes that will access it.  */
752
753 static void
754 dwarf2_tracepoint_var_access (struct agent_expr *ax,
755                               struct axs_value *value,
756                               struct axs_var_loc *loc)
757 {
758   value->kind = loc->kind;
759   
760   switch (loc->kind)
761     {
762     case axs_lvalue_register:
763       value->u.reg = loc->reg;
764       break;
765       
766     case axs_lvalue_memory:
767       ax_reg (ax, loc->reg);
768       if (loc->offset)
769         {
770           ax_const_l (ax, loc->offset);
771           ax_simple (ax, aop_add);
772         }
773       break;
774       
775     default:
776       internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
777     }
778 }
779
780 static void
781 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
782                            struct agent_expr *ax, struct axs_value *value,
783                            gdb_byte *data, int size)
784 {
785   gdb_byte *end = data + size;
786   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
787   /* In practice, a variable is not going to be spread across
788      dozens of registers or memory locations.  If someone comes up
789      with a real-world example, revisit this.  */
790 #define MAX_FRAGS 16
791   struct axs_var_loc fragments[MAX_FRAGS];
792   int nfrags = 0, frag;
793   int length = 0;
794   int piece_ok = 0;
795   int bad = 0;
796   int first = 1;
797       
798   if (!data || size == 0)
799     {
800       value->optimized_out = 1;
801       return;
802     }
803
804   while (data < end)
805     {
806       if (!piece_ok)
807         {
808           if (nfrags == MAX_FRAGS)
809             error (_("Too many pieces in location for \"%s\"."),
810                    SYMBOL_PRINT_NAME (symbol));
811
812           fragments[nfrags].bytes = 0;
813           data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
814                                             gdbarch, data, end);
815           nfrags++;
816           piece_ok = 1;
817         }
818       else if (data[0] == DW_OP_piece)
819         {
820           ULONGEST bytes;
821               
822           data = read_uleb128 (data + 1, end, &bytes);
823           /* Only deal with 4 byte fragments for now.  */
824           if (bytes != 4)
825             error (_("DW_OP_piece %s not supported in location for \"%s\"."),
826                    pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
827           fragments[nfrags - 1].bytes = bytes;
828           length += bytes;
829           piece_ok = 0;
830         }
831       else
832         {
833           bad = 1;
834           break;
835         }
836     }
837
838   if (bad || data > end)
839     error (_("Corrupted DWARF expression for \"%s\"."),
840            SYMBOL_PRINT_NAME (symbol));
841
842   /* If single expression, no pieces, convert to external format.  */
843   if (length == 0)
844     {
845       dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
846       return;
847     }
848
849   if (length != TYPE_LENGTH (value->type))
850     error (_("Inconsistent piece information for \"%s\"."),
851            SYMBOL_PRINT_NAME (symbol));
852
853   /* Emit bytecodes to assemble the pieces into a single stack entry.  */
854
855   for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
856        nfrags--;
857        (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
858     {
859       if (!first)
860         {
861           /* shift the previous fragment up 32 bits */
862           ax_const_l (ax, 32);
863           ax_simple (ax, aop_lsh);
864         }
865
866       dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
867
868       switch (value->kind)
869         {
870         case axs_lvalue_register:
871           ax_reg (ax, value->u.reg);
872           break;
873
874         case axs_lvalue_memory:
875           {
876             extern int trace_kludge;  /* Ugh. */
877
878             gdb_assert (fragments[frag].bytes == 4);
879             if (trace_kludge)
880               ax_trace_quick (ax, 4);
881             ax_simple (ax, aop_ref32);
882           }
883           break;
884         }
885
886       if (!first)
887         {
888           /* or the new fragment into the previous */
889           ax_zero_ext (ax, 32);
890           ax_simple (ax, aop_bit_or);
891         }
892       first = 0;
893     }
894   value->kind = axs_rvalue;
895 }
896
897 \f
898 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
899    evaluator to calculate the location.  */
900 static struct value *
901 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
902 {
903   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
904   struct value *val;
905   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
906                                   dlbaton->size, dlbaton->per_cu);
907
908   return val;
909 }
910
911 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
912 static int
913 locexpr_read_needs_frame (struct symbol *symbol)
914 {
915   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
916   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
917                                       dlbaton->per_cu);
918 }
919
920 /* Describe a single piece of a location, returning an updated
921    position in the bytecode sequence.  */
922
923 static gdb_byte *
924 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
925                                  CORE_ADDR addr, struct objfile *objfile,
926                                  gdb_byte *data, int size, unsigned int addr_size)
927 {
928   struct gdbarch *gdbarch = get_objfile_arch (objfile);
929   int regno;
930
931   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
932     {
933       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
934       fprintf_filtered (stream, _("a variable in $%s"),
935                         gdbarch_register_name (gdbarch, regno));
936       data += 1;
937     }
938   else if (data[0] == DW_OP_regx)
939     {
940       ULONGEST reg;
941
942       data = read_uleb128 (data + 1, data + size, &reg);
943       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
944       fprintf_filtered (stream, _("a variable in $%s"),
945                         gdbarch_register_name (gdbarch, regno));
946     }
947   else if (data[0] == DW_OP_fbreg)
948     {
949       struct block *b;
950       struct symbol *framefunc;
951       int frame_reg = 0;
952       LONGEST frame_offset;
953       gdb_byte *base_data;
954       size_t base_size;
955       LONGEST base_offset = 0;
956
957       b = block_for_pc (addr);
958
959       if (!b)
960         error (_("No block found for address for symbol \"%s\"."),
961                SYMBOL_PRINT_NAME (symbol));
962
963       framefunc = block_linkage_function (b);
964
965       if (!framefunc)
966         error (_("No function found for block for symbol \"%s\"."),
967                SYMBOL_PRINT_NAME (symbol));
968
969       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
970
971       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
972         {
973           gdb_byte *buf_end;
974           
975           frame_reg = base_data[0] - DW_OP_breg0;
976           buf_end = read_sleb128 (base_data + 1,
977                                   base_data + base_size, &base_offset);
978           if (buf_end != base_data + base_size)
979             error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
980                    frame_reg, SYMBOL_PRINT_NAME (symbol));
981         }
982       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
983         {
984           /* The frame base is just the register, with no offset.  */
985           frame_reg = base_data[0] - DW_OP_reg0;
986           base_offset = 0;
987         }
988       else
989         {
990           /* We don't know what to do with the frame base expression,
991              so we can't trace this variable; give up.  */
992           error (_("Cannot describe location of symbol \"%s\"; "
993                    "DWARF 2 encoding not handled, "
994                    "first opcode in base data is 0x%x."),
995                  SYMBOL_PRINT_NAME (symbol), base_data[0]);
996         }
997
998       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
999
1000       data = read_sleb128 (data + 1, data + size, &frame_offset);
1001
1002       fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1003                         gdbarch_register_name (gdbarch, regno),
1004                         plongest (base_offset), plongest (frame_offset));
1005     }
1006   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1007     {
1008       LONGEST offset;
1009
1010       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1011
1012       data = read_sleb128 (data + 1, data + size, &offset);
1013
1014       fprintf_filtered (stream,
1015                         _("a variable at offset %s from base reg $%s"),
1016                         plongest (offset),
1017                         gdbarch_register_name (gdbarch, regno));
1018     }
1019
1020   /* The location expression for a TLS variable looks like this (on a
1021      64-bit LE machine):
1022
1023      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1024                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1025      
1026      0x3 is the encoding for DW_OP_addr, which has an operand as long
1027      as the size of an address on the target machine (here is 8
1028      bytes).  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1029      The operand represents the offset at which the variable is within
1030      the thread local storage.  */
1031
1032   else if (size > 1
1033            && data[size - 1] == DW_OP_GNU_push_tls_address
1034            && data[0] == DW_OP_addr)
1035     {
1036       CORE_ADDR offset = dwarf2_read_address (gdbarch,
1037                                               data + 1,
1038                                               data + size - 1,
1039                                               addr_size);
1040       fprintf_filtered (stream, 
1041                         _("a thread-local variable at offset %s "
1042                           "in the thread-local storage for `%s'"),
1043                         paddress (gdbarch, offset), objfile->name);
1044
1045       data += 1 + addr_size + 1;
1046     }
1047   else
1048     fprintf_filtered (stream,
1049                       _("a variable with complex or multiple locations (DWARF2)"));
1050
1051   return data;
1052 }
1053
1054 /* Describe a single location, which may in turn consist of multiple
1055    pieces.  */
1056
1057 static void
1058 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1059                              struct ui_file *stream, gdb_byte *data, int size,
1060                              struct objfile *objfile, unsigned int addr_size)
1061 {
1062   gdb_byte *end = data + size;
1063   int piece_done = 0, first_piece = 1, bad = 0;
1064
1065   /* A multi-piece description consists of multiple sequences of bytes
1066      each followed by DW_OP_piece + length of piece.  */
1067   while (data < end)
1068     {
1069       if (!piece_done)
1070         {
1071           if (first_piece)
1072             first_piece = 0;
1073           else
1074             fprintf_filtered (stream, _(", and "));
1075
1076           data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1077                                                   data, size, addr_size);
1078           piece_done = 1;
1079         }
1080       else if (data[0] == DW_OP_piece)
1081         {
1082           ULONGEST bytes;
1083               
1084           data = read_uleb128 (data + 1, end, &bytes);
1085
1086           fprintf_filtered (stream, _(" [%s-byte piece]"), pulongest (bytes));
1087
1088           piece_done = 0;
1089         }
1090       else
1091         {
1092           bad = 1;
1093           break;
1094         }
1095     }
1096
1097   if (bad || data > end)
1098     error (_("Corrupted DWARF2 expression for \"%s\"."),
1099            SYMBOL_PRINT_NAME (symbol));
1100 }
1101
1102 /* Print a natural-language description of SYMBOL to STREAM.  This
1103    version is for a symbol with a single location.  */
1104
1105 static void
1106 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1107                            struct ui_file *stream)
1108 {
1109   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1110   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1111   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1112
1113   locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1114                                objfile, addr_size);
1115 }
1116
1117 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1118    any necessary bytecode in AX.  */
1119
1120 static void
1121 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1122                             struct agent_expr *ax, struct axs_value *value)
1123 {
1124   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1125
1126   dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1127                              dlbaton->data, dlbaton->size);
1128 }
1129
1130 /* The set of location functions used with the DWARF-2 expression
1131    evaluator.  */
1132 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
1133   locexpr_read_variable,
1134   locexpr_read_needs_frame,
1135   locexpr_describe_location,
1136   locexpr_tracepoint_var_ref
1137 };
1138
1139
1140 /* Wrapper functions for location lists.  These generally find
1141    the appropriate location expression and call something above.  */
1142
1143 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1144    evaluator to calculate the location.  */
1145 static struct value *
1146 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1147 {
1148   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1149   struct value *val;
1150   gdb_byte *data;
1151   size_t size;
1152
1153   data = find_location_expression (dlbaton, &size,
1154                                    frame ? get_frame_address_in_block (frame)
1155                                    : 0);
1156   if (data == NULL)
1157     {
1158       val = allocate_value (SYMBOL_TYPE (symbol));
1159       VALUE_LVAL (val) = not_lval;
1160       set_value_optimized_out (val, 1);
1161     }
1162   else
1163     val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
1164                                     dlbaton->per_cu);
1165
1166   return val;
1167 }
1168
1169 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
1170 static int
1171 loclist_read_needs_frame (struct symbol *symbol)
1172 {
1173   /* If there's a location list, then assume we need to have a frame
1174      to choose the appropriate location expression.  With tracking of
1175      global variables this is not necessarily true, but such tracking
1176      is disabled in GCC at the moment until we figure out how to
1177      represent it.  */
1178
1179   return 1;
1180 }
1181
1182 /* Print a natural-language description of SYMBOL to STREAM.  This
1183    version applies when there is a list of different locations, each
1184    with a specified address range.  */
1185
1186 static void
1187 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1188                            struct ui_file *stream)
1189 {
1190   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1191   CORE_ADDR low, high;
1192   gdb_byte *loc_ptr, *buf_end;
1193   int length, first = 1;
1194   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1195   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1196   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1197   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1198   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1199   /* Adjust base_address for relocatable objects.  */
1200   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1201                                     SECT_OFF_TEXT (objfile));
1202   CORE_ADDR base_address = dlbaton->base_address + base_offset;
1203
1204   loc_ptr = dlbaton->data;
1205   buf_end = dlbaton->data + dlbaton->size;
1206
1207   fprintf_filtered (stream, _("multi-location ("));
1208
1209   /* Iterate through locations until we run out.  */
1210   while (1)
1211     {
1212       if (buf_end - loc_ptr < 2 * addr_size)
1213         error (_("Corrupted DWARF expression for symbol \"%s\"."),
1214                SYMBOL_PRINT_NAME (symbol));
1215
1216       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1217       loc_ptr += addr_size;
1218
1219       /* A base-address-selection entry.  */
1220       if (low == base_mask)
1221         {
1222           base_address = dwarf2_read_address (gdbarch,
1223                                               loc_ptr, buf_end, addr_size);
1224           fprintf_filtered (stream, _("[base address %s]"),
1225                             paddress (gdbarch, base_address));
1226           loc_ptr += addr_size;
1227           continue;
1228         }
1229
1230       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1231       loc_ptr += addr_size;
1232
1233       /* An end-of-list entry.  */
1234       if (low == 0 && high == 0)
1235         {
1236           /* Indicate the end of the list, for readability.  */
1237           fprintf_filtered (stream, _(")"));
1238           return;
1239         }
1240
1241       /* Otherwise, a location expression entry.  */
1242       low += base_address;
1243       high += base_address;
1244
1245       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1246       loc_ptr += 2;
1247
1248       /* Separate the different locations with a semicolon.  */
1249       if (first)
1250         first = 0;
1251       else
1252         fprintf_filtered (stream, _("; "));
1253
1254       /* (It would improve readability to print only the minimum
1255          necessary digits of the second number of the range.)  */
1256       fprintf_filtered (stream, _("range %s-%s, "),
1257                         paddress (gdbarch, low), paddress (gdbarch, high));
1258
1259       /* Now describe this particular location.  */
1260       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1261                                    objfile, addr_size);
1262
1263       loc_ptr += length;
1264     }
1265 }
1266
1267 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1268    any necessary bytecode in AX.  */
1269 static void
1270 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1271                             struct agent_expr *ax, struct axs_value *value)
1272 {
1273   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1274   gdb_byte *data;
1275   size_t size;
1276
1277   data = find_location_expression (dlbaton, &size, ax->scope);
1278
1279   dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
1280 }
1281
1282 /* The set of location functions used with the DWARF-2 expression
1283    evaluator and location lists.  */
1284 const struct symbol_computed_ops dwarf2_loclist_funcs = {
1285   loclist_read_variable,
1286   loclist_read_needs_frame,
1287   loclist_describe_location,
1288   loclist_tracepoint_var_ref
1289 };