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