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