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