Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: gdb/gdbserver/ax.c

Issue 11969036: Merge GDB 7.5.1 (Closed) Base URL: http://git.chromium.org/native_client/nacl-gdb.git@master
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gdb/gdbserver/ax.h ('k') | gdb/gdbserver/config.in » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
3
4 This file is part of GDB.
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 "server.h"
20 #include "ax.h"
21 #include "format.h"
22
23 static void ax_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
24
25 #ifdef IN_PROCESS_AGENT
26 int debug_agent = 0;
27 #endif
28
29 static void
30 ax_vdebug (const char *fmt, ...)
31 {
32 char buf[1024];
33 va_list ap;
34
35 va_start (ap, fmt);
36 vsprintf (buf, fmt, ap);
37 fprintf (stderr, PROG "/ax: %s\n", buf);
38 va_end (ap);
39 }
40
41 #define ax_debug_1(level, fmt, args...) \
42 do { \
43 if (level <= debug_threads) \
44 ax_vdebug ((fmt), ##args); \
45 } while (0)
46
47 #define ax_debug(FMT, args...) \
48 ax_debug_1 (1, FMT, ##args)
49
50 /* This enum must exactly match what is documented in
51 gdb/doc/agentexpr.texi, including all the numerical values. */
52
53 enum gdb_agent_op
54 {
55 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
56 gdb_agent_op_ ## NAME = VALUE,
57 #include "ax.def"
58 #undef DEFOP
59 gdb_agent_op_last
60 };
61
62 static const char *gdb_agent_op_names [gdb_agent_op_last] =
63 {
64 "?undef?"
65 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
66 #include "ax.def"
67 #undef DEFOP
68 };
69
70 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
71 {
72 0
73 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
74 #include "ax.def"
75 #undef DEFOP
76 };
77
78 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
79
80 static const char *
81 gdb_agent_op_name (int op)
82 {
83 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
84 return "?undef?";
85 return gdb_agent_op_names[op];
86 }
87
88 #ifndef IN_PROCESS_AGENT
89
90 /* The packet form of an agent expression consists of an 'X', number
91 of bytes in expression, a comma, and then the bytes. */
92
93 struct agent_expr *
94 gdb_parse_agent_expr (char **actparm)
95 {
96 char *act = *actparm;
97 ULONGEST xlen;
98 struct agent_expr *aexpr;
99
100 ++act; /* skip the X */
101 act = unpack_varlen_hex (act, &xlen);
102 ++act; /* skip a comma */
103 aexpr = xmalloc (sizeof (struct agent_expr));
104 aexpr->length = xlen;
105 aexpr->bytes = xmalloc (xlen);
106 convert_ascii_to_int (act, aexpr->bytes, xlen);
107 *actparm = act + (xlen * 2);
108 return aexpr;
109 }
110
111 /* Convert the bytes of an agent expression back into hex digits, so
112 they can be printed or uploaded. This allocates the buffer,
113 callers should free when they are done with it. */
114
115 char *
116 gdb_unparse_agent_expr (struct agent_expr *aexpr)
117 {
118 char *rslt;
119
120 rslt = xmalloc (2 * aexpr->length + 1);
121 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
122 return rslt;
123 }
124
125 /* Bytecode compilation. */
126
127 CORE_ADDR current_insn_ptr;
128
129 int emit_error;
130
131 struct bytecode_address
132 {
133 int pc;
134 CORE_ADDR address;
135 int goto_pc;
136 /* Offset and size of field to be modified in the goto block. */
137 int from_offset, from_size;
138 struct bytecode_address *next;
139 } *bytecode_address_table;
140
141 void
142 emit_prologue (void)
143 {
144 target_emit_ops ()->emit_prologue ();
145 }
146
147 void
148 emit_epilogue (void)
149 {
150 target_emit_ops ()->emit_epilogue ();
151 }
152
153 static void
154 emit_add (void)
155 {
156 target_emit_ops ()->emit_add ();
157 }
158
159 static void
160 emit_sub (void)
161 {
162 target_emit_ops ()->emit_sub ();
163 }
164
165 static void
166 emit_mul (void)
167 {
168 target_emit_ops ()->emit_mul ();
169 }
170
171 static void
172 emit_lsh (void)
173 {
174 target_emit_ops ()->emit_lsh ();
175 }
176
177 static void
178 emit_rsh_signed (void)
179 {
180 target_emit_ops ()->emit_rsh_signed ();
181 }
182
183 static void
184 emit_rsh_unsigned (void)
185 {
186 target_emit_ops ()->emit_rsh_unsigned ();
187 }
188
189 static void
190 emit_ext (int arg)
191 {
192 target_emit_ops ()->emit_ext (arg);
193 }
194
195 static void
196 emit_log_not (void)
197 {
198 target_emit_ops ()->emit_log_not ();
199 }
200
201 static void
202 emit_bit_and (void)
203 {
204 target_emit_ops ()->emit_bit_and ();
205 }
206
207 static void
208 emit_bit_or (void)
209 {
210 target_emit_ops ()->emit_bit_or ();
211 }
212
213 static void
214 emit_bit_xor (void)
215 {
216 target_emit_ops ()->emit_bit_xor ();
217 }
218
219 static void
220 emit_bit_not (void)
221 {
222 target_emit_ops ()->emit_bit_not ();
223 }
224
225 static void
226 emit_equal (void)
227 {
228 target_emit_ops ()->emit_equal ();
229 }
230
231 static void
232 emit_less_signed (void)
233 {
234 target_emit_ops ()->emit_less_signed ();
235 }
236
237 static void
238 emit_less_unsigned (void)
239 {
240 target_emit_ops ()->emit_less_unsigned ();
241 }
242
243 static void
244 emit_ref (int size)
245 {
246 target_emit_ops ()->emit_ref (size);
247 }
248
249 static void
250 emit_if_goto (int *offset_p, int *size_p)
251 {
252 target_emit_ops ()->emit_if_goto (offset_p, size_p);
253 }
254
255 static void
256 emit_goto (int *offset_p, int *size_p)
257 {
258 target_emit_ops ()->emit_goto (offset_p, size_p);
259 }
260
261 static void
262 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
263 {
264 target_emit_ops ()->write_goto_address (from, to, size);
265 }
266
267 static void
268 emit_const (LONGEST num)
269 {
270 target_emit_ops ()->emit_const (num);
271 }
272
273 static void
274 emit_reg (int reg)
275 {
276 target_emit_ops ()->emit_reg (reg);
277 }
278
279 static void
280 emit_pop (void)
281 {
282 target_emit_ops ()->emit_pop ();
283 }
284
285 static void
286 emit_stack_flush (void)
287 {
288 target_emit_ops ()->emit_stack_flush ();
289 }
290
291 static void
292 emit_zero_ext (int arg)
293 {
294 target_emit_ops ()->emit_zero_ext (arg);
295 }
296
297 static void
298 emit_swap (void)
299 {
300 target_emit_ops ()->emit_swap ();
301 }
302
303 static void
304 emit_stack_adjust (int n)
305 {
306 target_emit_ops ()->emit_stack_adjust (n);
307 }
308
309 /* FN's prototype is `LONGEST(*fn)(int)'. */
310
311 static void
312 emit_int_call_1 (CORE_ADDR fn, int arg1)
313 {
314 target_emit_ops ()->emit_int_call_1 (fn, arg1);
315 }
316
317 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
318
319 static void
320 emit_void_call_2 (CORE_ADDR fn, int arg1)
321 {
322 target_emit_ops ()->emit_void_call_2 (fn, arg1);
323 }
324
325 static void
326 emit_eq_goto (int *offset_p, int *size_p)
327 {
328 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
329 }
330
331 static void
332 emit_ne_goto (int *offset_p, int *size_p)
333 {
334 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
335 }
336
337 static void
338 emit_lt_goto (int *offset_p, int *size_p)
339 {
340 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
341 }
342
343 static void
344 emit_ge_goto (int *offset_p, int *size_p)
345 {
346 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
347 }
348
349 static void
350 emit_gt_goto (int *offset_p, int *size_p)
351 {
352 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
353 }
354
355 static void
356 emit_le_goto (int *offset_p, int *size_p)
357 {
358 target_emit_ops ()->emit_le_goto (offset_p, size_p);
359 }
360
361 /* Scan an agent expression for any evidence that the given PC is the
362 target of a jump bytecode in the expression. */
363
364 int
365 is_goto_target (struct agent_expr *aexpr, int pc)
366 {
367 int i;
368 unsigned char op;
369
370 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
371 {
372 op = aexpr->bytes[i];
373
374 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
375 {
376 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
377 if (target == pc)
378 return 1;
379 }
380 }
381
382 return 0;
383 }
384
385 /* Given an agent expression, turn it into native code. */
386
387 enum eval_result_type
388 compile_bytecodes (struct agent_expr *aexpr)
389 {
390 int pc = 0;
391 int done = 0;
392 unsigned char op, next_op;
393 int arg;
394 /* This is only used to build 64-bit value for constants. */
395 ULONGEST top;
396 struct bytecode_address *aentry, *aentry2;
397
398 #define UNHANDLED \
399 do \
400 { \
401 ax_debug ("Cannot compile op 0x%x\n", op); \
402 return expr_eval_unhandled_opcode; \
403 } while (0)
404
405 if (aexpr->length == 0)
406 {
407 ax_debug ("empty agent expression\n");
408 return expr_eval_empty_expression;
409 }
410
411 bytecode_address_table = NULL;
412
413 while (!done)
414 {
415 op = aexpr->bytes[pc];
416
417 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
418
419 /* Record the compiled-code address of the bytecode, for use by
420 jump instructions. */
421 aentry = xmalloc (sizeof (struct bytecode_address));
422 aentry->pc = pc;
423 aentry->address = current_insn_ptr;
424 aentry->goto_pc = -1;
425 aentry->from_offset = aentry->from_size = 0;
426 aentry->next = bytecode_address_table;
427 bytecode_address_table = aentry;
428
429 ++pc;
430
431 emit_error = 0;
432
433 switch (op)
434 {
435 case gdb_agent_op_add:
436 emit_add ();
437 break;
438
439 case gdb_agent_op_sub:
440 emit_sub ();
441 break;
442
443 case gdb_agent_op_mul:
444 emit_mul ();
445 break;
446
447 case gdb_agent_op_div_signed:
448 UNHANDLED;
449 break;
450
451 case gdb_agent_op_div_unsigned:
452 UNHANDLED;
453 break;
454
455 case gdb_agent_op_rem_signed:
456 UNHANDLED;
457 break;
458
459 case gdb_agent_op_rem_unsigned:
460 UNHANDLED;
461 break;
462
463 case gdb_agent_op_lsh:
464 emit_lsh ();
465 break;
466
467 case gdb_agent_op_rsh_signed:
468 emit_rsh_signed ();
469 break;
470
471 case gdb_agent_op_rsh_unsigned:
472 emit_rsh_unsigned ();
473 break;
474
475 case gdb_agent_op_trace:
476 UNHANDLED;
477 break;
478
479 case gdb_agent_op_trace_quick:
480 UNHANDLED;
481 break;
482
483 case gdb_agent_op_log_not:
484 emit_log_not ();
485 break;
486
487 case gdb_agent_op_bit_and:
488 emit_bit_and ();
489 break;
490
491 case gdb_agent_op_bit_or:
492 emit_bit_or ();
493 break;
494
495 case gdb_agent_op_bit_xor:
496 emit_bit_xor ();
497 break;
498
499 case gdb_agent_op_bit_not:
500 emit_bit_not ();
501 break;
502
503 case gdb_agent_op_equal:
504 next_op = aexpr->bytes[pc];
505 if (next_op == gdb_agent_op_if_goto
506 && !is_goto_target (aexpr, pc)
507 && target_emit_ops ()->emit_eq_goto)
508 {
509 ax_debug ("Combining equal & if_goto");
510 pc += 1;
511 aentry->pc = pc;
512 arg = aexpr->bytes[pc++];
513 arg = (arg << 8) + aexpr->bytes[pc++];
514 aentry->goto_pc = arg;
515 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
516 }
517 else if (next_op == gdb_agent_op_log_not
518 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
519 && !is_goto_target (aexpr, pc + 1)
520 && target_emit_ops ()->emit_ne_goto)
521 {
522 ax_debug ("Combining equal & log_not & if_goto");
523 pc += 2;
524 aentry->pc = pc;
525 arg = aexpr->bytes[pc++];
526 arg = (arg << 8) + aexpr->bytes[pc++];
527 aentry->goto_pc = arg;
528 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
529 }
530 else
531 emit_equal ();
532 break;
533
534 case gdb_agent_op_less_signed:
535 next_op = aexpr->bytes[pc];
536 if (next_op == gdb_agent_op_if_goto
537 && !is_goto_target (aexpr, pc))
538 {
539 ax_debug ("Combining less_signed & if_goto");
540 pc += 1;
541 aentry->pc = pc;
542 arg = aexpr->bytes[pc++];
543 arg = (arg << 8) + aexpr->bytes[pc++];
544 aentry->goto_pc = arg;
545 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
546 }
547 else if (next_op == gdb_agent_op_log_not
548 && !is_goto_target (aexpr, pc)
549 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
550 && !is_goto_target (aexpr, pc + 1))
551 {
552 ax_debug ("Combining less_signed & log_not & if_goto");
553 pc += 2;
554 aentry->pc = pc;
555 arg = aexpr->bytes[pc++];
556 arg = (arg << 8) + aexpr->bytes[pc++];
557 aentry->goto_pc = arg;
558 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
559 }
560 else
561 emit_less_signed ();
562 break;
563
564 case gdb_agent_op_less_unsigned:
565 emit_less_unsigned ();
566 break;
567
568 case gdb_agent_op_ext:
569 arg = aexpr->bytes[pc++];
570 if (arg < (sizeof (LONGEST) * 8))
571 emit_ext (arg);
572 break;
573
574 case gdb_agent_op_ref8:
575 emit_ref (1);
576 break;
577
578 case gdb_agent_op_ref16:
579 emit_ref (2);
580 break;
581
582 case gdb_agent_op_ref32:
583 emit_ref (4);
584 break;
585
586 case gdb_agent_op_ref64:
587 emit_ref (8);
588 break;
589
590 case gdb_agent_op_if_goto:
591 arg = aexpr->bytes[pc++];
592 arg = (arg << 8) + aexpr->bytes[pc++];
593 aentry->goto_pc = arg;
594 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
595 break;
596
597 case gdb_agent_op_goto:
598 arg = aexpr->bytes[pc++];
599 arg = (arg << 8) + aexpr->bytes[pc++];
600 aentry->goto_pc = arg;
601 emit_goto (&(aentry->from_offset), &(aentry->from_size));
602 break;
603
604 case gdb_agent_op_const8:
605 emit_stack_flush ();
606 top = aexpr->bytes[pc++];
607 emit_const (top);
608 break;
609
610 case gdb_agent_op_const16:
611 emit_stack_flush ();
612 top = aexpr->bytes[pc++];
613 top = (top << 8) + aexpr->bytes[pc++];
614 emit_const (top);
615 break;
616
617 case gdb_agent_op_const32:
618 emit_stack_flush ();
619 top = aexpr->bytes[pc++];
620 top = (top << 8) + aexpr->bytes[pc++];
621 top = (top << 8) + aexpr->bytes[pc++];
622 top = (top << 8) + aexpr->bytes[pc++];
623 emit_const (top);
624 break;
625
626 case gdb_agent_op_const64:
627 emit_stack_flush ();
628 top = aexpr->bytes[pc++];
629 top = (top << 8) + aexpr->bytes[pc++];
630 top = (top << 8) + aexpr->bytes[pc++];
631 top = (top << 8) + aexpr->bytes[pc++];
632 top = (top << 8) + aexpr->bytes[pc++];
633 top = (top << 8) + aexpr->bytes[pc++];
634 top = (top << 8) + aexpr->bytes[pc++];
635 top = (top << 8) + aexpr->bytes[pc++];
636 emit_const (top);
637 break;
638
639 case gdb_agent_op_reg:
640 emit_stack_flush ();
641 arg = aexpr->bytes[pc++];
642 arg = (arg << 8) + aexpr->bytes[pc++];
643 emit_reg (arg);
644 break;
645
646 case gdb_agent_op_end:
647 ax_debug ("At end of expression\n");
648
649 /* Assume there is one stack element left, and that it is
650 cached in "top" where emit_epilogue can get to it. */
651 emit_stack_adjust (1);
652
653 done = 1;
654 break;
655
656 case gdb_agent_op_dup:
657 /* In our design, dup is equivalent to stack flushing. */
658 emit_stack_flush ();
659 break;
660
661 case gdb_agent_op_pop:
662 emit_pop ();
663 break;
664
665 case gdb_agent_op_zero_ext:
666 arg = aexpr->bytes[pc++];
667 if (arg < (sizeof (LONGEST) * 8))
668 emit_zero_ext (arg);
669 break;
670
671 case gdb_agent_op_swap:
672 next_op = aexpr->bytes[pc];
673 /* Detect greater-than comparison sequences. */
674 if (next_op == gdb_agent_op_less_signed
675 && !is_goto_target (aexpr, pc)
676 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
677 && !is_goto_target (aexpr, pc + 1))
678 {
679 ax_debug ("Combining swap & less_signed & if_goto");
680 pc += 2;
681 aentry->pc = pc;
682 arg = aexpr->bytes[pc++];
683 arg = (arg << 8) + aexpr->bytes[pc++];
684 aentry->goto_pc = arg;
685 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
686 }
687 else if (next_op == gdb_agent_op_less_signed
688 && !is_goto_target (aexpr, pc)
689 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
690 && !is_goto_target (aexpr, pc + 1)
691 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
692 && !is_goto_target (aexpr, pc + 2))
693 {
694 ax_debug ("Combining swap & less_signed & log_not & if_goto");
695 pc += 3;
696 aentry->pc = pc;
697 arg = aexpr->bytes[pc++];
698 arg = (arg << 8) + aexpr->bytes[pc++];
699 aentry->goto_pc = arg;
700 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
701 }
702 else
703 emit_swap ();
704 break;
705
706 case gdb_agent_op_getv:
707 emit_stack_flush ();
708 arg = aexpr->bytes[pc++];
709 arg = (arg << 8) + aexpr->bytes[pc++];
710 emit_int_call_1 (get_get_tsv_func_addr (),
711 arg);
712 break;
713
714 case gdb_agent_op_setv:
715 arg = aexpr->bytes[pc++];
716 arg = (arg << 8) + aexpr->bytes[pc++];
717 emit_void_call_2 (get_set_tsv_func_addr (),
718 arg);
719 break;
720
721 case gdb_agent_op_tracev:
722 UNHANDLED;
723 break;
724
725 /* GDB never (currently) generates any of these ops. */
726 case gdb_agent_op_float:
727 case gdb_agent_op_ref_float:
728 case gdb_agent_op_ref_double:
729 case gdb_agent_op_ref_long_double:
730 case gdb_agent_op_l_to_d:
731 case gdb_agent_op_d_to_l:
732 case gdb_agent_op_trace16:
733 UNHANDLED;
734 break;
735
736 default:
737 ax_debug ("Agent expression op 0x%x not recognized\n", op);
738 /* Don't struggle on, things will just get worse. */
739 return expr_eval_unrecognized_opcode;
740 }
741
742 /* This catches errors that occur in target-specific code
743 emission. */
744 if (emit_error)
745 {
746 ax_debug ("Error %d while emitting code for %s\n",
747 emit_error, gdb_agent_op_name (op));
748 return expr_eval_unhandled_opcode;
749 }
750
751 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
752 }
753
754 /* Now fill in real addresses as goto destinations. */
755 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
756 {
757 int written = 0;
758
759 if (aentry->goto_pc < 0)
760 continue;
761
762 /* Find the location that we are going to, and call back into
763 target-specific code to write the actual address or
764 displacement. */
765 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
766 {
767 if (aentry2->pc == aentry->goto_pc)
768 {
769 ax_debug ("Want to jump from %s to %s\n",
770 paddress (aentry->address),
771 paddress (aentry2->address));
772 write_goto_address (aentry->address + aentry->from_offset,
773 aentry2->address, aentry->from_size);
774 written = 1;
775 break;
776 }
777 }
778
779 /* Error out if we didn't find a destination. */
780 if (!written)
781 {
782 ax_debug ("Destination of goto %d not found\n",
783 aentry->goto_pc);
784 return expr_eval_invalid_goto;
785 }
786 }
787
788 return expr_eval_no_error;
789 }
790
791 #endif
792
793 /* Make printf-type calls using arguments supplied from the host. We
794 need to parse the format string ourselves, and call the formatting
795 function with one argument at a time, partly because there is no
796 safe portable way to construct a varargs call, and partly to serve
797 as a security barrier against bad format strings that might get
798 in. */
799
800 static void
801 ax_printf (CORE_ADDR fn, CORE_ADDR chan, char *format,
802 int nargs, ULONGEST *args)
803 {
804 char *f = format;
805 struct format_piece *fpieces;
806 int i, fp;
807 char *current_substring;
808 int nargs_wanted;
809
810 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
811
812 fpieces = parse_format_string (&f);
813
814 nargs_wanted = 0;
815 for (fp = 0; fpieces[fp].string != NULL; fp++)
816 if (fpieces[fp].argclass != literal_piece)
817 ++nargs_wanted;
818
819 if (nargs != nargs_wanted)
820 error (_("Wrong number of arguments for specified format-string"));
821
822 i = 0;
823 for (fp = 0; fpieces[fp].string != NULL; fp++)
824 {
825 current_substring = fpieces[fp].string;
826 ax_debug ("current substring is '%s', class is %d",
827 current_substring, fpieces[fp].argclass);
828 switch (fpieces[fp].argclass)
829 {
830 case string_arg:
831 {
832 gdb_byte *str;
833 CORE_ADDR tem;
834 int j;
835
836 tem = args[i];
837
838 /* This is a %s argument. Find the length of the string. */
839 for (j = 0;; j++)
840 {
841 gdb_byte c;
842
843 read_inferior_memory (tem + j, &c, 1);
844 if (c == 0)
845 break;
846 }
847
848 /* Copy the string contents into a string inside GDB. */
849 str = (gdb_byte *) alloca (j + 1);
850 if (j != 0)
851 read_inferior_memory (tem, str, j);
852 str[j] = 0;
853
854 printf (current_substring, (char *) str);
855 }
856 break;
857
858 case long_long_arg:
859 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
860 {
861 long long val = args[i];
862
863 printf (current_substring, val);
864 break;
865 }
866 #else
867 error (_("long long not supported in agent printf"));
868 #endif
869 case int_arg:
870 {
871 int val = args[i];
872
873 printf (current_substring, val);
874 break;
875 }
876
877 case long_arg:
878 {
879 long val = args[i];
880
881 printf (current_substring, val);
882 break;
883 }
884
885 case literal_piece:
886 /* Print a portion of the format string that has no
887 directives. Note that this will not include any
888 ordinary %-specs, but it might include "%%". That is
889 why we use printf_filtered and not puts_filtered here.
890 Also, we pass a dummy argument because some platforms
891 have modified GCC to include -Wformat-security by
892 default, which will warn here if there is no
893 argument. */
894 printf (current_substring, 0);
895 break;
896
897 default:
898 error (_("Format directive in '%s' not supported in agent printf"),
899 current_substring);
900 }
901
902 /* Maybe advance to the next argument. */
903 if (fpieces[fp].argclass != literal_piece)
904 ++i;
905 }
906
907 free_format_pieces (fpieces);
908 }
909
910 /* The agent expression evaluator, as specified by the GDB docs. It
911 returns 0 if everything went OK, and a nonzero error code
912 otherwise. */
913
914 enum eval_result_type
915 gdb_eval_agent_expr (struct regcache *regcache,
916 struct traceframe *tframe,
917 struct agent_expr *aexpr,
918 ULONGEST *rslt)
919 {
920 int pc = 0;
921 #define STACK_MAX 100
922 ULONGEST stack[STACK_MAX], top;
923 int sp = 0;
924 unsigned char op;
925 int arg;
926
927 /* This union is a convenient way to convert representations. For
928 now, assume a standard architecture where the hardware integer
929 types have 8, 16, 32, 64 bit types. A more robust solution would
930 be to import stdint.h from gnulib. */
931 union
932 {
933 union
934 {
935 unsigned char bytes[1];
936 unsigned char val;
937 } u8;
938 union
939 {
940 unsigned char bytes[2];
941 unsigned short val;
942 } u16;
943 union
944 {
945 unsigned char bytes[4];
946 unsigned int val;
947 } u32;
948 union
949 {
950 unsigned char bytes[8];
951 ULONGEST val;
952 } u64;
953 } cnv;
954
955 if (aexpr->length == 0)
956 {
957 ax_debug ("empty agent expression");
958 return expr_eval_empty_expression;
959 }
960
961 /* Cache the stack top in its own variable. Much of the time we can
962 operate on this variable, rather than dinking with the stack. It
963 needs to be copied to the stack when sp changes. */
964 top = 0;
965
966 while (1)
967 {
968 op = aexpr->bytes[pc++];
969
970 ax_debug ("About to interpret byte 0x%x", op);
971
972 switch (op)
973 {
974 case gdb_agent_op_add:
975 top += stack[--sp];
976 break;
977
978 case gdb_agent_op_sub:
979 top = stack[--sp] - top;
980 break;
981
982 case gdb_agent_op_mul:
983 top *= stack[--sp];
984 break;
985
986 case gdb_agent_op_div_signed:
987 if (top == 0)
988 {
989 ax_debug ("Attempted to divide by zero");
990 return expr_eval_divide_by_zero;
991 }
992 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
993 break;
994
995 case gdb_agent_op_div_unsigned:
996 if (top == 0)
997 {
998 ax_debug ("Attempted to divide by zero");
999 return expr_eval_divide_by_zero;
1000 }
1001 top = stack[--sp] / top;
1002 break;
1003
1004 case gdb_agent_op_rem_signed:
1005 if (top == 0)
1006 {
1007 ax_debug ("Attempted to divide by zero");
1008 return expr_eval_divide_by_zero;
1009 }
1010 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1011 break;
1012
1013 case gdb_agent_op_rem_unsigned:
1014 if (top == 0)
1015 {
1016 ax_debug ("Attempted to divide by zero");
1017 return expr_eval_divide_by_zero;
1018 }
1019 top = stack[--sp] % top;
1020 break;
1021
1022 case gdb_agent_op_lsh:
1023 top = stack[--sp] << top;
1024 break;
1025
1026 case gdb_agent_op_rsh_signed:
1027 top = ((LONGEST) stack[--sp]) >> top;
1028 break;
1029
1030 case gdb_agent_op_rsh_unsigned:
1031 top = stack[--sp] >> top;
1032 break;
1033
1034 case gdb_agent_op_trace:
1035 agent_mem_read (tframe,
1036 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
1037 if (--sp >= 0)
1038 top = stack[sp];
1039 break;
1040
1041 case gdb_agent_op_trace_quick:
1042 arg = aexpr->bytes[pc++];
1043 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1044 break;
1045
1046 case gdb_agent_op_log_not:
1047 top = !top;
1048 break;
1049
1050 case gdb_agent_op_bit_and:
1051 top &= stack[--sp];
1052 break;
1053
1054 case gdb_agent_op_bit_or:
1055 top |= stack[--sp];
1056 break;
1057
1058 case gdb_agent_op_bit_xor:
1059 top ^= stack[--sp];
1060 break;
1061
1062 case gdb_agent_op_bit_not:
1063 top = ~top;
1064 break;
1065
1066 case gdb_agent_op_equal:
1067 top = (stack[--sp] == top);
1068 break;
1069
1070 case gdb_agent_op_less_signed:
1071 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1072 break;
1073
1074 case gdb_agent_op_less_unsigned:
1075 top = (stack[--sp] < top);
1076 break;
1077
1078 case gdb_agent_op_ext:
1079 arg = aexpr->bytes[pc++];
1080 if (arg < (sizeof (LONGEST) * 8))
1081 {
1082 LONGEST mask = 1 << (arg - 1);
1083 top &= ((LONGEST) 1 << arg) - 1;
1084 top = (top ^ mask) - mask;
1085 }
1086 break;
1087
1088 case gdb_agent_op_ref8:
1089 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
1090 top = cnv.u8.val;
1091 break;
1092
1093 case gdb_agent_op_ref16:
1094 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
1095 top = cnv.u16.val;
1096 break;
1097
1098 case gdb_agent_op_ref32:
1099 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
1100 top = cnv.u32.val;
1101 break;
1102
1103 case gdb_agent_op_ref64:
1104 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
1105 top = cnv.u64.val;
1106 break;
1107
1108 case gdb_agent_op_if_goto:
1109 if (top)
1110 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1111 else
1112 pc += 2;
1113 if (--sp >= 0)
1114 top = stack[sp];
1115 break;
1116
1117 case gdb_agent_op_goto:
1118 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1119 break;
1120
1121 case gdb_agent_op_const8:
1122 /* Flush the cached stack top. */
1123 stack[sp++] = top;
1124 top = aexpr->bytes[pc++];
1125 break;
1126
1127 case gdb_agent_op_const16:
1128 /* Flush the cached stack top. */
1129 stack[sp++] = top;
1130 top = aexpr->bytes[pc++];
1131 top = (top << 8) + aexpr->bytes[pc++];
1132 break;
1133
1134 case gdb_agent_op_const32:
1135 /* Flush the cached stack top. */
1136 stack[sp++] = top;
1137 top = aexpr->bytes[pc++];
1138 top = (top << 8) + aexpr->bytes[pc++];
1139 top = (top << 8) + aexpr->bytes[pc++];
1140 top = (top << 8) + aexpr->bytes[pc++];
1141 break;
1142
1143 case gdb_agent_op_const64:
1144 /* Flush the cached stack top. */
1145 stack[sp++] = top;
1146 top = aexpr->bytes[pc++];
1147 top = (top << 8) + aexpr->bytes[pc++];
1148 top = (top << 8) + aexpr->bytes[pc++];
1149 top = (top << 8) + aexpr->bytes[pc++];
1150 top = (top << 8) + aexpr->bytes[pc++];
1151 top = (top << 8) + aexpr->bytes[pc++];
1152 top = (top << 8) + aexpr->bytes[pc++];
1153 top = (top << 8) + aexpr->bytes[pc++];
1154 break;
1155
1156 case gdb_agent_op_reg:
1157 /* Flush the cached stack top. */
1158 stack[sp++] = top;
1159 arg = aexpr->bytes[pc++];
1160 arg = (arg << 8) + aexpr->bytes[pc++];
1161 {
1162 int regnum = arg;
1163
1164 switch (register_size (regnum))
1165 {
1166 case 8:
1167 collect_register (regcache, regnum, cnv.u64.bytes);
1168 top = cnv.u64.val;
1169 break;
1170 case 4:
1171 collect_register (regcache, regnum, cnv.u32.bytes);
1172 top = cnv.u32.val;
1173 break;
1174 case 2:
1175 collect_register (regcache, regnum, cnv.u16.bytes);
1176 top = cnv.u16.val;
1177 break;
1178 case 1:
1179 collect_register (regcache, regnum, cnv.u8.bytes);
1180 top = cnv.u8.val;
1181 break;
1182 default:
1183 internal_error (__FILE__, __LINE__,
1184 "unhandled register size");
1185 }
1186 }
1187 break;
1188
1189 case gdb_agent_op_end:
1190 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1191 sp, pulongest (top));
1192 if (rslt)
1193 {
1194 if (sp <= 0)
1195 {
1196 /* This should be an error */
1197 ax_debug ("Stack is empty, nothing to return");
1198 return expr_eval_empty_stack;
1199 }
1200 *rslt = top;
1201 }
1202 return expr_eval_no_error;
1203
1204 case gdb_agent_op_dup:
1205 stack[sp++] = top;
1206 break;
1207
1208 case gdb_agent_op_pop:
1209 if (--sp >= 0)
1210 top = stack[sp];
1211 break;
1212
1213 case gdb_agent_op_pick:
1214 arg = aexpr->bytes[pc++];
1215 stack[sp] = top;
1216 top = stack[sp - arg];
1217 ++sp;
1218 break;
1219
1220 case gdb_agent_op_rot:
1221 {
1222 ULONGEST tem = stack[sp - 1];
1223
1224 stack[sp - 1] = stack[sp - 2];
1225 stack[sp - 2] = top;
1226 top = tem;
1227 }
1228 break;
1229
1230 case gdb_agent_op_zero_ext:
1231 arg = aexpr->bytes[pc++];
1232 if (arg < (sizeof (LONGEST) * 8))
1233 top &= ((LONGEST) 1 << arg) - 1;
1234 break;
1235
1236 case gdb_agent_op_swap:
1237 /* Interchange top two stack elements, making sure top gets
1238 copied back onto stack. */
1239 stack[sp] = top;
1240 top = stack[sp - 1];
1241 stack[sp - 1] = stack[sp];
1242 break;
1243
1244 case gdb_agent_op_getv:
1245 /* Flush the cached stack top. */
1246 stack[sp++] = top;
1247 arg = aexpr->bytes[pc++];
1248 arg = (arg << 8) + aexpr->bytes[pc++];
1249 top = agent_get_trace_state_variable_value (arg);
1250 break;
1251
1252 case gdb_agent_op_setv:
1253 arg = aexpr->bytes[pc++];
1254 arg = (arg << 8) + aexpr->bytes[pc++];
1255 agent_set_trace_state_variable_value (arg, top);
1256 /* Note that we leave the value on the stack, for the
1257 benefit of later/enclosing expressions. */
1258 break;
1259
1260 case gdb_agent_op_tracev:
1261 arg = aexpr->bytes[pc++];
1262 arg = (arg << 8) + aexpr->bytes[pc++];
1263 agent_tsv_read (tframe, arg);
1264 break;
1265
1266 case gdb_agent_op_tracenz:
1267 agent_mem_read_string (tframe, NULL, (CORE_ADDR) stack[--sp],
1268 (ULONGEST) top);
1269 if (--sp >= 0)
1270 top = stack[sp];
1271 break;
1272
1273 case gdb_agent_op_printf:
1274 {
1275 int nargs, slen, i;
1276 CORE_ADDR fn = 0, chan = 0;
1277 /* Can't have more args than the entire size of the stack. */
1278 ULONGEST args[STACK_MAX];
1279 char *format;
1280
1281 nargs = aexpr->bytes[pc++];
1282 slen = aexpr->bytes[pc++];
1283 slen = (slen << 8) + aexpr->bytes[pc++];
1284 format = (char *) &(aexpr->bytes[pc]);
1285 pc += slen;
1286 /* Pop function and channel. */
1287 fn = top;
1288 if (--sp >= 0)
1289 top = stack[sp];
1290 chan = top;
1291 if (--sp >= 0)
1292 top = stack[sp];
1293 /* Pop arguments into a dedicated array. */
1294 for (i = 0; i < nargs; ++i)
1295 {
1296 args[i] = top;
1297 if (--sp >= 0)
1298 top = stack[sp];
1299 }
1300
1301 /* A bad format string means something is very wrong; give
1302 up immediately. */
1303 if (format[slen - 1] != '\0')
1304 error (_("Unterminated format string in printf bytecode"));
1305
1306 ax_printf (fn, chan, format, nargs, args);
1307 }
1308 break;
1309
1310 /* GDB never (currently) generates any of these ops. */
1311 case gdb_agent_op_float:
1312 case gdb_agent_op_ref_float:
1313 case gdb_agent_op_ref_double:
1314 case gdb_agent_op_ref_long_double:
1315 case gdb_agent_op_l_to_d:
1316 case gdb_agent_op_d_to_l:
1317 case gdb_agent_op_trace16:
1318 ax_debug ("Agent expression op 0x%x valid, but not handled",
1319 op);
1320 /* If ever GDB generates any of these, we don't have the
1321 option of ignoring. */
1322 return 1;
1323
1324 default:
1325 ax_debug ("Agent expression op 0x%x not recognized", op);
1326 /* Don't struggle on, things will just get worse. */
1327 return expr_eval_unrecognized_opcode;
1328 }
1329
1330 /* Check for stack badness. */
1331 if (sp >= (STACK_MAX - 1))
1332 {
1333 ax_debug ("Expression stack overflow");
1334 return expr_eval_stack_overflow;
1335 }
1336
1337 if (sp < 0)
1338 {
1339 ax_debug ("Expression stack underflow");
1340 return expr_eval_stack_underflow;
1341 }
1342
1343 ax_debug ("Op %s -> sp=%d, top=0x%s",
1344 gdb_agent_op_name (op), sp, phex_nz (top, 0));
1345 }
1346 }
OLDNEW
« no previous file with comments | « gdb/gdbserver/ax.h ('k') | gdb/gdbserver/config.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698