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