* gas/config/tc-arm.c (do_t_mov_cmp): Fix reporting of unpredictable and
[deliverable/binutils-gdb.git] / gdb / gdbserver / tracepoint.c
CommitLineData
219f2f23
PA
1/* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "server.h"
20#include <ctype.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <sys/time.h>
fa593d66
PA
24#include <stddef.h>
25#if HAVE_MALLOC_H
26#include <malloc.h>
27#endif
28#if HAVE_STDINT_H
29#include <stdint.h>
30#endif
219f2f23 31
fa593d66
PA
32/* This file is built for both both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
41
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
49
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
55 at appropriate times.
56
57*/
58
59static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
219f2f23
PA
60
61static void
fa593d66 62trace_vdebug (const char *fmt, ...)
219f2f23
PA
63{
64 char buf[1024];
65 va_list ap;
66
67 va_start (ap, fmt);
68 vsprintf (buf, fmt, ap);
69 fprintf (stderr, "gdbserver/tracepoint: %s\n", buf);
70 va_end (ap);
71}
72
fa593d66 73#define trace_debug_1(level, fmt, args...) \
219f2f23 74 do { \
fa593d66
PA
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
219f2f23
PA
77 } while (0)
78
fa593d66
PA
79#define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
81
82#if defined(__GNUC__)
83# define ATTR_USED __attribute__((used))
84# define ATTR_NOINLINE __attribute__((noinline))
85# define ATTR_CONSTRUCTOR __attribute__ ((constructor))
86#else
87# define ATTR_USED
88# define ATTR_NOINLINE
89# define ATTR_CONSTRUCTOR
90#endif
91
92/* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
94
95#ifdef IN_PROCESS_AGENT
96# if defined _WIN32 || defined __CYGWIN__
97# define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
98# else
99# if __GNUC__ >= 4
100# define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
102# else
103# define IP_AGENT_EXPORT ATTR_USED
104# endif
105# endif
106#else
107# define IP_AGENT_EXPORT
108#endif
109
110/* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112#ifdef IN_PROCESS_AGENT
113# define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114# define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115# define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116# define collecting gdb_agent_collecting
117# define gdb_collect gdb_agent_gdb_collect
118# define stop_tracing gdb_agent_stop_tracing
119# define flush_trace_buffer gdb_agent_flush_trace_buffer
120# define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
121# define trace_buffer_is_full gdb_agent_trace_buffer_is_full
122# define stopping_tracepoint gdb_agent_stopping_tracepoint
123# define expr_eval_result gdb_agent_expr_eval_result
124# define error_tracepoint gdb_agent_error_tracepoint
125# define tracepoints gdb_agent_tracepoints
126# define tracing gdb_agent_tracing
127# define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
128# define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
129# define trace_buffer_lo gdb_agent_trace_buffer_lo
130# define trace_buffer_hi gdb_agent_trace_buffer_hi
131# define traceframe_read_count gdb_agent_traceframe_read_count
132# define traceframe_write_count gdb_agent_traceframe_write_count
133# define traceframes_created gdb_agent_traceframes_created
134# define trace_state_variables gdb_agent_trace_state_variables
6a271cae
PA
135# define get_raw_reg gdb_agent_get_raw_reg
136# define get_trace_state_variable_value gdb_agent_get_trace_state_variable_value
137# define set_trace_state_variable_value gdb_agent_set_trace_state_variable_value
fa593d66
PA
138#endif
139
140#ifndef IN_PROCESS_AGENT
141
142/* Addresses of in-process agent's symbols GDBserver cares about. */
143
144struct ipa_sym_addresses
145{
146 CORE_ADDR addr_gdb_tp_heap_buffer;
147 CORE_ADDR addr_gdb_jump_pad_buffer;
148 CORE_ADDR addr_gdb_jump_pad_buffer_end;
149 CORE_ADDR addr_collecting;
150 CORE_ADDR addr_gdb_collect;
151 CORE_ADDR addr_stop_tracing;
152 CORE_ADDR addr_flush_trace_buffer;
153 CORE_ADDR addr_about_to_request_buffer_space;
154 CORE_ADDR addr_trace_buffer_is_full;
155 CORE_ADDR addr_stopping_tracepoint;
156 CORE_ADDR addr_expr_eval_result;
157 CORE_ADDR addr_error_tracepoint;
158 CORE_ADDR addr_tracepoints;
159 CORE_ADDR addr_tracing;
160 CORE_ADDR addr_trace_buffer_ctrl;
161 CORE_ADDR addr_trace_buffer_ctrl_curr;
162 CORE_ADDR addr_trace_buffer_lo;
163 CORE_ADDR addr_trace_buffer_hi;
164 CORE_ADDR addr_traceframe_read_count;
165 CORE_ADDR addr_traceframe_write_count;
166 CORE_ADDR addr_traceframes_created;
167 CORE_ADDR addr_trace_state_variables;
6a271cae
PA
168 CORE_ADDR addr_get_raw_reg;
169 CORE_ADDR addr_get_trace_state_variable_value;
170 CORE_ADDR addr_set_trace_state_variable_value;
fa593d66
PA
171};
172
173#define STRINGIZE_1(STR) #STR
174#define STRINGIZE(STR) STRINGIZE_1(STR)
175#define IPA_SYM(SYM) \
176 { \
177 STRINGIZE (gdb_agent_ ## SYM), \
178 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
179 }
180
181static struct
182{
183 const char *name;
184 int offset;
185 int required;
186} symbol_list[] = {
187 IPA_SYM(gdb_tp_heap_buffer),
188 IPA_SYM(gdb_jump_pad_buffer),
189 IPA_SYM(gdb_jump_pad_buffer_end),
190 IPA_SYM(collecting),
191 IPA_SYM(gdb_collect),
192 IPA_SYM(stop_tracing),
193 IPA_SYM(flush_trace_buffer),
194 IPA_SYM(about_to_request_buffer_space),
195 IPA_SYM(trace_buffer_is_full),
196 IPA_SYM(stopping_tracepoint),
197 IPA_SYM(expr_eval_result),
198 IPA_SYM(error_tracepoint),
199 IPA_SYM(tracepoints),
200 IPA_SYM(tracing),
201 IPA_SYM(trace_buffer_ctrl),
202 IPA_SYM(trace_buffer_ctrl_curr),
203 IPA_SYM(trace_buffer_lo),
204 IPA_SYM(trace_buffer_hi),
205 IPA_SYM(traceframe_read_count),
206 IPA_SYM(traceframe_write_count),
207 IPA_SYM(traceframes_created),
208 IPA_SYM(trace_state_variables),
6a271cae
PA
209 IPA_SYM(get_raw_reg),
210 IPA_SYM(get_trace_state_variable_value),
211 IPA_SYM(set_trace_state_variable_value),
fa593d66
PA
212};
213
214struct ipa_sym_addresses ipa_sym_addrs;
215
216int all_tracepoint_symbols_looked_up;
217
218int
219in_process_agent_loaded (void)
220{
221 return all_tracepoint_symbols_looked_up;
222}
223
224static int read_inferior_integer (CORE_ADDR symaddr, int *val);
225
226static void
227write_e_ipa_not_loaded (char *buffer)
228{
229 sprintf (buffer,
230 "E.In-process agent library not loaded in process. "
231 "Dynamic tracepoints unavailable.");
232}
233
234static int
235maybe_write_ipa_not_loaded (char *buffer)
236{
237 if (!in_process_agent_loaded ())
238 {
239 write_e_ipa_not_loaded (buffer);
240 return 1;
241 }
242 return 0;
243}
244
245/* Cache all future symbols that the tracepoints module might request.
246 We can not request symbols at arbitrary states in the remote
247 protocol, only when the client tells us that new symbols are
248 available. So when we load the in-process library, make sure to
249 check the entire list. */
250
251void
252tracepoint_look_up_symbols (void)
253{
254 int all_ok;
255 int i;
256
257 if (all_tracepoint_symbols_looked_up)
258 return;
259
260 all_ok = 1;
261 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
262 {
263 CORE_ADDR *addrp =
264 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
265
266 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
267 {
268 if (debug_threads)
269 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
270 all_ok = 0;
271 }
272 }
273
274 all_tracepoint_symbols_looked_up = all_ok;
275}
276
277#endif
278
279/* GDBserver places a breakpoint on the IPA's version (which is a nop)
280 of the "stop_tracing" function. When this breakpoint is hit,
281 tracing stopped in the IPA for some reason. E.g., due to
282 tracepoint reaching the pass count, hitting conditional expression
283 evaluation error, etc.
284
285 The IPA's trace buffer is never in circular tracing mode: instead,
286 GDBserver's is, and whenever the in-process buffer fills, it calls
287 "flush_trace_buffer", which triggers an internal breakpoint.
288 GDBserver reacts to this breakpoint by pulling the meanwhile
289 collected data. Old frames discarding is always handled on the
290 GDBserver side. */
291
292#ifdef IN_PROCESS_AGENT
293int debug_threads = 0;
294
295int
296read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
297{
298 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
299 return 0;
300}
301
302/* Call this in the functions where GDBserver places a breakpoint, so
303 that the compiler doesn't try to be clever and skip calling the
304 function at all. This is necessary, even if we tell the compiler
305 to not inline said functions. */
306
307#if defined(__GNUC__)
308# define UNKNOWN_SIDE_EFFECTS() asm ("")
309#else
310# define UNKNOWN_SIDE_EFFECTS() do {} while (0)
311#endif
312
313IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
314stop_tracing (void)
315{
316 /* GDBserver places breakpoint here. */
317 UNKNOWN_SIDE_EFFECTS();
318}
319
320IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
321flush_trace_buffer (void)
322{
323 /* GDBserver places breakpoint here. */
324 UNKNOWN_SIDE_EFFECTS();
325}
326
327#endif
328
329#ifndef IN_PROCESS_AGENT
219f2f23
PA
330static int
331tracepoint_handler (CORE_ADDR address)
332{
333 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
334 paddress (address));
335 return 0;
336}
337
fa593d66
PA
338/* Breakpoint at "stop_tracing" in the inferior lib. */
339struct breakpoint *stop_tracing_bkpt;
340static int stop_tracing_handler (CORE_ADDR);
341
342/* Breakpoint at "flush_trace_buffer" in the inferior lib. */
343struct breakpoint *flush_trace_buffer_bkpt;
344static int flush_trace_buffer_handler (CORE_ADDR);
345
346static void download_tracepoints (void);
347static void download_trace_state_variables (void);
348static void upload_fast_traceframes (void);
349
350static int
351read_inferior_integer (CORE_ADDR symaddr, int *val)
352{
353 return read_inferior_memory (symaddr, (unsigned char *) val,
354 sizeof (*val));
355}
356
357static int
358read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
359{
360 return read_inferior_memory (symaddr, (unsigned char *) val,
361 sizeof (*val));
362}
363
364static int
365read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
366{
367 void *pval = (void *) (uintptr_t) val;
368 int ret;
369
370 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
371 *val = (uintptr_t) pval;
372 return ret;
373}
374
375static int
376write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
377{
378 void *pval = (void *) (uintptr_t) val;
379 return write_inferior_memory (symaddr,
380 (unsigned char *) &pval, sizeof (pval));
381}
382
383static int
384write_inferior_integer (CORE_ADDR symaddr, int val)
385{
386 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
387}
388
389static int
390write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
391{
392 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
393}
394
395#endif
396
219f2f23
PA
397/* This enum must exactly match what is documented in
398 gdb/doc/agentexpr.texi, including all the numerical values. */
399
400enum gdb_agent_op
401 {
402 gdb_agent_op_float = 0x01,
403 gdb_agent_op_add = 0x02,
404 gdb_agent_op_sub = 0x03,
405 gdb_agent_op_mul = 0x04,
406 gdb_agent_op_div_signed = 0x05,
407 gdb_agent_op_div_unsigned = 0x06,
408 gdb_agent_op_rem_signed = 0x07,
409 gdb_agent_op_rem_unsigned = 0x08,
410 gdb_agent_op_lsh = 0x09,
411 gdb_agent_op_rsh_signed = 0x0a,
412 gdb_agent_op_rsh_unsigned = 0x0b,
413 gdb_agent_op_trace = 0x0c,
414 gdb_agent_op_trace_quick = 0x0d,
415 gdb_agent_op_log_not = 0x0e,
416 gdb_agent_op_bit_and = 0x0f,
417 gdb_agent_op_bit_or = 0x10,
418 gdb_agent_op_bit_xor = 0x11,
419 gdb_agent_op_bit_not = 0x12,
420 gdb_agent_op_equal = 0x13,
421 gdb_agent_op_less_signed = 0x14,
422 gdb_agent_op_less_unsigned = 0x15,
423 gdb_agent_op_ext = 0x16,
424 gdb_agent_op_ref8 = 0x17,
425 gdb_agent_op_ref16 = 0x18,
426 gdb_agent_op_ref32 = 0x19,
427 gdb_agent_op_ref64 = 0x1a,
428 gdb_agent_op_ref_float = 0x1b,
429 gdb_agent_op_ref_double = 0x1c,
430 gdb_agent_op_ref_long_double = 0x1d,
431 gdb_agent_op_l_to_d = 0x1e,
432 gdb_agent_op_d_to_l = 0x1f,
433 gdb_agent_op_if_goto = 0x20,
434 gdb_agent_op_goto = 0x21,
435 gdb_agent_op_const8 = 0x22,
436 gdb_agent_op_const16 = 0x23,
437 gdb_agent_op_const32 = 0x24,
438 gdb_agent_op_const64 = 0x25,
439 gdb_agent_op_reg = 0x26,
440 gdb_agent_op_end = 0x27,
441 gdb_agent_op_dup = 0x28,
442 gdb_agent_op_pop = 0x29,
443 gdb_agent_op_zero_ext = 0x2a,
444 gdb_agent_op_swap = 0x2b,
445 gdb_agent_op_getv = 0x2c,
446 gdb_agent_op_setv = 0x2d,
447 gdb_agent_op_tracev = 0x2e,
448 gdb_agent_op_trace16 = 0x30,
449 gdb_agent_op_last
450 };
451
452static const char *gdb_agent_op_names [gdb_agent_op_last] =
453 {
454 "?undef?",
455 "float",
456 "add",
457 "sub",
458 "mul",
459 "div_signed",
460 "div_unsigned",
461 "rem_signed",
462 "rem_unsigned",
463 "lsh",
464 "rsh_signed",
465 "rsh_unsigned",
466 "trace",
467 "trace_quick",
468 "log_not",
469 "bit_and",
470 "bit_or",
471 "bit_xor",
472 "bit_not",
473 "equal",
474 "less_signed",
475 "less_unsigned",
476 "ext",
477 "ref8",
478 "ref16",
479 "ref32",
480 "ref64",
481 "ref_float",
482 "ref_double",
483 "ref_long_double",
484 "l_to_d",
485 "d_to_l",
486 "if_goto",
487 "goto",
488 "const8",
489 "const16",
490 "const32",
491 "const64",
492 "reg",
493 "end",
494 "dup",
495 "pop",
496 "zero_ext",
497 "swap",
498 "getv",
499 "setv",
500 "tracev",
501 "?undef?",
502 "trace16",
503 };
504
505struct agent_expr
506{
507 int length;
508
509 unsigned char *bytes;
510};
511
512/* Base action. Concrete actions inherit this. */
513
514struct tracepoint_action
515{
516 char type;
517};
518
519/* An 'M' (collect memory) action. */
520struct collect_memory_action
521{
522 struct tracepoint_action base;
523
524 ULONGEST addr;
525 ULONGEST len;
526 int basereg;
527};
528
529/* An 'R' (collect registers) action. */
530
531struct collect_registers_action
532{
533 struct tracepoint_action base;
534};
535
536/* An 'X' (evaluate expression) action. */
537
538struct eval_expr_action
539{
540 struct tracepoint_action base;
541
542 struct agent_expr *expr;
543};
544
219f2f23
PA
545/* This structure describes a piece of the source-level definition of
546 the tracepoint. The contents are not interpreted by the target,
547 but preserved verbatim for uploading upon reconnection. */
548
549struct source_string
550{
551 /* The type of string, such as "cond" for a conditional. */
552 char *type;
553
554 /* The source-level string itself. For the sake of target
555 debugging, we store it in plaintext, even though it is always
556 transmitted in hex. */
557 char *str;
558
559 /* Link to the next one in the list. We link them in the order
560 received, in case some make up an ordered list of commands or
561 some such. */
562 struct source_string *next;
563};
564
fa593d66
PA
565enum tracepoint_type
566{
567 /* Trap based tracepoint. */
568 trap_tracepoint,
569
570 /* A fast tracepoint implemented with a jump instead of a trap. */
571 fast_tracepoint,
572};
573
219f2f23
PA
574struct tracepoint_hit_ctx;
575
6a271cae
PA
576typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
577 ULONGEST *);
578
219f2f23
PA
579/* The definition of a tracepoint. */
580
581/* Tracepoints may have multiple locations, each at a different
582 address. This can occur with optimizations, template
583 instantiation, etc. Since the locations may be in different
584 scopes, the conditions and actions may be different for each
585 location. Our target version of tracepoints is more like GDB's
586 notion of "breakpoint locations", but we have almost nothing that
587 is not per-location, so we bother having two kinds of objects. The
588 key consequence is that numbers are not unique, and that it takes
589 both number and address to identify a tracepoint uniquely. */
590
591struct tracepoint
592{
593 /* The number of the tracepoint, as specified by GDB. Several
594 tracepoint objects here may share a number. */
595 int number;
596
597 /* Address at which the tracepoint is supposed to trigger. Several
598 tracepoints may share an address. */
599 CORE_ADDR address;
600
fa593d66
PA
601 /* Tracepoint type. */
602 enum tracepoint_type type;
603
219f2f23
PA
604 /* True if the tracepoint is currently enabled. */
605 int enabled;
606
607 /* The number of single steps that will be performed after each
608 tracepoint hit. */
609 long step_count;
610
611 /* The number of times the tracepoint may be hit before it will
612 terminate the entire tracing run. */
613 long pass_count;
614
615 /* Pointer to the agent expression that is the tracepoint's
616 conditional, or NULL if the tracepoint is unconditional. */
617 struct agent_expr *cond;
618
619 /* The list of actions to take when the tracepoint triggers. */
620 int numactions;
621 struct tracepoint_action **actions;
219f2f23
PA
622
623 /* Count of the times we've hit this tracepoint during the run.
624 Note that while-stepping steps are not counted as "hits". */
625 long hit_count;
626
6a271cae
PA
627 CORE_ADDR compiled_cond;
628
fa593d66
PA
629 /* Link to the next tracepoint in the list. */
630 struct tracepoint *next;
631
632#ifndef IN_PROCESS_AGENT
633 /* The list of actions to take when the tracepoint triggers, in
634 string/packet form. */
635 char **actions_str;
636
219f2f23
PA
637 /* The collection of strings that describe the tracepoint as it was
638 entered into GDB. These are not used by the target, but are
639 reported back to GDB upon reconnection. */
640 struct source_string *source_strings;
641
fa593d66
PA
642 /* The number of bytes displaced by fast tracepoints. It may subsume
643 multiple instructions, for multi-byte fast tracepoints. This
644 field is only valid for fast tracepoints. */
645 int orig_size;
646
647 /* Only for fast tracepoints. */
648 CORE_ADDR obj_addr_on_target;
649
650 /* Address range where the original instruction under a fast
651 tracepoint was relocated to. (_end is actually one byte past
652 the end). */
653 CORE_ADDR adjusted_insn_addr;
654 CORE_ADDR adjusted_insn_addr_end;
655
656 /* The address range of the piece of the jump pad buffer that was
657 assigned to this fast tracepoint. (_end is actually one byte
658 past the end).*/
659 CORE_ADDR jump_pad;
660 CORE_ADDR jump_pad_end;
661
662 /* The list of actions to take while in a stepping loop. These
663 fields are only valid for patch-based tracepoints. */
664 int num_step_actions;
665 struct tracepoint_action **step_actions;
666 /* Same, but in string/packet form. */
667 char **step_actions_str;
668
669 /* Handle returned by the breakpoint or tracepoint module when we
670 inserted the trap or jump. NULL if we haven't inserted it
671 yet. */
219f2f23 672 void *handle;
fa593d66 673#endif
219f2f23 674
219f2f23
PA
675};
676
fa593d66
PA
677#ifndef IN_PROCESS_AGENT
678
219f2f23
PA
679/* Given `while-stepping', a thread may be collecting data for more
680 than one tracepoint simultaneously. On the other hand, the same
681 tracepoint with a while-stepping action may be hit by more than one
682 thread simultaneously (but not quite, each thread could be handling
683 a different step). Each thread holds a list of these objects,
684 representing the current step of each while-stepping action being
685 collected. */
686
687struct wstep_state
688{
689 struct wstep_state *next;
690
691 /* The tracepoint number. */
692 int tp_number;
693 /* The tracepoint's address. */
694 CORE_ADDR tp_address;
695
696 /* The number of the current step in this 'while-stepping'
697 action. */
698 long current_step;
699};
700
fa593d66
PA
701#endif
702
703/* The linked list of all tracepoints. Marked explicitly as used as
704 the in-process library doesn't use it for the fast tracepoints
705 support. */
706IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
219f2f23 707
fa593d66 708#ifndef IN_PROCESS_AGENT
219f2f23
PA
709
710/* Pointer to the last tracepoint in the list, new tracepoints are
711 linked in at the end. */
712
713static struct tracepoint *last_tracepoint;
fa593d66 714#endif
219f2f23
PA
715
716/* The first tracepoint to exceed its pass count. */
717
fa593d66 718IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
219f2f23
PA
719
720/* True if the trace buffer is full or otherwise no longer usable. */
721
fa593d66 722IP_AGENT_EXPORT int trace_buffer_is_full;
219f2f23
PA
723
724/* Enumeration of the different kinds of things that can happen during
725 agent expression evaluation. */
726
727enum eval_result_type
728 {
729 expr_eval_no_error,
730 expr_eval_empty_expression,
731 expr_eval_empty_stack,
732 expr_eval_stack_overflow,
733 expr_eval_stack_underflow,
734 expr_eval_unhandled_opcode,
735 expr_eval_unrecognized_opcode,
736 expr_eval_divide_by_zero,
737 expr_eval_invalid_goto
738 };
739
740static enum eval_result_type expr_eval_result = expr_eval_no_error;
741
fa593d66
PA
742#ifndef IN_PROCESS_AGENT
743
219f2f23
PA
744static const char *eval_result_names[] =
745 {
746 "terror:in the attic", /* this should never be reported */
747 "terror:empty expression",
748 "terror:empty stack",
749 "terror:stack overflow",
750 "terror:stack underflow",
751 "terror:unhandled opcode",
752 "terror:unrecognized opcode",
753 "terror:divide by zero"
754 };
755
fa593d66
PA
756#endif
757
219f2f23
PA
758/* The tracepoint in which the error occurred. */
759
760static struct tracepoint *error_tracepoint;
761
762struct trace_state_variable
763{
764 /* This is the name of the variable as used in GDB. The target
765 doesn't use the name, but needs to have it for saving and
766 reconnection purposes. */
767 char *name;
768
769 /* This number identifies the variable uniquely. Numbers may be
770 assigned either by the target (in the case of builtin variables),
771 or by GDB, and are presumed unique during the course of a trace
772 experiment. */
773 int number;
774
775 /* The variable's initial value, a 64-bit signed integer always. */
776 LONGEST initial_value;
777
778 /* The variable's value, a 64-bit signed integer always. */
779 LONGEST value;
780
781 /* Pointer to a getter function, used to supply computed values. */
782 LONGEST (*getter) (void);
783
784 /* Link to the next variable. */
785 struct trace_state_variable *next;
786};
787
788/* Linked list of all trace state variables. */
789
fa593d66
PA
790#ifdef IN_PROCESS_AGENT
791struct trace_state_variable *alloced_trace_state_variables;
792#endif
793
794IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
219f2f23
PA
795
796/* The results of tracing go into a fixed-size space known as the
797 "trace buffer". Because usage follows a limited number of
798 patterns, we manage it ourselves rather than with malloc. Basic
799 rules are that we create only one trace frame at a time, each is
800 variable in size, they are never moved once created, and we only
801 discard if we are doing a circular buffer, and then only the oldest
802 ones. Each trace frame includes its own size, so we don't need to
803 link them together, and the trace frame number is relative to the
804 first one, so we don't need to record numbers. A trace frame also
805 records the number of the tracepoint that created it. The data
806 itself is a series of blocks, each introduced by a single character
807 and with a defined format. Each type of block has enough
808 type/length info to allow scanners to jump quickly from one block
809 to the next without reading each byte in the block. */
810
811/* Trace buffer management would be simple - advance a free pointer
812 from beginning to end, then stop - were it not for the circular
813 buffer option, which is a useful way to prevent a trace run from
814 stopping prematurely because the buffer filled up. In the circular
815 case, the location of the first trace frame (trace_buffer_start)
816 moves as old trace frames are discarded. Also, since we grow trace
817 frames incrementally as actions are performed, we wrap around to
818 the beginning of the trace buffer. This is per-block, so each
819 block within a trace frame remains contiguous. Things get messy
820 when the wrapped-around trace frame is the one being discarded; the
821 free space ends up in two parts at opposite ends of the buffer. */
822
823#ifndef ATTR_PACKED
824# if defined(__GNUC__)
825# define ATTR_PACKED __attribute__ ((packed))
826# else
827# define ATTR_PACKED /* nothing */
828# endif
829#endif
830
831/* The data collected at a tracepoint hit. This object should be as
832 small as possible, since there may be a great many of them. We do
833 not need to keep a frame number, because they are all sequential
834 and there are no deletions; so the Nth frame in the buffer is
835 always frame number N. */
836
837struct traceframe
838{
839 /* Number of the tracepoint that collected this traceframe. A value
840 of 0 indicates the current end of the trace buffer. We make this
841 a 16-bit field because it's never going to happen that GDB's
842 numbering of tracepoints reaches 32,000. */
843 int tpnum : 16;
844
845 /* The size of the data in this trace frame. We limit this to 32
846 bits, even on a 64-bit target, because it's just implausible that
847 one is validly going to collect 4 gigabytes of data at a single
848 tracepoint hit. */
849 unsigned int data_size : 32;
850
851 /* The base of the trace data, which is contiguous from this point. */
852 unsigned char data[0];
853
fa593d66 854} ATTR_PACKED;
219f2f23
PA
855
856/* The traceframe to be used as the source of data to send back to
857 GDB. A value of -1 means to get data from the live program. */
858
859int current_traceframe = -1;
860
861/* This flag is true if the trace buffer is circular, meaning that
862 when it fills, the oldest trace frames are discarded in order to
863 make room. */
864
fa593d66 865#ifndef IN_PROCESS_AGENT
219f2f23 866static int circular_trace_buffer;
fa593d66 867#endif
219f2f23
PA
868
869/* Pointer to the block of memory that traceframes all go into. */
870
871static unsigned char *trace_buffer_lo;
872
873/* Pointer to the end of the trace buffer, more precisely to the byte
874 after the end of the buffer. */
875
876static unsigned char *trace_buffer_hi;
877
fa593d66
PA
878/* Control structure holding the read/write/etc. pointers into the
879 trace buffer. We need more than one of these to implement a
880 transaction-like mechanism to garantees that both GDBserver and the
881 in-process agent can try to change the trace buffer
882 simultaneously. */
883
884struct trace_buffer_control
885{
886 /* Pointer to the first trace frame in the buffer. In the
887 non-circular case, this is equal to trace_buffer_lo, otherwise it
888 moves around in the buffer. */
889 unsigned char *start;
890
891 /* Pointer to the free part of the trace buffer. Note that we clear
892 several bytes at and after this pointer, so that traceframe
893 scans/searches terminate properly. */
894 unsigned char *free;
895
896 /* Pointer to the byte after the end of the free part. Note that
897 this may be smaller than trace_buffer_free in the circular case,
898 and means that the free part is in two pieces. Initially it is
899 equal to trace_buffer_hi, then is generally equivalent to
900 trace_buffer_start. */
901 unsigned char *end_free;
902
903 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
904 this is the point at which the trace data breaks, and resumes at
905 trace_buffer_lo. */
906 unsigned char *wrap;
907};
908
909/* Same as above, to be used by GDBserver when updating the in-process
910 agent. */
911struct ipa_trace_buffer_control
912{
913 uintptr_t start;
914 uintptr_t free;
915 uintptr_t end_free;
916 uintptr_t wrap;
917};
918
919
920/* We have possibly both GDBserver and an inferior thread accessing
921 the same IPA trace buffer memory. The IPA is the producer (tries
922 to put new frames in the buffer), while GDBserver occasionally
923 consumes them, that is, flushes the IPA's buffer into its own
924 buffer. Both sides need to update the trace buffer control
925 pointers (current head, tail, etc.). We can't use a global lock to
926 synchronize the accesses, as otherwise we could deadlock GDBserver
927 (if the thread holding the lock stops for a signal, say). So
928 instead of that, we use a transaction scheme where GDBserver writes
929 always prevail over the IPAs writes, and, we have the IPA detect
930 the commit failure/overwrite, and retry the whole attempt. This is
931 mainly implemented by having a global token object that represents
932 who wrote last to the buffer control structure. We need to freeze
933 any inferior writing to the buffer while GDBserver touches memory,
934 so that the inferior can correctly detect that GDBserver had been
935 there, otherwise, it could mistakingly think its commit was
936 successful; that's implemented by simply having GDBserver set a
937 breakpoint the inferior hits if it is the critical region.
938
939 There are three cycling trace buffer control structure copies
940 (buffer head, tail, etc.), with the token object including an index
941 indicating which is current live copy. The IPA tentatively builds
942 an updated copy in a non-current control structure, while GDBserver
943 always clobbers the current version directly. The IPA then tries
944 to atomically "commit" its version; if GDBserver clobbered the
945 structure meanwhile, that will fail, and the IPA restarts the
946 allocation process.
947
948 Listing the step in further detail, we have:
949
950 In-process agent (producer):
951
952 - passes by `about_to_request_buffer_space' breakpoint/lock
953
954 - reads current token, extracts current trace buffer control index,
955 and starts tentatively updating the rightmost one (0->1, 1->2,
956 2->0). Note that only one inferior thread is executing this code
957 at any given time, due to an outer lock in the jump pads.
219f2f23 958
fa593d66 959 - updates counters, and tries to commit the token.
219f2f23 960
fa593d66
PA
961 - passes by second `about_to_request_buffer_space' breakpoint/lock,
962 leaving the sync region.
219f2f23 963
fa593d66 964 - checks if the update was effective.
219f2f23 965
fa593d66
PA
966 - if trace buffer was found full, hits flush_trace_buffer
967 breakpoint, and restarts later afterwards.
219f2f23 968
fa593d66 969 GDBserver (consumer):
219f2f23 970
fa593d66
PA
971 - sets `about_to_request_buffer_space' breakpoint/lock.
972
973 - updates the token unconditionally, using the current buffer
974 control index, since it knows that the IP agent always writes to
975 the rightmost, and due to the breakpoint, at most one IP thread
976 can try to update the trace buffer concurrently to GDBserver, so
977 there will be no danger of trace buffer control index wrap making
978 the IPA write to the same index as GDBserver.
979
980 - flushes the IP agent's trace buffer completely, and updates the
981 current trace buffer control structure. GDBserver *always* wins.
982
983 - removes the `about_to_request_buffer_space' breakpoint.
984
985The token is stored in the `trace_buffer_ctrl_curr' variable.
986Internally, it's bits are defined as:
987
988 |-------------+-----+-------------+--------+-------------+--------------|
989 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
990 |-------------+-----+-------------+--------+-------------+--------------|
991 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
992 |-------------+-----+-------------+--------+-------------+--------------|
993
994 GSB - GDBserver Stamp Bit
995 PC - Previous Counter
996 CC - Current Counter
997 TBCI - Trace Buffer Control Index
998
999
1000An IPA update of `trace_buffer_ctrl_curr' does:
1001
1002 - read CC from the current token, save as PC.
1003 - updates pointers
1004 - atomically tries to write PC+1,CC
1005
1006A GDBserver update of `trace_buffer_ctrl_curr' does:
1007
1008 - reads PC and CC from the current token.
1009 - updates pointers
1010 - writes GSB,PC,CC
1011*/
1012
1013/* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1014 for the counters described below. The cleared bits are used to
1015 hold the index of the items of the `trace_buffer_ctrl' array that
1016 is "current". */
1017#define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1018
1019/* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1020 counter, and the `current' counter. */
1021
1022#define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1023#define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1024
1025/* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1026 always stamps this bit as set. */
1027#define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1028
1029#ifdef IN_PROCESS_AGENT
1030IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
1031IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
1032
1033# define TRACE_BUFFER_CTRL_CURR \
1034 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1035
1036#else
1037
1038/* The GDBserver side agent only needs one instance of this object, as
1039 it doesn't need to sync with itself. Define it as array anyway so
1040 that the rest of the code base doesn't need to care for the
1041 difference. */
1042struct trace_buffer_control trace_buffer_ctrl[1];
1043# define TRACE_BUFFER_CTRL_CURR 0
1044#endif
1045
1046/* These are convenience macros used to access the current trace
1047 buffer control in effect. */
1048#define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1049#define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1050#define trace_buffer_end_free \
1051 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1052#define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
219f2f23 1053
219f2f23
PA
1054
1055/* Macro that returns a pointer to the first traceframe in the buffer. */
1056
1057#define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1058
1059/* Macro that returns a pointer to the next traceframe in the buffer.
1060 If the computed location is beyond the wraparound point, subtract
1061 the offset of the wraparound. */
1062
1063#define NEXT_TRACEFRAME_1(TF) \
1064 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1065
1066#define NEXT_TRACEFRAME(TF) \
1067 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1068 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1069 ? (trace_buffer_wrap - trace_buffer_lo) \
1070 : 0)))
1071
1072/* The difference between these counters represents the total number
fa593d66
PA
1073 of complete traceframes present in the trace buffer. The IP agent
1074 writes to the write count, GDBserver writes to read count. */
219f2f23 1075
fa593d66
PA
1076IP_AGENT_EXPORT unsigned int traceframe_write_count;
1077IP_AGENT_EXPORT unsigned int traceframe_read_count;
219f2f23
PA
1078
1079/* Convenience macro. */
1080
1081#define traceframe_count \
1082 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1083
1084/* The count of all traceframes created in the current run, including
1085 ones that were discarded to make room. */
1086
fa593d66
PA
1087IP_AGENT_EXPORT int traceframes_created;
1088
1089#ifndef IN_PROCESS_AGENT
219f2f23
PA
1090
1091/* Read-only regions are address ranges whose contents don't change,
1092 and so can be read from target memory even while looking at a trace
1093 frame. Without these, disassembly for instance will likely fail,
1094 because the program code is not usually collected into a trace
1095 frame. This data structure does not need to be very complicated or
1096 particularly efficient, it's only going to be used occasionally,
1097 and only by some commands. */
1098
1099struct readonly_region
1100{
1101 /* The bounds of the region. */
1102 CORE_ADDR start, end;
1103
1104 /* Link to the next one. */
1105 struct readonly_region *next;
1106};
1107
1108/* Linked list of readonly regions. This list stays in effect from
1109 one tstart to the next. */
1110
1111static struct readonly_region *readonly_regions;
1112
fa593d66
PA
1113#endif
1114
219f2f23
PA
1115/* The global that controls tracing overall. */
1116
fa593d66
PA
1117IP_AGENT_EXPORT int tracing;
1118
1119#ifndef IN_PROCESS_AGENT
8336d594
PA
1120
1121/* Controls whether tracing should continue after GDB disconnects. */
1122
1123int disconnected_tracing;
219f2f23
PA
1124
1125/* The reason for the last tracing run to have stopped. We initialize
1126 to a distinct string so that GDB can distinguish between "stopped
1127 after running" and "stopped because never run" cases. */
1128
1129static const char *tracing_stop_reason = "tnotrun";
1130
1131static int tracing_stop_tpnum;
1132
fa593d66
PA
1133#endif
1134
219f2f23
PA
1135/* Functions local to this file. */
1136
1137/* Base "class" for tracepoint type specific data to be passed down to
fa593d66 1138 collect_data_at_tracepoint. */
219f2f23
PA
1139struct tracepoint_hit_ctx
1140{
fa593d66 1141 enum tracepoint_type type;
219f2f23
PA
1142};
1143
fa593d66
PA
1144#ifdef IN_PROCESS_AGENT
1145
1146/* Fast/jump tracepoint specific data to be passed down to
219f2f23 1147 collect_data_at_tracepoint. */
fa593d66
PA
1148struct fast_tracepoint_ctx
1149{
1150 struct tracepoint_hit_ctx base;
1151
1152 struct regcache regcache;
1153 int regcache_initted;
1154 unsigned char *regspace;
1155
1156 unsigned char *regs;
1157 struct tracepoint *tpoint;
1158};
219f2f23 1159
fa593d66
PA
1160#else
1161
1162/* Static tracepoint specific data to be passed down to
1163 collect_data_at_tracepoint. */
219f2f23
PA
1164struct trap_tracepoint_ctx
1165{
1166 struct tracepoint_hit_ctx base;
1167
1168 struct regcache *regcache;
1169};
1170
fa593d66
PA
1171#endif
1172
1173#ifndef IN_PROCESS_AGENT
219f2f23
PA
1174static struct agent_expr *parse_agent_expr (char **actparm);
1175static char *unparse_agent_expr (struct agent_expr *aexpr);
fa593d66 1176#endif
219f2f23
PA
1177static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx,
1178 struct traceframe *tframe,
1179 struct agent_expr *aexpr,
1180 ULONGEST *rslt);
1181
1182static int agent_mem_read (struct traceframe *tframe,
1183 unsigned char *to, CORE_ADDR from, ULONGEST len);
1184static int agent_tsv_read (struct traceframe *tframe, int n);
1185
fa593d66 1186#ifndef IN_PROCESS_AGENT
219f2f23
PA
1187static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1188static int traceframe_read_tsv (int num, LONGEST *val);
fa593d66 1189#endif
219f2f23
PA
1190
1191static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1192 struct tracepoint *tpoint);
1193
fa593d66 1194#ifndef IN_PROCESS_AGENT
219f2f23
PA
1195static void clear_readonly_regions (void);
1196static void clear_installed_tracepoints (void);
fa593d66 1197#endif
219f2f23
PA
1198
1199static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1200 CORE_ADDR stop_pc,
1201 struct tracepoint *tpoint);
fa593d66 1202#ifndef IN_PROCESS_AGENT
219f2f23
PA
1203static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1204 CORE_ADDR stop_pc,
1205 struct tracepoint *tpoint, int current_step);
6a271cae
PA
1206static void compile_tracepoint_condition (struct tracepoint *tpoint,
1207 CORE_ADDR *jump_entry);
fa593d66 1208#endif
219f2f23
PA
1209static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1210 CORE_ADDR stop_pc,
1211 struct tracepoint *tpoint,
1212 struct traceframe *tframe,
1213 struct tracepoint_action *taction);
1214
fa593d66
PA
1215#ifndef IN_PROCESS_AGENT
1216static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1217#endif
1218
1219#if defined(__GNUC__)
1220# define memory_barrier() asm volatile ("" : : : "memory")
1221#else
1222# define memory_barrier() do {} while (0)
1223#endif
1224
1225/* We only build the IPA if this builtin is supported, and there are
1226 no uses of this in GDBserver itself, so we're safe in defining this
1227 unconditionally. */
1228#define cmpxchg(mem, oldval, newval) \
1229 __sync_val_compare_and_swap (mem, oldval, newval)
1230
219f2f23
PA
1231/* Record that an error occurred during expression evaluation. */
1232
1233static void
1234record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1235 enum eval_result_type rtype)
1236{
1237 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1238 tpoint->number, paddress (tpoint->address), which, rtype);
1239
fa593d66
PA
1240#ifdef IN_PROCESS_AGENT
1241 /* Only record the first error we get. */
1242 if (cmpxchg (&expr_eval_result,
1243 expr_eval_no_error,
1244 rtype) != expr_eval_no_error)
1245 return;
1246#else
1247 if (expr_eval_result != expr_eval_no_error)
1248 return;
1249#endif
1250
219f2f23
PA
1251 error_tracepoint = tpoint;
1252}
1253
1254/* Trace buffer management. */
1255
1256static void
1257clear_trace_buffer (void)
1258{
1259 trace_buffer_start = trace_buffer_lo;
1260 trace_buffer_free = trace_buffer_lo;
1261 trace_buffer_end_free = trace_buffer_hi;
1262 trace_buffer_wrap = trace_buffer_hi;
1263 /* A traceframe with zeroed fields marks the end of trace data. */
1264 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1265 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1266 traceframe_read_count = traceframe_write_count = 0;
1267 traceframes_created = 0;
1268}
1269
fa593d66
PA
1270#ifndef IN_PROCESS_AGENT
1271
1272static void
1273clear_inferior_trace_buffer (void)
1274{
1275 CORE_ADDR ipa_trace_buffer_lo;
1276 CORE_ADDR ipa_trace_buffer_hi;
1277 struct traceframe ipa_traceframe = { 0 };
1278 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1279
1280 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1281 &ipa_trace_buffer_lo);
1282 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1283 &ipa_trace_buffer_hi);
1284
1285 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1286 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1287 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1288 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1289
1290 /* A traceframe with zeroed fields marks the end of trace data. */
1291 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1292 (unsigned char *) &ipa_trace_buffer_ctrl,
1293 sizeof (ipa_trace_buffer_ctrl));
1294
1295 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1296
1297 /* A traceframe with zeroed fields marks the end of trace data. */
1298 write_inferior_memory (ipa_trace_buffer_lo,
1299 (unsigned char *) &ipa_traceframe,
1300 sizeof (ipa_traceframe));
1301
1302 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1303 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1304 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1305}
1306
1307#endif
1308
219f2f23
PA
1309static void
1310init_trace_buffer (unsigned char *buf, int bufsize)
1311{
1312 trace_buffer_lo = buf;
1313 trace_buffer_hi = trace_buffer_lo + bufsize;
1314
1315 clear_trace_buffer ();
1316}
1317
fa593d66
PA
1318#ifdef IN_PROCESS_AGENT
1319
1320IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1321about_to_request_buffer_space (void)
1322{
1323 /* GDBserver places breakpoint here while it goes about to flush
1324 data at random times. */
1325 UNKNOWN_SIDE_EFFECTS();
1326}
1327
1328#endif
1329
219f2f23
PA
1330/* Carve out a piece of the trace buffer, returning NULL in case of
1331 failure. */
1332
1333static void *
1334trace_buffer_alloc (size_t amt)
1335{
1336 unsigned char *rslt;
fa593d66
PA
1337 struct trace_buffer_control *tbctrl;
1338 unsigned int curr;
1339#ifdef IN_PROCESS_AGENT
1340 unsigned int prev, prev_filtered;
1341 unsigned int commit_count;
1342 unsigned int commit;
1343 unsigned int readout;
1344#else
219f2f23
PA
1345 struct traceframe *oldest;
1346 unsigned char *new_start;
fa593d66 1347#endif
219f2f23
PA
1348
1349 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1350 (long) amt, (long) sizeof (struct traceframe));
1351
1352 /* Account for the EOB marker. */
1353 amt += sizeof (struct traceframe);
1354
fa593d66
PA
1355#ifdef IN_PROCESS_AGENT
1356 again:
1357 memory_barrier ();
1358
1359 /* Read the current token and extract the index to try to write to,
1360 storing it in CURR. */
1361 prev = trace_buffer_ctrl_curr;
1362 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1363 curr = prev_filtered + 1;
1364 if (curr > 2)
1365 curr = 0;
1366
1367 about_to_request_buffer_space ();
1368
1369 /* Start out with a copy of the current state. GDBserver may be
1370 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1371 be able to commit anyway if that happens. */
1372 trace_buffer_ctrl[curr]
1373 = trace_buffer_ctrl[prev_filtered];
1374 trace_debug ("trying curr=%u", curr);
1375#else
1376 /* The GDBserver's agent doesn't need all that syncing, and always
1377 updates TCB 0 (there's only one, mind you). */
1378 curr = 0;
1379#endif
1380 tbctrl = &trace_buffer_ctrl[curr];
1381
219f2f23
PA
1382 /* Offsets are easier to grok for debugging than raw addresses,
1383 especially for the small trace buffer sizes that are useful for
1384 testing. */
fa593d66
PA
1385 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1386 curr,
1387 (int) (tbctrl->start - trace_buffer_lo),
1388 (int) (tbctrl->free - trace_buffer_lo),
1389 (int) (tbctrl->end_free - trace_buffer_lo),
1390 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1391 (int) (trace_buffer_hi - trace_buffer_lo));
1392
1393 /* The algorithm here is to keep trying to get a contiguous block of
1394 the requested size, possibly discarding older traceframes to free
1395 up space. Since free space might come in one or two pieces,
1396 depending on whether discarded traceframes wrapped around at the
1397 high end of the buffer, we test both pieces after each
1398 discard. */
1399 while (1)
1400 {
1401 /* First, if we have two free parts, try the upper one first. */
fa593d66 1402 if (tbctrl->end_free < tbctrl->free)
219f2f23 1403 {
fa593d66 1404 if (tbctrl->free + amt <= trace_buffer_hi)
219f2f23
PA
1405 /* We have enough in the upper part. */
1406 break;
1407 else
1408 {
1409 /* Our high part of free space wasn't enough. Give up
1410 on it for now, set wraparound. We will recover the
1411 space later, if/when the wrapped-around traceframe is
1412 discarded. */
1413 trace_debug ("Upper part too small, setting wraparound");
fa593d66
PA
1414 tbctrl->wrap = tbctrl->free;
1415 tbctrl->free = trace_buffer_lo;
219f2f23
PA
1416 }
1417 }
1418
1419 /* The normal case. */
fa593d66 1420 if (tbctrl->free + amt <= tbctrl->end_free)
219f2f23
PA
1421 break;
1422
fa593d66
PA
1423#ifdef IN_PROCESS_AGENT
1424 /* The IP Agent's buffer is always circular. It isn't used
1425 currently, but `circular_trace_buffer' could represent
1426 GDBserver's mode. If we didn't find space, ask GDBserver to
1427 flush. */
1428
1429 flush_trace_buffer ();
1430 memory_barrier ();
1431 if (tracing)
1432 {
1433 trace_debug ("gdbserver flushed buffer, retrying");
1434 goto again;
1435 }
1436
1437 /* GDBserver cancelled the tracing. Bail out as well. */
1438 return NULL;
1439#else
219f2f23
PA
1440 /* If we're here, then neither part is big enough, and
1441 non-circular trace buffers are now full. */
1442 if (!circular_trace_buffer)
1443 {
1444 trace_debug ("Not enough space in the trace buffer");
1445 return NULL;
1446 }
1447
1448 trace_debug ("Need more space in the trace buffer");
1449
1450 /* If we have a circular buffer, we can try discarding the
1451 oldest traceframe and see if that helps. */
1452 oldest = FIRST_TRACEFRAME ();
1453 if (oldest->tpnum == 0)
1454 {
1455 /* Not good; we have no traceframes to free. Perhaps we're
1456 asking for a block that is larger than the buffer? In
1457 any case, give up. */
1458 trace_debug ("No traceframes to discard");
1459 return NULL;
1460 }
1461
fa593d66
PA
1462 /* We don't run this code in the in-process agent currently.
1463 E.g., we could leave the in-process agent in autonomous
1464 circular mode if we only have fast tracepoints. If we do
1465 that, then this bit becomes racy with GDBserver, which also
1466 writes to this counter. */
219f2f23
PA
1467 --traceframe_write_count;
1468
1469 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1470 /* If we freed the traceframe that wrapped around, go back
1471 to the non-wrap case. */
fa593d66 1472 if (new_start < tbctrl->start)
219f2f23
PA
1473 {
1474 trace_debug ("Discarding past the wraparound");
fa593d66 1475 tbctrl->wrap = trace_buffer_hi;
219f2f23 1476 }
fa593d66
PA
1477 tbctrl->start = new_start;
1478 tbctrl->end_free = tbctrl->start;
219f2f23
PA
1479
1480 trace_debug ("Discarded a traceframe\n"
fa593d66
PA
1481 "Trace buffer [%d], start=%d free=%d "
1482 "endfree=%d wrap=%d hi=%d",
1483 curr,
1484 (int) (tbctrl->start - trace_buffer_lo),
1485 (int) (tbctrl->free - trace_buffer_lo),
1486 (int) (tbctrl->end_free - trace_buffer_lo),
1487 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1488 (int) (trace_buffer_hi - trace_buffer_lo));
1489
1490 /* Now go back around the loop. The discard might have resulted
1491 in either one or two pieces of free space, so we want to try
1492 both before freeing any more traceframes. */
fa593d66 1493#endif
219f2f23
PA
1494 }
1495
1496 /* If we get here, we know we can provide the asked-for space. */
1497
fa593d66 1498 rslt = tbctrl->free;
219f2f23
PA
1499
1500 /* Adjust the request back down, now that we know we have space for
fa593d66
PA
1501 the marker, but don't commit to AMT yet, we may still need to
1502 restart the operation if GDBserver touches the trace buffer
1503 (obviously only important in the in-process agent's version). */
1504 tbctrl->free += (amt - sizeof (struct traceframe));
1505
1506 /* Or not. If GDBserver changed the trace buffer behind our back,
1507 we get to restart a new allocation attempt. */
1508
1509#ifdef IN_PROCESS_AGENT
1510 /* Build the tentative token. */
1511 commit_count = (((prev & 0x0007ff00) + 0x100) & 0x0007ff00);
1512 commit = (((prev & 0x0007ff00) << 12)
1513 | commit_count
1514 | curr);
1515
1516 /* Try to commit it. */
1517 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1518 if (readout != prev)
1519 {
1520 trace_debug ("GDBserver has touched the trace buffer, restarting."
1521 " (prev=%08x, commit=%08x, readout=%08x)",
1522 prev, commit, readout);
1523 goto again;
1524 }
219f2f23 1525
fa593d66
PA
1526 /* Hold your horses here. Even if that change was committed,
1527 GDBserver could come in, and clobber it. We need to hold to be
1528 able to tell if GDBserver clobbers before or after we committed
1529 the change. Whenever GDBserver goes about touching the IPA
1530 buffer, it sets a breakpoint in this routine, so we have a sync
1531 point here. */
1532 about_to_request_buffer_space ();
219f2f23 1533
fa593d66
PA
1534 /* Check if the change has been effective, even if GDBserver stopped
1535 us at the breakpoint. */
219f2f23 1536
fa593d66
PA
1537 {
1538 unsigned int refetch;
219f2f23 1539
fa593d66
PA
1540 memory_barrier ();
1541
1542 refetch = trace_buffer_ctrl_curr;
1543
1544 if ((refetch == commit
1545 || ((refetch & 0x7ff00000) >> 12) == commit_count))
1546 {
1547 /* effective */
1548 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1549 "readout=%08x, refetch=%08x)",
1550 prev, commit, readout, refetch);
1551 }
1552 else
1553 {
1554 trace_debug ("GDBserver has touched the trace buffer, not effective."
1555 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1556 prev, commit, readout, refetch);
1557 goto again;
1558 }
1559 }
1560#endif
1561
1562 /* We have a new piece of the trace buffer. Hurray! */
1563
1564 /* Add an EOB marker just past this allocation. */
1565 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1566 ((struct traceframe *) tbctrl->free)->data_size = 0;
1567
1568 /* Adjust the request back down, now that we know we have space for
1569 the marker. */
1570 amt -= sizeof (struct traceframe);
1571
1572 if (debug_threads)
1573 {
219f2f23 1574 trace_debug ("Allocated %d bytes", (int) amt);
fa593d66
PA
1575 trace_debug ("Trace buffer [%d] start=%d free=%d "
1576 "endfree=%d wrap=%d hi=%d",
1577 curr,
1578 (int) (tbctrl->start - trace_buffer_lo),
1579 (int) (tbctrl->free - trace_buffer_lo),
1580 (int) (tbctrl->end_free - trace_buffer_lo),
1581 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1582 (int) (trace_buffer_hi - trace_buffer_lo));
1583 }
1584
1585 return rslt;
1586}
1587
fa593d66
PA
1588#ifndef IN_PROCESS_AGENT
1589
219f2f23
PA
1590/* Return the total free space. This is not necessarily the largest
1591 block we can allocate, because of the two-part case. */
1592
1593static int
1594free_space (void)
1595{
1596 if (trace_buffer_free <= trace_buffer_end_free)
1597 return trace_buffer_end_free - trace_buffer_free;
1598 else
1599 return ((trace_buffer_end_free - trace_buffer_lo)
1600 + (trace_buffer_hi - trace_buffer_free));
1601}
1602
1603/* An 'S' in continuation packets indicates remainder are for
1604 while-stepping. */
1605
1606static int seen_step_action_flag;
1607
1608/* Create a tracepoint (location) with given number and address. */
1609
1610static struct tracepoint *
1611add_tracepoint (int num, CORE_ADDR addr)
1612{
1613 struct tracepoint *tpoint;
1614
1615 tpoint = xmalloc (sizeof (struct tracepoint));
1616 tpoint->number = num;
1617 tpoint->address = addr;
1618 tpoint->numactions = 0;
1619 tpoint->actions = NULL;
1620 tpoint->actions_str = NULL;
1621 tpoint->cond = NULL;
1622 tpoint->num_step_actions = 0;
1623 tpoint->step_actions = NULL;
1624 tpoint->step_actions_str = NULL;
fa593d66
PA
1625 /* Start all off as regular (slow) tracepoints. */
1626 tpoint->type = trap_tracepoint;
1627 tpoint->orig_size = -1;
219f2f23 1628 tpoint->source_strings = NULL;
6a271cae 1629 tpoint->compiled_cond = 0;
219f2f23
PA
1630 tpoint->handle = NULL;
1631 tpoint->next = NULL;
1632
1633 if (!last_tracepoint)
1634 tracepoints = tpoint;
1635 else
1636 last_tracepoint->next = tpoint;
1637 last_tracepoint = tpoint;
1638
1639 seen_step_action_flag = 0;
1640
1641 return tpoint;
1642}
1643
fa593d66
PA
1644#ifndef IN_PROCESS_AGENT
1645
219f2f23
PA
1646/* Return the tracepoint with the given number and address, or NULL. */
1647
1648static struct tracepoint *
1649find_tracepoint (int id, CORE_ADDR addr)
1650{
1651 struct tracepoint *tpoint;
1652
1653 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1654 if (tpoint->number == id && tpoint->address == addr)
1655 return tpoint;
1656
1657 return NULL;
1658}
1659
1660/* There may be several tracepoints with the same number (because they
1661 are "locations", in GDB parlance); return the next one after the
1662 given tracepoint, or search from the beginning of the list if the
1663 first argument is NULL. */
1664
1665static struct tracepoint *
1666find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1667{
1668 struct tracepoint *tpoint;
1669
1670 if (prev_tp)
1671 tpoint = prev_tp->next;
1672 else
1673 tpoint = tracepoints;
1674 for (; tpoint; tpoint = tpoint->next)
1675 if (tpoint->number == num)
1676 return tpoint;
1677
1678 return NULL;
1679}
1680
fa593d66
PA
1681#endif
1682
219f2f23
PA
1683static char *
1684save_string (const char *str, size_t len)
1685{
1686 char *s;
1687
1688 s = xmalloc (len + 1);
1689 memcpy (s, str, len);
1690 s[len] = '\0';
1691
1692 return s;
1693}
1694
1695/* Append another action to perform when the tracepoint triggers. */
1696
1697static void
1698add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1699{
1700 char *act;
1701
1702 if (*packet == 'S')
1703 {
1704 seen_step_action_flag = 1;
1705 ++packet;
1706 }
1707
1708 act = packet;
1709
1710 while (*act)
1711 {
1712 char *act_start = act;
1713 struct tracepoint_action *action = NULL;
1714
1715 switch (*act)
1716 {
1717 case 'M':
1718 {
1719 struct collect_memory_action *maction;
1720 ULONGEST basereg;
1721 int is_neg;
1722
1723 maction = xmalloc (sizeof *maction);
1724 maction->base.type = *act;
1725 action = &maction->base;
1726
1727 ++act;
1728 is_neg = (*act == '-');
1729 if (*act == '-')
1730 ++act;
1731 act = unpack_varlen_hex (act, &basereg);
1732 ++act;
1733 act = unpack_varlen_hex (act, &maction->addr);
1734 ++act;
1735 act = unpack_varlen_hex (act, &maction->len);
1736 maction->basereg = (is_neg
1737 ? - (int) basereg
1738 : (int) basereg);
1739 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1740 pulongest (maction->len),
1741 paddress (maction->addr), maction->basereg);
1742 break;
1743 }
1744 case 'R':
1745 {
1746 struct collect_registers_action *raction;
1747
1748 raction = xmalloc (sizeof *raction);
1749 raction->base.type = *act;
1750 action = &raction->base;
1751
1752 trace_debug ("Want to collect registers");
1753 ++act;
1754 /* skip past hex digits of mask for now */
1755 while (isxdigit(*act))
1756 ++act;
1757 break;
1758 }
1759 case 'S':
1760 trace_debug ("Unexpected step action, ignoring");
1761 ++act;
1762 break;
1763 case 'X':
1764 {
1765 struct eval_expr_action *xaction;
1766
1767 xaction = xmalloc (sizeof (*xaction));
1768 xaction->base.type = *act;
1769 action = &xaction->base;
1770
1771 trace_debug ("Want to evaluate expression");
1772 xaction->expr = parse_agent_expr (&act);
1773 break;
1774 }
1775 default:
1776 trace_debug ("unknown trace action '%c', ignoring...", *act);
1777 break;
1778 case '-':
1779 break;
1780 }
1781
1782 if (action == NULL)
1783 break;
1784
1785 if (seen_step_action_flag)
1786 {
1787 tpoint->num_step_actions++;
1788
1789 tpoint->step_actions
1790 = xrealloc (tpoint->step_actions,
1791 (sizeof (*tpoint->step_actions)
1792 * tpoint->num_step_actions));
1793 tpoint->step_actions_str
1794 = xrealloc (tpoint->step_actions_str,
1795 (sizeof (*tpoint->step_actions_str)
1796 * tpoint->num_step_actions));
1797 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1798 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1799 = save_string (act_start, act - act_start);
1800 }
1801 else
1802 {
1803 tpoint->numactions++;
1804 tpoint->actions
1805 = xrealloc (tpoint->actions,
1806 sizeof (*tpoint->actions) * tpoint->numactions);
1807 tpoint->actions_str
1808 = xrealloc (tpoint->actions_str,
1809 sizeof (*tpoint->actions_str) * tpoint->numactions);
1810 tpoint->actions[tpoint->numactions - 1] = action;
1811 tpoint->actions_str[tpoint->numactions - 1]
1812 = save_string (act_start, act - act_start);
1813 }
1814 }
1815}
1816
fa593d66
PA
1817#endif
1818
219f2f23
PA
1819/* Find or create a trace state variable with the given number. */
1820
1821static struct trace_state_variable *
1822get_trace_state_variable (int num)
1823{
1824 struct trace_state_variable *tsv;
1825
fa593d66
PA
1826#ifdef IN_PROCESS_AGENT
1827 /* Search for an existing variable. */
1828 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1829 if (tsv->number == num)
1830 return tsv;
1831#endif
1832
219f2f23
PA
1833 /* Search for an existing variable. */
1834 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1835 if (tsv->number == num)
1836 return tsv;
1837
1838 return NULL;
1839}
1840
1841/* Find or create a trace state variable with the given number. */
1842
1843static struct trace_state_variable *
fa593d66 1844create_trace_state_variable (int num, int gdb)
219f2f23
PA
1845{
1846 struct trace_state_variable *tsv;
1847
1848 tsv = get_trace_state_variable (num);
1849 if (tsv != NULL)
1850 return tsv;
1851
1852 /* Create a new variable. */
1853 tsv = xmalloc (sizeof (struct trace_state_variable));
1854 tsv->number = num;
1855 tsv->initial_value = 0;
1856 tsv->value = 0;
1857 tsv->getter = NULL;
1858 tsv->name = NULL;
fa593d66
PA
1859#ifdef IN_PROCESS_AGENT
1860 if (!gdb)
1861 {
1862 tsv->next = alloced_trace_state_variables;
1863 alloced_trace_state_variables = tsv;
1864 }
1865 else
1866#endif
1867 {
1868 tsv->next = trace_state_variables;
1869 trace_state_variables = tsv;
1870 }
219f2f23
PA
1871 return tsv;
1872}
1873
6a271cae 1874IP_AGENT_EXPORT LONGEST
219f2f23
PA
1875get_trace_state_variable_value (int num)
1876{
1877 struct trace_state_variable *tsv;
1878
1879 tsv = get_trace_state_variable (num);
1880
1881 if (!tsv)
1882 {
1883 trace_debug ("No trace state variable %d, skipping value get", num);
1884 return 0;
1885 }
1886
1887 /* Call a getter function if we have one. While it's tempting to
1888 set up something to only call the getter once per tracepoint hit,
1889 it could run afoul of thread races. Better to let the getter
1890 handle it directly, if necessary to worry about it. */
1891 if (tsv->getter)
1892 tsv->value = (tsv->getter) ();
1893
1894 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1895 num, plongest (tsv->value));
1896
1897 return tsv->value;
1898}
1899
6a271cae 1900IP_AGENT_EXPORT void
219f2f23
PA
1901set_trace_state_variable_value (int num, LONGEST val)
1902{
1903 struct trace_state_variable *tsv;
1904
1905 tsv = get_trace_state_variable (num);
1906
1907 if (!tsv)
1908 {
1909 trace_debug ("No trace state variable %d, skipping value set", num);
1910 return;
1911 }
1912
1913 tsv->value = val;
1914}
1915
1916static void
1917set_trace_state_variable_name (int num, const char *name)
1918{
1919 struct trace_state_variable *tsv;
1920
1921 tsv = get_trace_state_variable (num);
1922
1923 if (!tsv)
1924 {
1925 trace_debug ("No trace state variable %d, skipping name set", num);
1926 return;
1927 }
1928
1929 tsv->name = (char *) name;
1930}
1931
1932static void
1933set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
1934{
1935 struct trace_state_variable *tsv;
1936
1937 tsv = get_trace_state_variable (num);
1938
1939 if (!tsv)
1940 {
1941 trace_debug ("No trace state variable %d, skipping getter set", num);
1942 return;
1943 }
1944
1945 tsv->getter = getter;
1946}
1947
1948/* Add a raw traceframe for the given tracepoint. */
1949
1950static struct traceframe *
1951add_traceframe (struct tracepoint *tpoint)
1952{
1953 struct traceframe *tframe;
1954
1955 tframe = trace_buffer_alloc (sizeof (struct traceframe));
1956
1957 if (tframe == NULL)
1958 return NULL;
1959
1960 tframe->tpnum = tpoint->number;
1961 tframe->data_size = 0;
1962
1963 return tframe;
1964}
1965
1966/* Add a block to the traceframe currently being worked on. */
1967
1968static unsigned char *
1969add_traceframe_block (struct traceframe *tframe, int amt)
1970{
1971 unsigned char *block;
1972
1973 if (!tframe)
1974 return NULL;
1975
1976 block = trace_buffer_alloc (amt);
1977
1978 if (!block)
1979 return NULL;
1980
1981 tframe->data_size += amt;
1982
1983 return block;
1984}
1985
1986/* Flag that the current traceframe is finished. */
1987
1988static void
1989finish_traceframe (struct traceframe *tframe)
1990{
1991 ++traceframe_write_count;
1992 ++traceframes_created;
1993}
1994
fa593d66
PA
1995#ifndef IN_PROCESS_AGENT
1996
219f2f23
PA
1997/* Given a traceframe number NUM, find the NUMth traceframe in the
1998 buffer. */
1999
2000static struct traceframe *
2001find_traceframe (int num)
2002{
2003 struct traceframe *tframe;
2004 int tfnum = 0;
2005
2006 for (tframe = FIRST_TRACEFRAME ();
2007 tframe->tpnum != 0;
2008 tframe = NEXT_TRACEFRAME (tframe))
2009 {
2010 if (tfnum == num)
2011 return tframe;
2012 ++tfnum;
2013 }
2014
2015 return NULL;
2016}
2017
2018static CORE_ADDR
2019get_traceframe_address (struct traceframe *tframe)
2020{
2021 CORE_ADDR addr;
2022 struct tracepoint *tpoint;
2023
2024 addr = traceframe_get_pc (tframe);
2025
2026 if (addr)
2027 return addr;
2028
2029 /* Fallback strategy, will be incorrect for while-stepping frames
2030 and multi-location tracepoints. */
2031 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2032 return tpoint->address;
2033}
2034
2035/* Search for the next traceframe whose address is inside or outside
2036 the given range. */
2037
2038static struct traceframe *
2039find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2040 int *tfnump)
2041{
2042 struct traceframe *tframe;
2043 CORE_ADDR tfaddr;
2044
2045 *tfnump = current_traceframe + 1;
2046 tframe = find_traceframe (*tfnump);
2047 /* The search is not supposed to wrap around. */
2048 if (!tframe)
2049 {
2050 *tfnump = -1;
2051 return NULL;
2052 }
2053
2054 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2055 {
2056 tfaddr = get_traceframe_address (tframe);
2057 if (inside_p
2058 ? (lo <= tfaddr && tfaddr <= hi)
2059 : (lo > tfaddr || tfaddr > hi))
2060 return tframe;
2061 ++*tfnump;
2062 }
2063
2064 *tfnump = -1;
2065 return NULL;
2066}
2067
2068/* Search for the next traceframe recorded by the given tracepoint.
2069 Note that for multi-location tracepoints, this will find whatever
2070 location appears first. */
2071
2072static struct traceframe *
2073find_next_traceframe_by_tracepoint (int num, int *tfnump)
2074{
2075 struct traceframe *tframe;
2076
2077 *tfnump = current_traceframe + 1;
2078 tframe = find_traceframe (*tfnump);
2079 /* The search is not supposed to wrap around. */
2080 if (!tframe)
2081 {
2082 *tfnump = -1;
2083 return NULL;
2084 }
2085
2086 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2087 {
2088 if (tframe->tpnum == num)
2089 return tframe;
2090 ++*tfnump;
2091 }
2092
2093 *tfnump = -1;
2094 return NULL;
2095}
2096
fa593d66
PA
2097#endif
2098
2099#ifndef IN_PROCESS_AGENT
2100
219f2f23
PA
2101/* Clear all past trace state. */
2102
2103static void
2104cmd_qtinit (char *packet)
2105{
2106 struct trace_state_variable *tsv, *prev, *next;
2107
2108 /* Make sure we don't try to read from a trace frame. */
2109 current_traceframe = -1;
2110
2111 trace_debug ("Initializing the trace");
2112
2113 clear_installed_tracepoints ();
2114 clear_readonly_regions ();
2115
2116 tracepoints = NULL;
2117 last_tracepoint = NULL;
2118
2119 /* Clear out any leftover trace state variables. Ones with target
2120 defined getters should be kept however. */
2121 prev = NULL;
2122 tsv = trace_state_variables;
2123 while (tsv)
2124 {
2125 trace_debug ("Looking at var %d", tsv->number);
2126 if (tsv->getter == NULL)
2127 {
2128 next = tsv->next;
2129 if (prev)
2130 prev->next = next;
2131 else
2132 trace_state_variables = next;
2133 trace_debug ("Deleting var %d", tsv->number);
2134 free (tsv);
2135 tsv = next;
2136 }
2137 else
2138 {
2139 prev = tsv;
2140 tsv = tsv->next;
2141 }
2142 }
2143
2144 clear_trace_buffer ();
fa593d66 2145 clear_inferior_trace_buffer ();
219f2f23
PA
2146
2147 write_ok (packet);
2148}
2149
2150/* Restore the program to its pre-tracing state. This routine may be called
2151 in error situations, so it needs to be careful about only restoring
2152 from known-valid bits. */
2153
2154static void
2155clear_installed_tracepoints (void)
2156{
2157 struct tracepoint *tpoint;
2158 struct tracepoint *prev_stpoint;
2159
7984d532
PA
2160 pause_all (1);
2161 cancel_breakpoints ();
2162
219f2f23
PA
2163 prev_stpoint = NULL;
2164
2165 /* Restore any bytes overwritten by tracepoints. */
2166 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2167 {
2168 if (!tpoint->enabled)
2169 continue;
2170
2171 /* Catch the case where we might try to remove a tracepoint that
2172 was never actually installed. */
2173 if (tpoint->handle == NULL)
2174 {
2175 trace_debug ("Tracepoint %d at 0x%s was "
2176 "never installed, nothing to clear",
2177 tpoint->number, paddress (tpoint->address));
2178 continue;
2179 }
2180
fa593d66
PA
2181 switch (tpoint->type)
2182 {
2183 case trap_tracepoint:
2184 delete_breakpoint (tpoint->handle);
2185 break;
2186 case fast_tracepoint:
2187 delete_fast_tracepoint_jump (tpoint->handle);
2188 break;
2189 }
2190
219f2f23
PA
2191 tpoint->handle = NULL;
2192 }
7984d532
PA
2193
2194 unpause_all (1);
219f2f23
PA
2195}
2196
2197/* Parse a packet that defines a tracepoint. */
2198
2199static void
2200cmd_qtdp (char *own_buf)
2201{
2202 int tppacket;
2203 ULONGEST num;
2204 ULONGEST addr;
2205 ULONGEST count;
2206 struct tracepoint *tpoint;
2207 char *actparm;
2208 char *packet = own_buf;
2209
2210 packet += strlen ("QTDP:");
2211
2212 /* A hyphen at the beginning marks a packet specifying actions for a
2213 tracepoint already supplied. */
2214 tppacket = 1;
2215 if (*packet == '-')
2216 {
2217 tppacket = 0;
2218 ++packet;
2219 }
2220 packet = unpack_varlen_hex (packet, &num);
2221 ++packet; /* skip a colon */
2222 packet = unpack_varlen_hex (packet, &addr);
2223 ++packet; /* skip a colon */
2224
2225 /* See if we already have this tracepoint. */
2226 tpoint = find_tracepoint (num, addr);
2227
2228 if (tppacket)
2229 {
2230 /* Duplicate tracepoints are never allowed. */
2231 if (tpoint)
2232 {
2233 trace_debug ("Tracepoint error: tracepoint %d"
2234 " at 0x%s already exists",
2235 (int) num, paddress (addr));
2236 write_enn (own_buf);
2237 return;
2238 }
2239
2240 tpoint = add_tracepoint (num, addr);
2241
2242 tpoint->enabled = (*packet == 'E');
2243 ++packet; /* skip 'E' */
2244 ++packet; /* skip a colon */
2245 packet = unpack_varlen_hex (packet, &count);
2246 tpoint->step_count = count;
2247 ++packet; /* skip a colon */
2248 packet = unpack_varlen_hex (packet, &count);
2249 tpoint->pass_count = count;
2250 /* See if we have any of the additional optional fields. */
2251 while (*packet == ':')
2252 {
2253 ++packet;
fa593d66
PA
2254 if (*packet == 'F')
2255 {
2256 tpoint->type = fast_tracepoint;
2257 ++packet;
2258 packet = unpack_varlen_hex (packet, &count);
2259 tpoint->orig_size = count;
2260 }
2261 else if (*packet == 'X')
219f2f23
PA
2262 {
2263 actparm = (char *) packet;
2264 tpoint->cond = parse_agent_expr (&actparm);
2265 packet = actparm;
2266 }
2267 else if (*packet == '-')
2268 break;
2269 else if (*packet == '\0')
2270 break;
2271 else
2272 trace_debug ("Unknown optional tracepoint field");
2273 }
2274 if (*packet == '-')
2275 trace_debug ("Also has actions\n");
2276
fa593d66 2277 trace_debug ("Defined %stracepoint %d at 0x%s, "
219f2f23 2278 "enabled %d step %ld pass %ld",
fa593d66
PA
2279 tpoint->type == fast_tracepoint ? "fast "
2280 : "",
2281 tpoint->number, paddress (tpoint->address), tpoint->enabled,
219f2f23
PA
2282 tpoint->step_count, tpoint->pass_count);
2283 }
2284 else if (tpoint)
2285 add_tracepoint_action (tpoint, packet);
2286 else
2287 {
2288 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2289 (int) num, paddress (addr));
2290 write_enn (own_buf);
2291 return;
2292 }
2293
2294 write_ok (own_buf);
2295}
2296
2297static void
2298cmd_qtdpsrc (char *own_buf)
2299{
2300 ULONGEST num, addr, start, slen;
2301 struct tracepoint *tpoint;
2302 char *packet = own_buf;
2303 char *saved, *srctype, *src;
2304 size_t nbytes;
2305 struct source_string *last, *newlast;
2306
2307 packet += strlen ("QTDPsrc:");
2308
2309 packet = unpack_varlen_hex (packet, &num);
2310 ++packet; /* skip a colon */
2311 packet = unpack_varlen_hex (packet, &addr);
2312 ++packet; /* skip a colon */
2313
2314 /* See if we already have this tracepoint. */
2315 tpoint = find_tracepoint (num, addr);
2316
2317 if (!tpoint)
2318 {
2319 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2320 (int) num, paddress (addr));
2321 write_enn (own_buf);
2322 return;
2323 }
2324
2325 saved = packet;
2326 packet = strchr (packet, ':');
2327 srctype = xmalloc (packet - saved + 1);
2328 memcpy (srctype, saved, packet - saved);
2329 srctype[packet - saved] = '\0';
2330 ++packet;
2331 packet = unpack_varlen_hex (packet, &start);
2332 ++packet; /* skip a colon */
2333 packet = unpack_varlen_hex (packet, &slen);
2334 ++packet; /* skip a colon */
2335 src = xmalloc (slen + 1);
2336 nbytes = unhexify (src, packet, strlen (packet) / 2);
2337 src[nbytes] = '\0';
2338
2339 newlast = xmalloc (sizeof (struct source_string));
2340 newlast->type = srctype;
2341 newlast->str = src;
2342 newlast->next = NULL;
2343 /* Always add a source string to the end of the list;
2344 this keeps sequences of actions/commands in the right
2345 order. */
2346 if (tpoint->source_strings)
2347 {
2348 for (last = tpoint->source_strings; last->next; last = last->next)
2349 ;
2350 last->next = newlast;
2351 }
2352 else
2353 tpoint->source_strings = newlast;
2354
2355 write_ok (own_buf);
2356}
2357
2358static void
2359cmd_qtdv (char *own_buf)
2360{
2361 ULONGEST num, val, builtin;
2362 char *varname;
2363 size_t nbytes;
2364 struct trace_state_variable *tsv;
2365 char *packet = own_buf;
2366
2367 packet += strlen ("QTDV:");
2368
2369 packet = unpack_varlen_hex (packet, &num);
2370 ++packet; /* skip a colon */
2371 packet = unpack_varlen_hex (packet, &val);
2372 ++packet; /* skip a colon */
2373 packet = unpack_varlen_hex (packet, &builtin);
2374 ++packet; /* skip a colon */
2375
2376 nbytes = strlen (packet) / 2;
2377 varname = xmalloc (nbytes + 1);
2378 nbytes = unhexify (varname, packet, nbytes);
2379 varname[nbytes] = '\0';
2380
fa593d66 2381 tsv = create_trace_state_variable (num, 1);
219f2f23
PA
2382 tsv->initial_value = (LONGEST) val;
2383 tsv->name = varname;
2384
2385 set_trace_state_variable_value (num, (LONGEST) val);
2386
2387 write_ok (own_buf);
2388}
2389
2390static void
2391cmd_qtv (char *own_buf)
2392{
2393 ULONGEST num;
2394 LONGEST val;
2395 int err;
2396 char *packet = own_buf;
2397
2398 packet += strlen ("qTV:");
2399 packet = unpack_varlen_hex (packet, &num);
2400
2401 if (current_traceframe >= 0)
2402 {
2403 err = traceframe_read_tsv ((int) num, &val);
2404 if (err)
2405 {
2406 strcpy (own_buf, "U");
2407 return;
2408 }
2409 }
2410 /* Only make tsv's be undefined before the first trace run. After a
2411 trace run is over, the user might want to see the last value of
2412 the tsv, and it might not be available in a traceframe. */
2413 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2414 {
2415 strcpy (own_buf, "U");
2416 return;
2417 }
2418 else
2419 val = get_trace_state_variable_value (num);
2420
2421 sprintf (own_buf, "V%s", phex_nz (val, 0));
2422}
2423
2424/* Clear out the list of readonly regions. */
2425
2426static void
2427clear_readonly_regions (void)
2428{
2429 struct readonly_region *roreg;
2430
2431 while (readonly_regions)
2432 {
2433 roreg = readonly_regions;
2434 readonly_regions = readonly_regions->next;
2435 free (roreg);
2436 }
2437}
2438
2439/* Parse the collection of address ranges whose contents GDB believes
2440 to be unchanging and so can be read directly from target memory
2441 even while looking at a traceframe. */
2442
2443static void
2444cmd_qtro (char *own_buf)
2445{
2446 ULONGEST start, end;
2447 struct readonly_region *roreg;
2448 char *packet = own_buf;
2449
2450 trace_debug ("Want to mark readonly regions");
2451
2452 clear_readonly_regions ();
2453
2454 packet += strlen ("QTro");
2455
2456 while (*packet == ':')
2457 {
2458 ++packet; /* skip a colon */
2459 packet = unpack_varlen_hex (packet, &start);
2460 ++packet; /* skip a comma */
2461 packet = unpack_varlen_hex (packet, &end);
2462 roreg = xmalloc (sizeof (struct readonly_region));
2463 roreg->start = start;
2464 roreg->end = end;
2465 roreg->next = readonly_regions;
2466 readonly_regions = roreg;
2467 trace_debug ("Added readonly region from 0x%s to 0x%s",
2468 paddress (roreg->start), paddress (roreg->end));
2469 }
2470
2471 write_ok (own_buf);
2472}
2473
2474/* Test to see if the given range is in our list of readonly ranges.
2475 We only test for being entirely within a range, GDB is not going to
2476 send a single memory packet that spans multiple regions. */
2477
2478int
2479in_readonly_region (CORE_ADDR addr, ULONGEST length)
2480{
2481 struct readonly_region *roreg;
2482
2483 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2484 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2485 return 1;
2486
2487 return 0;
2488}
2489
fa593d66
PA
2490/* The maximum size of a jump pad entry. */
2491static const int max_jump_pad_size = 0x100;
2492
2493static CORE_ADDR gdb_jump_pad_head;
2494
2495/* Return the address of the next free jump space. */
2496
2497static CORE_ADDR
2498get_jump_space_head (void)
2499{
2500 if (gdb_jump_pad_head == 0)
2501 {
2502 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2503 &gdb_jump_pad_head))
2504 fatal ("error extracting jump_pad_buffer");
2505 }
2506
2507 return gdb_jump_pad_head;
2508}
2509
2510/* Reserve USED bytes from the jump space. */
2511
2512static void
2513claim_jump_space (ULONGEST used)
2514{
2515 trace_debug ("claim_jump_space reserves %s bytes at %s",
2516 pulongest (used), paddress (gdb_jump_pad_head));
2517 gdb_jump_pad_head += used;
2518}
2519
2520/* Sort tracepoints by PC, using a bubble sort. */
2521
2522static void
2523sort_tracepoints (void)
2524{
2525 struct tracepoint *lst, *tmp, *prev = NULL;
2526 int i, j, n = 0;
2527
2528 if (tracepoints == NULL)
2529 return;
2530
2531 /* Count nodes. */
2532 for (tmp = tracepoints; tmp->next; tmp = tmp->next)
2533 n++;
2534
2535 for (i = 0; i < n - 1; i++)
2536 for (j = 0, lst = tracepoints;
2537 lst && lst->next && (j <= n - 1 - i);
2538 j++)
2539 {
2540 /* If we're at beginning, the start node is the prev
2541 node. */
2542 if (j == 0)
2543 prev = lst;
2544
2545 /* Compare neighbors. */
2546 if (lst->next->address < lst->address)
2547 {
2548 struct tracepoint *p;
2549
2550 /* Swap'em. */
2551 tmp = (lst->next ? lst->next->next : NULL);
2552
2553 if (j == 0 && prev == tracepoints)
2554 tracepoints = lst->next;
2555
2556 p = lst->next;
2557 prev->next = lst->next;
2558 lst->next->next = lst;
2559 lst->next = tmp;
2560 prev = p;
2561 }
2562 else
2563 {
2564 lst = lst->next;
2565 /* Keep track of the previous node. We need it if we need
2566 to swap nodes. */
2567 if (j != 0)
2568 prev = prev->next;
2569 }
2570 }
2571}
2572
2573#define MAX_JUMP_SIZE 20
2574
219f2f23
PA
2575static void
2576cmd_qtstart (char *packet)
2577{
fa593d66
PA
2578 struct tracepoint *tpoint, *prev_ftpoint;
2579 int slow_tracepoint_count, fast_count;
2580 CORE_ADDR jump_entry;
2581
2582 /* The jump to the jump pad of the last fast tracepoint
2583 installed. */
2584 unsigned char fjump[MAX_JUMP_SIZE];
2585 ULONGEST fjump_size;
219f2f23
PA
2586
2587 trace_debug ("Starting the trace");
2588
fa593d66 2589 slow_tracepoint_count = fast_count = 0;
219f2f23 2590
fa593d66
PA
2591 /* Sort tracepoints by ascending address. This makes installing
2592 fast tracepoints at the same address easier to handle. */
2593 sort_tracepoints ();
219f2f23 2594
7984d532 2595 /* Pause all threads temporarily while we patch tracepoints. */
fa593d66
PA
2596 pause_all (0);
2597
2598 /* Get threads out of jump pads. Safe to do here, since this is a
2599 top level command. And, required to do here, since we're
2600 deleting/rewriting jump pads. */
2601
2602 stabilize_threads ();
2603
2604 /* Freeze threads. */
7984d532
PA
2605 pause_all (1);
2606
fa593d66
PA
2607 /* Sync the fast tracepoints list in the inferior ftlib. */
2608 if (in_process_agent_loaded ())
2609 {
2610 download_tracepoints ();
2611 download_trace_state_variables ();
2612 }
2613
2614 /* No previous fast tpoint yet. */
2615 prev_ftpoint = NULL;
2616
2617 *packet = '\0';
2618
219f2f23
PA
2619 /* Install tracepoints. */
2620 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2621 {
2622 /* Ensure all the hit counts start at zero. */
2623 tpoint->hit_count = 0;
2624
2625 if (!tpoint->enabled)
2626 continue;
2627
fa593d66
PA
2628 if (tpoint->type == trap_tracepoint)
2629 {
2630 ++slow_tracepoint_count;
2631
2632 /* Tracepoints are installed as memory breakpoints. Just go
2633 ahead and install the trap. The breakpoints module
2634 handles duplicated breakpoints, and the memory read
2635 routine handles un-patching traps from memory reads. */
2636 tpoint->handle = set_breakpoint_at (tpoint->address,
2637 tracepoint_handler);
2638 }
2639 else if (tpoint->type == fast_tracepoint)
2640 {
2641 ++fast_count;
2642
2643 if (maybe_write_ipa_not_loaded (packet))
2644 {
2645 trace_debug ("Requested a fast tracepoint, but fast "
2646 "tracepoints aren't supported.");
2647 break;
2648 }
2649
2650 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
2651 {
2652 tpoint->handle = set_fast_tracepoint_jump (tpoint->address,
2653 fjump,
2654 fjump_size);
2655 tpoint->jump_pad = prev_ftpoint->jump_pad;
2656 tpoint->jump_pad_end = prev_ftpoint->jump_pad_end;
2657 tpoint->adjusted_insn_addr = prev_ftpoint->adjusted_insn_addr;
2658 tpoint->adjusted_insn_addr_end
2659 = prev_ftpoint->adjusted_insn_addr_end;
2660 }
2661 else
2662 {
2663 CORE_ADDR jentry;
2664 int err = 0;
2665
2666 prev_ftpoint = NULL;
2667
2668 jentry = jump_entry = get_jump_space_head ();
2669
2670 /* Install the jump pad. */
2671 err = install_fast_tracepoint_jump_pad
2672 (tpoint->obj_addr_on_target,
2673 tpoint->address,
2674 ipa_sym_addrs.addr_gdb_collect,
2675 ipa_sym_addrs.addr_collecting,
2676 tpoint->orig_size,
2677 &jentry,
2678 fjump, &fjump_size,
2679 &tpoint->adjusted_insn_addr,
2680 &tpoint->adjusted_insn_addr_end);
2681
2682 /* Wire it in. */
2683 if (!err)
2684 tpoint->handle = set_fast_tracepoint_jump (tpoint->address,
2685 fjump, fjump_size);
2686
2687 if (tpoint->handle != NULL)
2688 {
2689 tpoint->jump_pad = jump_entry;
2690 tpoint->jump_pad_end = jentry;
219f2f23 2691
fa593d66
PA
2692 /* Pad to 8-byte alignment. */
2693 jentry = ((jentry + 7) & ~0x7);
2694 claim_jump_space (jentry - jump_entry);
219f2f23 2695
fa593d66
PA
2696 /* So that we can handle multiple fast tracepoints
2697 at the same address easily. */
2698 prev_ftpoint = tpoint;
2699 }
2700 }
2701 }
2702
2703 /* Any failure in the inner loop is sufficient cause to give
2704 up. */
219f2f23
PA
2705 if (tpoint->handle == NULL)
2706 break;
2707 }
2708
2709 /* Any error in tracepoint insertion is unacceptable; better to
2710 address the problem now, than end up with a useless or misleading
2711 trace run. */
2712 if (tpoint != NULL)
2713 {
2714 clear_installed_tracepoints ();
2715 if (*packet == '\0')
2716 write_enn (packet);
7984d532 2717 unpause_all (1);
219f2f23
PA
2718 return;
2719 }
2720
2721 stopping_tracepoint = NULL;
2722 trace_buffer_is_full = 0;
2723 expr_eval_result = expr_eval_no_error;
2724 error_tracepoint = NULL;
2725
2726 /* Tracing is now active, hits will now start being logged. */
2727 tracing = 1;
2728
fa593d66
PA
2729 if (in_process_agent_loaded ())
2730 {
2731 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
2732 fatal ("Error setting tracing variable in lib");
2733
2734 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
2735 0))
2736 fatal ("Error clearing stopping_tracepoint variable in lib");
2737
2738 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
2739 fatal ("Error clearing trace_buffer_is_full variable in lib");
2740
2741 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
2742 stop_tracing_handler);
2743 if (stop_tracing_bkpt == NULL)
2744 error ("Error setting stop_tracing breakpoint");
2745
2746 flush_trace_buffer_bkpt
2747 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
2748 flush_trace_buffer_handler);
2749 if (flush_trace_buffer_bkpt == NULL)
2750 error ("Error setting flush_trace_buffer breakpoint");
2751 }
2752
7984d532
PA
2753 unpause_all (1);
2754
219f2f23
PA
2755 write_ok (packet);
2756}
2757
2758/* End a tracing run, filling in a stop reason to report back to GDB,
2759 and removing the tracepoints from the code. */
2760
8336d594 2761void
219f2f23
PA
2762stop_tracing (void)
2763{
2764 if (!tracing)
2765 {
2766 trace_debug ("Tracing is already off, ignoring");
2767 return;
2768 }
2769
2770 trace_debug ("Stopping the trace");
2771
fa593d66
PA
2772 /* Pause all threads before removing fast jumps from memory,
2773 breakpoints, and touching IPA state variables (inferior memory).
2774 Some thread may hit the internal tracing breakpoints, or be
2775 collecting this moment, but that's ok, we don't release the
2776 tpoint object's memory or the jump pads here (we only do that
2777 when we're sure we can move all threads out of the jump pads).
2778 We can't now, since we may be getting here due to the inferior
2779 agent calling us. */
7984d532
PA
2780 pause_all (1);
2781 /* Since we're removing breakpoints, cancel breakpoint hits,
2782 possibly related to the breakpoints we're about to delete. */
2783 cancel_breakpoints ();
2784
219f2f23
PA
2785 /* Stop logging. Tracepoints can still be hit, but they will not be
2786 recorded. */
2787 tracing = 0;
fa593d66
PA
2788 if (in_process_agent_loaded ())
2789 {
2790 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
2791 fatal ("Error clearing tracing variable in lib");
2792 }
219f2f23
PA
2793
2794 tracing_stop_reason = "t???";
2795 tracing_stop_tpnum = 0;
2796 if (stopping_tracepoint)
2797 {
2798 trace_debug ("Stopping the trace because "
2799 "tracepoint %d was hit %ld times",
2800 stopping_tracepoint->number,
2801 stopping_tracepoint->pass_count);
2802 tracing_stop_reason = "tpasscount";
2803 tracing_stop_tpnum = stopping_tracepoint->number;
2804 }
2805 else if (trace_buffer_is_full)
2806 {
2807 trace_debug ("Stopping the trace because the trace buffer is full");
2808 tracing_stop_reason = "tfull";
2809 }
2810 else if (expr_eval_result != expr_eval_no_error)
2811 {
2812 trace_debug ("Stopping the trace because of an expression eval error");
2813 tracing_stop_reason = eval_result_names[expr_eval_result];
2814 tracing_stop_tpnum = error_tracepoint->number;
2815 }
fa593d66 2816#ifndef IN_PROCESS_AGENT
8336d594
PA
2817 else if (!gdb_connected ())
2818 {
2819 trace_debug ("Stopping the trace because GDB disconnected");
2820 tracing_stop_reason = "tdisconnected";
2821 }
fa593d66 2822#endif
219f2f23
PA
2823 else
2824 {
2825 trace_debug ("Stopping the trace because of a tstop command");
2826 tracing_stop_reason = "tstop";
2827 }
2828
2829 stopping_tracepoint = NULL;
2830 error_tracepoint = NULL;
2831
2832 /* Clear out the tracepoints. */
2833 clear_installed_tracepoints ();
7984d532 2834
fa593d66
PA
2835 if (in_process_agent_loaded ())
2836 {
2837 /* Pull in fast tracepoint trace frames from the inferior lib
2838 buffer into our buffer, even if our buffer is already full,
2839 because we want to present the full number of created frames
2840 in addition to what fit in the trace buffer. */
2841 upload_fast_traceframes ();
2842 }
2843
2844 if (stop_tracing_bkpt != NULL)
2845 {
2846 delete_breakpoint (stop_tracing_bkpt);
2847 stop_tracing_bkpt = NULL;
2848 }
2849
2850 if (flush_trace_buffer_bkpt != NULL)
2851 {
2852 delete_breakpoint (flush_trace_buffer_bkpt);
2853 flush_trace_buffer_bkpt = NULL;
2854 }
2855
7984d532 2856 unpause_all (1);
219f2f23
PA
2857}
2858
fa593d66
PA
2859static int
2860stop_tracing_handler (CORE_ADDR addr)
2861{
2862 trace_debug ("lib hit stop_tracing");
2863
2864 /* Don't actually handle it here. When we stop tracing we remove
2865 breakpoints from the inferior, and that is not allowed in a
2866 breakpoint handler (as the caller is walking the breakpoint
2867 list). */
2868 return 0;
2869}
2870
2871static int
2872flush_trace_buffer_handler (CORE_ADDR addr)
2873{
2874 trace_debug ("lib hit flush_trace_buffer");
2875 return 0;
2876}
2877
219f2f23
PA
2878static void
2879cmd_qtstop (char *packet)
2880{
2881 stop_tracing ();
2882 write_ok (packet);
2883}
2884
8336d594
PA
2885static void
2886cmd_qtdisconnected (char *own_buf)
2887{
2888 ULONGEST setting;
2889 char *packet = own_buf;
2890
2891 packet += strlen ("QTDisconnected:");
2892
2893 unpack_varlen_hex (packet, &setting);
2894
2895 write_ok (own_buf);
2896
2897 disconnected_tracing = setting;
2898}
2899
219f2f23
PA
2900static void
2901cmd_qtframe (char *own_buf)
2902{
2903 ULONGEST frame, pc, lo, hi, num;
2904 int tfnum, tpnum;
2905 struct traceframe *tframe;
2906 char *packet = own_buf;
2907
2908 packet += strlen ("QTFrame:");
2909
2910 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
2911 {
2912 packet += strlen ("pc:");
2913 packet = unpack_varlen_hex (packet, &pc);
2914 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
2915 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
2916 }
2917 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
2918 {
2919 packet += strlen ("range:");
2920 packet = unpack_varlen_hex (packet, &lo);
2921 ++packet;
2922 packet = unpack_varlen_hex (packet, &hi);
2923 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
2924 paddress (lo), paddress (hi));
2925 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
2926 }
2927 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
2928 {
2929 packet += strlen ("outside:");
2930 packet = unpack_varlen_hex (packet, &lo);
2931 ++packet;
2932 packet = unpack_varlen_hex (packet, &hi);
2933 trace_debug ("Want to find next traceframe "
2934 "outside the range 0x%s to 0x%s",
2935 paddress (lo), paddress (hi));
2936 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
2937 }
2938 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
2939 {
2940 packet += strlen ("tdp:");
2941 packet = unpack_varlen_hex (packet, &num);
2942 tpnum = (int) num;
2943 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
2944 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
2945 }
2946 else
2947 {
2948 unpack_varlen_hex (packet, &frame);
2949 tfnum = (int) frame;
2950 if (tfnum == -1)
2951 {
2952 trace_debug ("Want to stop looking at traceframes");
2953 current_traceframe = -1;
2954 write_ok (own_buf);
2955 return;
2956 }
2957 trace_debug ("Want to look at traceframe %d", tfnum);
2958 tframe = find_traceframe (tfnum);
2959 }
2960
2961 if (tframe)
2962 {
2963 current_traceframe = tfnum;
2964 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
2965 }
2966 else
2967 sprintf (own_buf, "F-1");
2968}
2969
2970static void
2971cmd_qtstatus (char *packet)
2972{
2973 char *stop_reason_rsp = NULL;
2974
2975 trace_debug ("Returning trace status as %d, stop reason %s",
2976 tracing, tracing_stop_reason);
2977
fa593d66
PA
2978 if (in_process_agent_loaded ())
2979 {
2980 pause_all (1);
2981
2982 upload_fast_traceframes ();
2983
2984 unpause_all (1);
2985 }
2986
219f2f23
PA
2987 stop_reason_rsp = (char *) tracing_stop_reason;
2988
2989 /* The user visible error string in terror needs to be hex encoded.
2990 We leave it as plain string in `tracepoint_stop_reason' to ease
2991 debugging. */
2992 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
2993 {
2994 const char *result_name;
2995 int hexstr_len;
2996 char *p;
2997
2998 result_name = stop_reason_rsp + strlen ("terror:");
2999 hexstr_len = strlen (result_name) * 2;
3000 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3001 strcpy (p, "terror:");
3002 p += strlen (p);
3003 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3004 }
3005
8336d594
PA
3006 sprintf (packet,
3007 "T%d;"
3008 "%s:%x;"
3009 "tframes:%x;tcreated:%x;"
3010 "tfree:%x;tsize:%s;"
3011 "circular:%d;"
3012 "disconn:%d",
623ccd72 3013 tracing ? 1 : 0,
219f2f23
PA
3014 stop_reason_rsp, tracing_stop_tpnum,
3015 traceframe_count, traceframes_created,
8336d594
PA
3016 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3017 circular_trace_buffer,
3018 disconnected_tracing);
219f2f23
PA
3019}
3020
3021/* State variables to help return all the tracepoint bits. */
3022static struct tracepoint *cur_tpoint;
3023static int cur_action;
3024static int cur_step_action;
3025static struct source_string *cur_source_string;
3026static struct trace_state_variable *cur_tsv;
3027
3028/* Compose a response that is an imitation of the syntax by which the
3029 tracepoint was originally downloaded. */
3030
3031static void
3032response_tracepoint (char *packet, struct tracepoint *tpoint)
3033{
3034 char *buf;
3035
3036 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3037 paddress (tpoint->address),
3038 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3039 tpoint->pass_count);
fa593d66
PA
3040 if (tpoint->type == fast_tracepoint)
3041 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
219f2f23
PA
3042
3043 if (tpoint->cond)
3044 {
3045 buf = unparse_agent_expr (tpoint->cond);
3046 sprintf (packet + strlen (packet), ":X%x,%s",
3047 tpoint->cond->length, buf);
3048 free (buf);
3049 }
3050}
3051
3052/* Compose a response that is an imitation of the syntax by which the
3053 tracepoint action was originally downloaded (with the difference
3054 that due to the way we store the actions, this will output a packet
3055 per action, while GDB could have combined more than one action
3056 per-packet. */
3057
3058static void
3059response_action (char *packet, struct tracepoint *tpoint,
3060 char *taction, int step)
3061{
3062 sprintf (packet, "%c%x:%s:%s",
3063 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3064 taction);
3065}
3066
3067/* Compose a response that is an imitation of the syntax by which the
3068 tracepoint source piece was originally downloaded. */
3069
3070static void
3071response_source (char *packet,
3072 struct tracepoint *tpoint, struct source_string *src)
3073{
3074 char *buf;
3075 int len;
3076
3077 len = strlen (src->str);
3078 buf = alloca (len * 2 + 1);
3079 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3080
3081 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3082 tpoint->number, paddress (tpoint->address),
3083 src->type, 0, len, buf);
3084}
3085
3086/* Return the first piece of tracepoint definition, and initialize the
3087 state machine that will iterate through all the tracepoint
3088 bits. */
3089
3090static void
3091cmd_qtfp (char *packet)
3092{
3093 trace_debug ("Returning first tracepoint definition piece");
3094
3095 cur_tpoint = tracepoints;
3096 cur_action = cur_step_action = -1;
3097 cur_source_string = NULL;
3098
3099 if (cur_tpoint)
3100 response_tracepoint (packet, cur_tpoint);
3101 else
3102 strcpy (packet, "l");
3103}
3104
3105/* Return additional pieces of tracepoint definition. Each action and
3106 stepping action must go into its own packet, because of packet size
3107 limits, and so we use state variables to deliver one piece at a
3108 time. */
3109
3110static void
3111cmd_qtsp (char *packet)
3112{
3113 trace_debug ("Returning subsequent tracepoint definition piece");
3114
3115 if (!cur_tpoint)
3116 {
3117 /* This case would normally never occur, but be prepared for
3118 GDB misbehavior. */
3119 strcpy (packet, "l");
3120 }
3121 else if (cur_action < cur_tpoint->numactions - 1)
3122 {
3123 ++cur_action;
3124 response_action (packet, cur_tpoint,
3125 cur_tpoint->actions_str[cur_action], 0);
3126 }
3127 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3128 {
3129 ++cur_step_action;
3130 response_action (packet, cur_tpoint,
3131 cur_tpoint->step_actions_str[cur_step_action], 1);
3132 }
3133 else if ((cur_source_string
3134 ? cur_source_string->next
3135 : cur_tpoint->source_strings))
3136 {
3137 if (cur_source_string)
3138 cur_source_string = cur_source_string->next;
3139 else
3140 cur_source_string = cur_tpoint->source_strings;
3141 response_source (packet, cur_tpoint, cur_source_string);
3142 }
3143 else
3144 {
3145 cur_tpoint = cur_tpoint->next;
3146 cur_action = cur_step_action = -1;
3147 cur_source_string = NULL;
3148 if (cur_tpoint)
3149 response_tracepoint (packet, cur_tpoint);
3150 else
3151 strcpy (packet, "l");
3152 }
3153}
3154
3155/* Compose a response that is an imitation of the syntax by which the
3156 trace state variable was originally downloaded. */
3157
3158static void
3159response_tsv (char *packet, struct trace_state_variable *tsv)
3160{
3161 char *buf = (char *) "";
3162 int namelen;
3163
3164 if (tsv->name)
3165 {
3166 namelen = strlen (tsv->name);
3167 buf = alloca (namelen * 2 + 1);
3168 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3169 }
3170
3171 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3172 tsv->getter ? 1 : 0, buf);
3173}
3174
3175/* Return the first trace state variable definition, and initialize
3176 the state machine that will iterate through all the tsv bits. */
3177
3178static void
3179cmd_qtfv (char *packet)
3180{
3181 trace_debug ("Returning first trace state variable definition");
3182
3183 cur_tsv = trace_state_variables;
3184
3185 if (cur_tsv)
3186 response_tsv (packet, cur_tsv);
3187 else
3188 strcpy (packet, "l");
3189}
3190
3191/* Return additional trace state variable definitions. */
3192
3193static void
3194cmd_qtsv (char *packet)
3195{
3196 trace_debug ("Returning first trace state variable definition");
3197
3198 if (!cur_tpoint)
3199 {
3200 /* This case would normally never occur, but be prepared for
3201 GDB misbehavior. */
3202 strcpy (packet, "l");
3203 }
3204 else if (cur_tsv)
3205 {
3206 cur_tsv = cur_tsv->next;
3207 if (cur_tsv)
3208 response_tsv (packet, cur_tsv);
3209 else
3210 strcpy (packet, "l");
3211 }
3212 else
3213 strcpy (packet, "l");
3214}
3215
3216/* Respond to qTBuffer packet with a block of raw data from the trace
3217 buffer. GDB may ask for a lot, but we are allowed to reply with
3218 only as much as will fit within packet limits or whatever. */
3219
3220static void
3221cmd_qtbuffer (char *own_buf)
3222{
3223 ULONGEST offset, num, tot;
3224 unsigned char *tbp;
3225 char *packet = own_buf;
3226
3227 packet += strlen ("qTBuffer:");
3228
3229 packet = unpack_varlen_hex (packet, &offset);
3230 ++packet; /* skip a comma */
3231 packet = unpack_varlen_hex (packet, &num);
3232
3233 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3234 (int) num, pulongest (offset));
3235
3236 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3237
3238 /* If we're right at the end, reply specially that we're done. */
3239 if (offset == tot)
3240 {
3241 strcpy (own_buf, "l");
3242 return;
3243 }
3244
3245 /* Object to any other out-of-bounds request. */
3246 if (offset > tot)
3247 {
3248 write_enn (own_buf);
3249 return;
3250 }
3251
3252 /* Compute the pointer corresponding to the given offset, accounting
3253 for wraparound. */
3254 tbp = trace_buffer_start + offset;
3255 if (tbp >= trace_buffer_wrap)
3256 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3257
3258 /* Trim to the remaining bytes if we're close to the end. */
3259 if (num > tot - offset)
3260 num = tot - offset;
3261
3262 /* Trim to available packet size. */
3263 if (num >= (PBUFSIZ - 16) / 2 )
3264 num = (PBUFSIZ - 16) / 2;
3265
3266 convert_int_to_ascii (tbp, own_buf, num);
3267 own_buf[num] = '\0';
3268}
3269
3270static void
3271cmd_bigqtbuffer (char *own_buf)
3272{
3273 ULONGEST val;
3274 char *packet = own_buf;
3275
3276 packet += strlen ("QTBuffer:");
3277
3278 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
3279 {
3280 packet += strlen ("circular:");
3281 packet = unpack_varlen_hex (packet, &val);
3282 circular_trace_buffer = val;
3283 trace_debug ("Trace buffer is now %s",
3284 circular_trace_buffer ? "circular" : "linear");
3285 write_ok (own_buf);
3286 }
3287 else
3288 write_enn (own_buf);
3289}
3290
3291int
3292handle_tracepoint_general_set (char *packet)
3293{
3294 if (strcmp ("QTinit", packet) == 0)
3295 {
3296 cmd_qtinit (packet);
3297 return 1;
3298 }
3299 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3300 {
3301 cmd_qtdp (packet);
3302 return 1;
3303 }
3304 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3305 {
3306 cmd_qtdpsrc (packet);
3307 return 1;
3308 }
3309 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3310 {
3311 cmd_qtdv (packet);
3312 return 1;
3313 }
3314 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3315 {
3316 cmd_qtro (packet);
3317 return 1;
3318 }
3319 else if (strcmp ("QTStart", packet) == 0)
3320 {
3321 cmd_qtstart (packet);
3322 return 1;
3323 }
3324 else if (strcmp ("QTStop", packet) == 0)
3325 {
3326 cmd_qtstop (packet);
3327 return 1;
3328 }
8336d594
PA
3329 else if (strncmp ("QTDisconnected:", packet,
3330 strlen ("QTDisconnected:")) == 0)
3331 {
3332 cmd_qtdisconnected (packet);
3333 return 1;
3334 }
219f2f23
PA
3335 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3336 {
3337 cmd_qtframe (packet);
3338 return 1;
3339 }
3340 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
3341 {
3342 cmd_bigqtbuffer (packet);
3343 return 1;
3344 }
3345
3346 return 0;
3347}
3348
3349int
3350handle_tracepoint_query (char *packet)
3351{
3352 if (strcmp ("qTStatus", packet) == 0)
3353 {
3354 cmd_qtstatus (packet);
3355 return 1;
3356 }
3357 else if (strcmp ("qTfP", packet) == 0)
3358 {
3359 cmd_qtfp (packet);
3360 return 1;
3361 }
3362 else if (strcmp ("qTsP", packet) == 0)
3363 {
3364 cmd_qtsp (packet);
3365 return 1;
3366 }
3367 else if (strcmp ("qTfV", packet) == 0)
3368 {
3369 cmd_qtfv (packet);
3370 return 1;
3371 }
3372 else if (strcmp ("qTsV", packet) == 0)
3373 {
3374 cmd_qtsv (packet);
3375 return 1;
3376 }
3377 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3378 {
3379 cmd_qtv (packet);
3380 return 1;
3381 }
3382 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3383 {
3384 cmd_qtbuffer (packet);
3385 return 1;
3386 }
3387
3388 return 0;
3389}
3390
fa593d66
PA
3391#endif
3392#ifndef IN_PROCESS_AGENT
3393
219f2f23
PA
3394/* Call this when thread TINFO has hit the tracepoint defined by
3395 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3396 action. This adds a while-stepping collecting state item to the
3397 threads' collecting state list, so that we can keep track of
3398 multiple simultaneous while-stepping actions being collected by the
3399 same thread. This can happen in cases like:
3400
3401 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3402 ff0002 INSN2
3403 ff0003 INSN3 <-- TP2, collect $regs
3404 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3405 ff0005 INSN5
3406
3407 Notice that when instruction INSN5 is reached, the while-stepping
3408 actions of both TP1 and TP3 are still being collected, and that TP2
3409 had been collected meanwhile. The whole range of ff0001-ff0005
3410 should be single-stepped, due to at least TP1's while-stepping
3411 action covering the whole range. */
3412
3413static void
3414add_while_stepping_state (struct thread_info *tinfo,
3415 int tp_number, CORE_ADDR tp_address)
3416{
3417 struct wstep_state *wstep;
3418
3419 wstep = xmalloc (sizeof (*wstep));
3420 wstep->next = tinfo->while_stepping;
3421
3422 wstep->tp_number = tp_number;
3423 wstep->tp_address = tp_address;
3424 wstep->current_step = 0;
3425
3426 tinfo->while_stepping = wstep;
3427}
3428
3429/* Release the while-stepping collecting state WSTEP. */
3430
3431static void
3432release_while_stepping_state (struct wstep_state *wstep)
3433{
3434 free (wstep);
3435}
3436
3437/* Release all while-stepping collecting states currently associated
3438 with thread TINFO. */
3439
3440void
3441release_while_stepping_state_list (struct thread_info *tinfo)
3442{
3443 struct wstep_state *head;
3444
3445 while (tinfo->while_stepping)
3446 {
3447 head = tinfo->while_stepping;
3448 tinfo->while_stepping = head->next;
3449 release_while_stepping_state (head);
3450 }
3451}
3452
3453/* If TINFO was handling a 'while-stepping' action, the step has
3454 finished, so collect any step data needed, and check if any more
3455 steps are required. Return true if the thread was indeed
3456 collecting tracepoint data, false otherwise. */
3457
3458int
3459tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
3460{
3461 struct tracepoint *tpoint;
3462 struct wstep_state *wstep;
3463 struct wstep_state **wstep_link;
3464 struct trap_tracepoint_ctx ctx;
3465
fa593d66
PA
3466 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3467 our buffer. */
3468 if (in_process_agent_loaded ())
3469 upload_fast_traceframes ();
3470
219f2f23
PA
3471 /* Check if we were indeed collecting data for one of more
3472 tracepoints with a 'while-stepping' count. */
3473 if (tinfo->while_stepping == NULL)
3474 return 0;
3475
3476 if (!tracing)
3477 {
3478 /* We're not even tracing anymore. Stop this thread from
3479 collecting. */
3480 release_while_stepping_state_list (tinfo);
3481
3482 /* The thread had stopped due to a single-step request indeed
3483 explained by a tracepoint. */
3484 return 1;
3485 }
3486
3487 wstep = tinfo->while_stepping;
3488 wstep_link = &tinfo->while_stepping;
3489
3490 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3491 target_pid_to_str (tinfo->entry.id),
3492 wstep->tp_number, paddress (wstep->tp_address));
3493
fa593d66 3494 ctx.base.type = trap_tracepoint;
219f2f23
PA
3495 ctx.regcache = get_thread_regcache (tinfo, 1);
3496
3497 while (wstep != NULL)
3498 {
3499 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
3500 if (tpoint == NULL)
3501 {
3502 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3503 wstep->tp_number, paddress (wstep->tp_address),
3504 target_pid_to_str (tinfo->entry.id));
3505
3506 /* Unlink. */
3507 *wstep_link = wstep->next;
3508 release_while_stepping_state (wstep);
3509 continue;
3510 }
3511
3512 /* We've just finished one step. */
3513 ++wstep->current_step;
3514
3515 /* Collect data. */
3516 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
3517 stop_pc, tpoint, wstep->current_step);
3518
3519 if (wstep->current_step >= tpoint->step_count)
3520 {
3521 /* The requested numbers of steps have occurred. */
3522 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3523 target_pid_to_str (tinfo->entry.id),
3524 wstep->tp_number, paddress (wstep->tp_address));
3525
3526 /* Unlink the wstep. */
3527 *wstep_link = wstep->next;
3528 release_while_stepping_state (wstep);
3529 wstep = *wstep_link;
3530
3531 /* Only check the hit count now, which ensure that we do all
3532 our stepping before stopping the run. */
3533 if (tpoint->pass_count > 0
3534 && tpoint->hit_count >= tpoint->pass_count
3535 && stopping_tracepoint == NULL)
3536 stopping_tracepoint = tpoint;
3537 }
3538 else
3539 {
3540 /* Keep single-stepping until the requested numbers of steps
3541 have occurred. */
3542 wstep_link = &wstep->next;
3543 wstep = *wstep_link;
3544 }
3545
3546 if (stopping_tracepoint
3547 || trace_buffer_is_full
3548 || expr_eval_result != expr_eval_no_error)
3549 {
3550 stop_tracing ();
3551 break;
3552 }
3553 }
3554
3555 return 1;
3556}
3557
fa593d66
PA
3558/* Handle any internal tracing control breakpoint hits. That means,
3559 pull traceframes from the IPA to our buffer, and syncing both
3560 tracing agents when the IPA's tracing stops for some reason. */
3561
3562int
3563handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
3564{
3565 /* Pull in fast tracepoint trace frames from the inferior in-process
3566 agent's buffer into our buffer. */
3567
3568 if (!in_process_agent_loaded ())
3569 return 0;
3570
3571 upload_fast_traceframes ();
3572
3573 /* Check if the in-process agent had decided we should stop
3574 tracing. */
3575 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
3576 {
3577 int ipa_trace_buffer_is_full;
3578 CORE_ADDR ipa_stopping_tracepoint;
3579 int ipa_expr_eval_result;
3580 CORE_ADDR ipa_error_tracepoint;
3581
3582 trace_debug ("lib stopped at stop_tracing");
3583
3584 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
3585 &ipa_trace_buffer_is_full);
3586
3587 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3588 &ipa_stopping_tracepoint);
3589 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
3590
3591 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
3592 &ipa_error_tracepoint);
3593 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
3594
3595 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
3596 &ipa_expr_eval_result);
3597 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
3598
3599 trace_debug ("lib: trace_buffer_is_full: %d, "
3600 "stopping_tracepoint: %s, "
3601 "ipa_expr_eval_result: %d, "
3602 "error_tracepoint: %s, ",
3603 ipa_trace_buffer_is_full,
3604 paddress (ipa_stopping_tracepoint),
3605 ipa_expr_eval_result,
3606 paddress (ipa_error_tracepoint));
3607
3608 if (debug_threads)
3609 {
3610 if (ipa_trace_buffer_is_full)
3611 trace_debug ("lib stopped due to full buffer.");
3612 if (ipa_stopping_tracepoint)
3613 trace_debug ("lib stopped due to tpoint");
3614 if (ipa_stopping_tracepoint)
3615 trace_debug ("lib stopped due to error");
3616 }
3617
3618 if (ipa_stopping_tracepoint != 0)
3619 {
3620 stopping_tracepoint
3621 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
3622 }
3623 else if (ipa_expr_eval_result != expr_eval_no_error)
3624 {
3625 expr_eval_result = ipa_expr_eval_result;
3626 error_tracepoint
3627 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
3628 }
3629 stop_tracing ();
3630 return 1;
3631 }
3632 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
3633 {
3634 trace_debug ("lib stopped at flush_trace_buffer");
3635 return 1;
3636 }
3637
3638 return 0;
3639}
3640
219f2f23
PA
3641/* Return true if TINFO just hit a tracepoint. Collect data if
3642 so. */
3643
3644int
3645tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
3646{
3647 struct tracepoint *tpoint;
3648 int ret = 0;
3649 struct trap_tracepoint_ctx ctx;
3650
3651 /* Not tracing, don't handle. */
3652 if (!tracing)
3653 return 0;
3654
fa593d66 3655 ctx.base.type = trap_tracepoint;
219f2f23
PA
3656 ctx.regcache = get_thread_regcache (tinfo, 1);
3657
3658 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3659 {
fa593d66
PA
3660 /* Note that we collect fast tracepoints here as well. We'll
3661 step over the fast tracepoint jump later, which avoids the
3662 double collect. */
219f2f23
PA
3663 if (tpoint->enabled && stop_pc == tpoint->address)
3664 {
3665 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3666 target_pid_to_str (tinfo->entry.id),
3667 tpoint->number, paddress (tpoint->address));
3668
3669 /* Test the condition if present, and collect if true. */
3670 if (!tpoint->cond
3671 || (condition_true_at_tracepoint
3672 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
3673 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
3674 stop_pc, tpoint);
3675
3676 if (stopping_tracepoint
3677 || trace_buffer_is_full
3678 || expr_eval_result != expr_eval_no_error)
3679 {
3680 stop_tracing ();
3681 }
3682 /* If the tracepoint had a 'while-stepping' action, then set
3683 the thread to collect this tracepoint on the following
3684 single-steps. */
3685 else if (tpoint->step_count > 0)
3686 {
3687 add_while_stepping_state (tinfo,
3688 tpoint->number, tpoint->address);
3689 }
3690
3691 ret = 1;
3692 }
3693 }
3694
3695 return ret;
3696}
3697
fa593d66
PA
3698#endif
3699
219f2f23
PA
3700/* Create a trace frame for the hit of the given tracepoint in the
3701 given thread. */
3702
3703static void
3704collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
3705 struct tracepoint *tpoint)
3706{
3707 struct traceframe *tframe;
3708 int acti;
3709
3710 /* Only count it as a hit when we actually collect data. */
3711 tpoint->hit_count++;
3712
3713 /* If we've exceeded a defined pass count, record the event for
3714 later, and finish the collection for this hit. This test is only
3715 for nonstepping tracepoints, stepping tracepoints test at the end
3716 of their while-stepping loop. */
3717 if (tpoint->pass_count > 0
3718 && tpoint->hit_count >= tpoint->pass_count
3719 && tpoint->step_count == 0
3720 && stopping_tracepoint == NULL)
3721 stopping_tracepoint = tpoint;
3722
3723 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3724 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
3725
3726 tframe = add_traceframe (tpoint);
3727
3728 if (tframe)
3729 {
3730 for (acti = 0; acti < tpoint->numactions; ++acti)
3731 {
fa593d66 3732#ifndef IN_PROCESS_AGENT
219f2f23
PA
3733 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3734 tpoint->number, paddress (tpoint->address),
3735 tpoint->actions_str[acti]);
fa593d66 3736#endif
219f2f23
PA
3737
3738 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
3739 tpoint->actions[acti]);
3740 }
3741
3742 finish_traceframe (tframe);
3743 }
3744
3745 if (tframe == NULL && tracing)
3746 trace_buffer_is_full = 1;
3747}
3748
fa593d66
PA
3749#ifndef IN_PROCESS_AGENT
3750
219f2f23
PA
3751static void
3752collect_data_at_step (struct tracepoint_hit_ctx *ctx,
3753 CORE_ADDR stop_pc,
3754 struct tracepoint *tpoint, int current_step)
3755{
3756 struct traceframe *tframe;
3757 int acti;
3758
3759 trace_debug ("Making new step traceframe for "
3760 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
3761 tpoint->number, paddress (tpoint->address),
3762 current_step, tpoint->step_count,
3763 tpoint->hit_count);
3764
3765 tframe = add_traceframe (tpoint);
3766
3767 if (tframe)
3768 {
3769 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
3770 {
3771 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
3772 tpoint->number, paddress (tpoint->address),
3773 tpoint->step_actions_str[acti]);
3774
3775 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
3776 tpoint->step_actions[acti]);
3777 }
3778
3779 finish_traceframe (tframe);
3780 }
3781
3782 if (tframe == NULL && tracing)
3783 trace_buffer_is_full = 1;
3784}
3785
fa593d66
PA
3786#endif
3787
219f2f23
PA
3788static struct regcache *
3789get_context_regcache (struct tracepoint_hit_ctx *ctx)
3790{
fa593d66
PA
3791 struct regcache *regcache = NULL;
3792
3793#ifdef IN_PROCESS_AGENT
3794 if (ctx->type == fast_tracepoint)
3795 {
3796 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
3797 if (!fctx->regcache_initted)
3798 {
3799 fctx->regcache_initted = 1;
3800 init_register_cache (&fctx->regcache, fctx->regspace);
3801 supply_regblock (&fctx->regcache, NULL);
3802 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
3803 }
3804 regcache = &fctx->regcache;
3805 }
3806#else
3807 if (ctx->type == trap_tracepoint)
3808 {
3809 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
3810 regcache = tctx->regcache;
3811 }
3812#endif
219f2f23
PA
3813
3814 gdb_assert (regcache != NULL);
3815
3816 return regcache;
3817}
3818
3819static void
3820do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
3821 CORE_ADDR stop_pc,
3822 struct tracepoint *tpoint,
3823 struct traceframe *tframe,
3824 struct tracepoint_action *taction)
3825{
3826 enum eval_result_type err;
3827
3828 switch (taction->type)
3829 {
3830 case 'M':
3831 {
3832 struct collect_memory_action *maction;
3833
3834 maction = (struct collect_memory_action *) taction;
3835
3836 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
3837 pulongest (maction->len),
3838 paddress (maction->addr), maction->basereg);
3839 /* (should use basereg) */
3840 agent_mem_read (tframe, NULL,
3841 (CORE_ADDR) maction->addr, maction->len);
3842 break;
3843 }
3844 case 'R':
3845 {
3846 struct collect_registers_action *raction;
3847
3848 unsigned char *regspace;
3849 struct regcache tregcache;
3850 struct regcache *context_regcache;
3851
3852 raction = (struct collect_registers_action *) taction;
3853
3854 trace_debug ("Want to collect registers");
3855
3856 /* Collect all registers for now. */
3857 regspace = add_traceframe_block (tframe,
3858 1 + register_cache_size ());
3859 if (regspace == NULL)
3860 {
3861 trace_debug ("Trace buffer block allocation failed, skipping");
3862 break;
3863 }
3864 /* Identify a register block. */
3865 *regspace = 'R';
3866
3867 context_regcache = get_context_regcache (ctx);
3868
3869 /* Wrap the regblock in a register cache (in the stack, we
3870 don't want to malloc here). */
3871 init_register_cache (&tregcache, regspace + 1);
3872
3873 /* Copy the register data to the regblock. */
3874 regcache_cpy (&tregcache, context_regcache);
3875
fa593d66 3876#ifndef IN_PROCESS_AGENT
219f2f23
PA
3877 /* On some platforms, trap-based tracepoints will have the PC
3878 pointing to the next instruction after the trap, but we
3879 don't want the user or GDB trying to guess whether the
3880 saved PC needs adjusting; so always record the adjusted
3881 stop_pc. Note that we can't use tpoint->address instead,
fa593d66
PA
3882 since it will be wrong for while-stepping actions. This
3883 adjustment is a nop for fast tracepoints collected from the
3884 in-process lib (but not if GDBserver is collecting one
3885 preemptively), since the PC had already been adjusted to
3886 contain the tracepoint's address by the jump pad. */
219f2f23
PA
3887 trace_debug ("Storing stop pc (0x%s) in regblock",
3888 paddress (tpoint->address));
3889
3890 /* This changes the regblock, not the thread's
3891 regcache. */
3892 regcache_write_pc (&tregcache, stop_pc);
fa593d66 3893#endif
219f2f23
PA
3894 }
3895 break;
3896 case 'X':
3897 {
3898 struct eval_expr_action *eaction;
3899
3900 eaction = (struct eval_expr_action *) taction;
3901
3902 trace_debug ("Want to evaluate expression");
3903
3904 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
3905
3906 if (err != expr_eval_no_error)
3907 {
3908 record_tracepoint_error (tpoint, "action expression", err);
3909 return;
3910 }
3911 }
3912 break;
3913 default:
3914 trace_debug ("unknown trace action '%c', ignoring", taction->type);
3915 break;
3916 }
3917}
3918
3919static int
3920condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
3921 struct tracepoint *tpoint)
3922{
3923 ULONGEST value = 0;
3924 enum eval_result_type err;
3925
c6beb2cb
PA
3926 /* Presently, gdbserver doesn't run compiled conditions, only the
3927 IPA does. If the program stops at a fast tracepoint's address
3928 (e.g., due to a breakpoint, trap tracepoint, or stepping),
3929 gdbserver preemptively collect the fast tracepoint. Later, on
3930 resume, gdbserver steps over the fast tracepoint like it steps
3931 over breakpoints, so that the IPA doesn't see that fast
3932 tracepoint. This avoids double collects of fast tracepoints in
3933 that stopping scenario. Having gdbserver itself handle the fast
3934 tracepoint gives the user a consistent view of when fast or trap
3935 tracepoints are collected, compared to an alternative where only
3936 trap tracepoints are collected on stop, and fast tracepoints on
3937 resume. When a fast tracepoint is being processed by gdbserver,
3938 it is always the non-compiled condition expression that is
3939 used. */
3940#ifdef IN_PROCESS_AGENT
6a271cae
PA
3941 if (tpoint->compiled_cond)
3942 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
3943 else
c6beb2cb 3944#endif
6a271cae 3945 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
219f2f23
PA
3946
3947 if (err != expr_eval_no_error)
3948 {
3949 record_tracepoint_error (tpoint, "condition", err);
3950 /* The error case must return false. */
3951 return 0;
3952 }
3953
3954 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
3955 tpoint->number, paddress (tpoint->address),
3956 pulongest (value));
3957 return (value ? 1 : 0);
3958}
3959
fa593d66
PA
3960#ifndef IN_PROCESS_AGENT
3961
219f2f23
PA
3962/* The packet form of an agent expression consists of an 'X', number
3963 of bytes in expression, a comma, and then the bytes. */
3964
3965static struct agent_expr *
3966parse_agent_expr (char **actparm)
3967{
3968 char *act = *actparm;
3969 ULONGEST xlen;
3970 struct agent_expr *aexpr;
3971
3972 ++act; /* skip the X */
3973 act = unpack_varlen_hex (act, &xlen);
3974 ++act; /* skip a comma */
3975 aexpr = xmalloc (sizeof (struct agent_expr));
3976 aexpr->length = xlen;
3977 aexpr->bytes = xmalloc (xlen);
3978 convert_ascii_to_int (act, aexpr->bytes, xlen);
3979 *actparm = act + (xlen * 2);
3980 return aexpr;
3981}
3982
3983/* Convert the bytes of an agent expression back into hex digits, so
3984 they can be printed or uploaded. This allocates the buffer,
3985 callers should free when they are done with it. */
3986
3987static char *
3988unparse_agent_expr (struct agent_expr *aexpr)
3989{
3990 char *rslt;
3991
3992 rslt = xmalloc (2 * aexpr->length + 1);
3993 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
3994 return rslt;
3995}
3996
fa593d66
PA
3997#endif
3998
219f2f23
PA
3999/* The agent expression evaluator, as specified by the GDB docs. It
4000 returns 0 if everything went OK, and a nonzero error code
4001 otherwise. */
4002
4003static enum eval_result_type
4004eval_agent_expr (struct tracepoint_hit_ctx *ctx,
4005 struct traceframe *tframe,
4006 struct agent_expr *aexpr,
4007 ULONGEST *rslt)
4008{
4009 int pc = 0;
4010#define STACK_MAX 100
4011 ULONGEST stack[STACK_MAX], top;
4012 int sp = 0;
4013 unsigned char op;
4014 int arg;
4015
4016 /* This union is a convenient way to convert representations. For
4017 now, assume a standard architecture where the hardware integer
4018 types have 8, 16, 32, 64 bit types. A more robust solution would
4019 be to import stdint.h from gnulib. */
4020 union
4021 {
4022 union
4023 {
4024 unsigned char bytes[1];
4025 unsigned char val;
4026 } u8;
4027 union
4028 {
4029 unsigned char bytes[2];
4030 unsigned short val;
4031 } u16;
4032 union
4033 {
4034 unsigned char bytes[4];
4035 unsigned int val;
4036 } u32;
4037 union
4038 {
4039 unsigned char bytes[8];
4040 ULONGEST val;
4041 } u64;
4042 } cnv;
4043
4044 if (aexpr->length == 0)
4045 {
4046 trace_debug ("empty agent expression");
4047 return expr_eval_empty_expression;
4048 }
4049
4050 /* Cache the stack top in its own variable. Much of the time we can
4051 operate on this variable, rather than dinking with the stack. It
4052 needs to be copied to the stack when sp changes. */
4053 top = 0;
4054
4055 while (1)
4056 {
4057 op = aexpr->bytes[pc++];
4058
4059 trace_debug ("About to interpret byte 0x%x", op);
4060
4061 switch (op)
4062 {
4063 case gdb_agent_op_add:
4064 top += stack[--sp];
4065 break;
4066
4067 case gdb_agent_op_sub:
4068 top = stack[--sp] - top;
4069 break;
4070
4071 case gdb_agent_op_mul:
4072 top *= stack[--sp];
4073 break;
4074
4075 case gdb_agent_op_div_signed:
4076 if (top == 0)
4077 {
4078 trace_debug ("Attempted to divide by zero");
4079 return expr_eval_divide_by_zero;
4080 }
4081 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4082 break;
4083
4084 case gdb_agent_op_div_unsigned:
4085 if (top == 0)
4086 {
4087 trace_debug ("Attempted to divide by zero");
4088 return expr_eval_divide_by_zero;
4089 }
4090 top = stack[--sp] / top;
4091 break;
4092
4093 case gdb_agent_op_rem_signed:
4094 if (top == 0)
4095 {
4096 trace_debug ("Attempted to divide by zero");
4097 return expr_eval_divide_by_zero;
4098 }
4099 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4100 break;
4101
4102 case gdb_agent_op_rem_unsigned:
4103 if (top == 0)
4104 {
4105 trace_debug ("Attempted to divide by zero");
4106 return expr_eval_divide_by_zero;
4107 }
4108 top = stack[--sp] % top;
4109 break;
4110
4111 case gdb_agent_op_lsh:
4112 top = stack[--sp] << top;
4113 break;
4114
4115 case gdb_agent_op_rsh_signed:
4116 top = ((LONGEST) stack[--sp]) >> top;
4117 break;
4118
4119 case gdb_agent_op_rsh_unsigned:
4120 top = stack[--sp] >> top;
4121 break;
4122
4123 case gdb_agent_op_trace:
4124 agent_mem_read (tframe,
4125 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4126 if (--sp >= 0)
4127 top = stack[sp];
4128 break;
4129
4130 case gdb_agent_op_trace_quick:
4131 arg = aexpr->bytes[pc++];
4132 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4133 break;
4134
4135 case gdb_agent_op_log_not:
4136 top = !top;
4137 break;
4138
4139 case gdb_agent_op_bit_and:
4140 top &= stack[--sp];
4141 break;
4142
4143 case gdb_agent_op_bit_or:
4144 top |= stack[--sp];
4145 break;
4146
4147 case gdb_agent_op_bit_xor:
4148 top ^= stack[--sp];
4149 break;
4150
4151 case gdb_agent_op_bit_not:
4152 top = ~top;
4153 break;
4154
4155 case gdb_agent_op_equal:
4156 top = (stack[--sp] == top);
4157 break;
4158
4159 case gdb_agent_op_less_signed:
4160 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4161 break;
4162
4163 case gdb_agent_op_less_unsigned:
4164 top = (stack[--sp] < top);
4165 break;
4166
4167 case gdb_agent_op_ext:
4168 arg = aexpr->bytes[pc++];
4169 if (arg < (sizeof (LONGEST) * 8))
4170 {
4171 LONGEST mask = 1 << (arg - 1);
4172 top &= ((LONGEST) 1 << arg) - 1;
4173 top = (top ^ mask) - mask;
4174 }
4175 break;
4176
4177 case gdb_agent_op_ref8:
4178 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4179 top = cnv.u8.val;
4180 break;
4181
4182 case gdb_agent_op_ref16:
4183 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4184 top = cnv.u16.val;
4185 break;
4186
4187 case gdb_agent_op_ref32:
4188 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4189 top = cnv.u32.val;
4190 break;
4191
4192 case gdb_agent_op_ref64:
4193 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4194 top = cnv.u64.val;
4195 break;
4196
4197 case gdb_agent_op_if_goto:
4198 if (top)
4199 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4200 else
4201 pc += 2;
4202 if (--sp >= 0)
4203 top = stack[sp];
4204 break;
4205
4206 case gdb_agent_op_goto:
4207 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4208 break;
4209
4210 case gdb_agent_op_const8:
4211 /* Flush the cached stack top. */
4212 stack[sp++] = top;
4213 top = aexpr->bytes[pc++];
4214 break;
4215
4216 case gdb_agent_op_const16:
4217 /* Flush the cached stack top. */
4218 stack[sp++] = top;
4219 top = aexpr->bytes[pc++];
4220 top = (top << 8) + aexpr->bytes[pc++];
4221 break;
4222
4223 case gdb_agent_op_const32:
4224 /* Flush the cached stack top. */
4225 stack[sp++] = top;
4226 top = aexpr->bytes[pc++];
4227 top = (top << 8) + aexpr->bytes[pc++];
4228 top = (top << 8) + aexpr->bytes[pc++];
4229 top = (top << 8) + aexpr->bytes[pc++];
4230 break;
4231
4232 case gdb_agent_op_const64:
4233 /* Flush the cached stack top. */
4234 stack[sp++] = top;
4235 top = aexpr->bytes[pc++];
4236 top = (top << 8) + aexpr->bytes[pc++];
4237 top = (top << 8) + aexpr->bytes[pc++];
4238 top = (top << 8) + aexpr->bytes[pc++];
4239 top = (top << 8) + aexpr->bytes[pc++];
4240 top = (top << 8) + aexpr->bytes[pc++];
4241 top = (top << 8) + aexpr->bytes[pc++];
4242 top = (top << 8) + aexpr->bytes[pc++];
4243 break;
4244
4245 case gdb_agent_op_reg:
4246 /* Flush the cached stack top. */
4247 stack[sp++] = top;
4248 arg = aexpr->bytes[pc++];
4249 arg = (arg << 8) + aexpr->bytes[pc++];
4250 {
4251 int regnum = arg;
4252 struct regcache *regcache;
4253
4254 regcache = get_context_regcache (ctx);
4255
4256 switch (register_size (regnum))
4257 {
4258 case 8:
4259 collect_register (regcache, regnum, cnv.u64.bytes);
4260 top = cnv.u64.val;
4261 break;
4262 case 4:
4263 collect_register (regcache, regnum, cnv.u32.bytes);
4264 top = cnv.u32.val;
4265 break;
4266 case 2:
4267 collect_register (regcache, regnum, cnv.u16.bytes);
4268 top = cnv.u16.val;
4269 break;
4270 case 1:
4271 collect_register (regcache, regnum, cnv.u8.bytes);
4272 top = cnv.u8.val;
4273 break;
4274 default:
4275 internal_error (__FILE__, __LINE__,
4276 "unhandled register size");
4277 }
4278 }
4279 break;
4280
4281 case gdb_agent_op_end:
4282 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4283 sp, pulongest (top));
4284 if (rslt)
4285 {
4286 if (sp <= 0)
4287 {
4288 /* This should be an error */
4289 trace_debug ("Stack is empty, nothing to return");
4290 return expr_eval_empty_stack;
4291 }
4292 *rslt = top;
4293 }
4294 return expr_eval_no_error;
4295
4296 case gdb_agent_op_dup:
4297 stack[sp++] = top;
4298 break;
4299
4300 case gdb_agent_op_pop:
4301 if (--sp >= 0)
4302 top = stack[sp];
4303 break;
4304
4305 case gdb_agent_op_zero_ext:
4306 arg = aexpr->bytes[pc++];
4307 if (arg < (sizeof (LONGEST) * 8))
4308 top &= ((LONGEST) 1 << arg) - 1;
4309 break;
4310
4311 case gdb_agent_op_swap:
4312 /* Interchange top two stack elements, making sure top gets
4313 copied back onto stack. */
4314 stack[sp] = top;
4315 top = stack[sp - 1];
4316 stack[sp - 1] = stack[sp];
4317 break;
4318
4319 case gdb_agent_op_getv:
4320 /* Flush the cached stack top. */
4321 stack[sp++] = top;
4322 arg = aexpr->bytes[pc++];
4323 arg = (arg << 8) + aexpr->bytes[pc++];
4324 top = get_trace_state_variable_value (arg);
4325 break;
4326
4327 case gdb_agent_op_setv:
4328 arg = aexpr->bytes[pc++];
4329 arg = (arg << 8) + aexpr->bytes[pc++];
4330 set_trace_state_variable_value (arg, top);
4331 /* Note that we leave the value on the stack, for the
4332 benefit of later/enclosing expressions. */
4333 break;
4334
4335 case gdb_agent_op_tracev:
4336 arg = aexpr->bytes[pc++];
4337 arg = (arg << 8) + aexpr->bytes[pc++];
4338 agent_tsv_read (tframe, arg);
4339 break;
4340
4341 /* GDB never (currently) generates any of these ops. */
4342 case gdb_agent_op_float:
4343 case gdb_agent_op_ref_float:
4344 case gdb_agent_op_ref_double:
4345 case gdb_agent_op_ref_long_double:
4346 case gdb_agent_op_l_to_d:
4347 case gdb_agent_op_d_to_l:
4348 case gdb_agent_op_trace16:
4349 trace_debug ("Agent expression op 0x%x valid, but not handled",
4350 op);
4351 /* If ever GDB generates any of these, we don't have the
4352 option of ignoring. */
4353 return 1;
4354
4355 default:
4356 trace_debug ("Agent expression op 0x%x not recognized", op);
4357 /* Don't struggle on, things will just get worse. */
4358 return expr_eval_unrecognized_opcode;
4359 }
4360
4361 /* Check for stack badness. */
4362 if (sp >= (STACK_MAX - 1))
4363 {
4364 trace_debug ("Expression stack overflow");
4365 return expr_eval_stack_overflow;
4366 }
4367
4368 if (sp < 0)
4369 {
4370 trace_debug ("Expression stack underflow");
4371 return expr_eval_stack_underflow;
4372 }
4373
4374 trace_debug ("Op %s -> sp=%d, top=0x%s",
4375 gdb_agent_op_names[op], sp, pulongest (top));
4376 }
4377}
4378
4379/* Do memory copies for bytecodes. */
4380/* Do the recording of memory blocks for actions and bytecodes. */
4381
4382static int
4383agent_mem_read (struct traceframe *tframe,
4384 unsigned char *to, CORE_ADDR from, ULONGEST len)
4385{
4386 unsigned char *mspace;
4387 ULONGEST remaining = len;
4388 unsigned short blocklen;
4389
4390 /* If a 'to' buffer is specified, use it. */
4391 if (to != NULL)
4392 {
4393 read_inferior_memory (from, to, len);
4394 return 0;
4395 }
4396
4397 /* Otherwise, create a new memory block in the trace buffer. */
4398 while (remaining > 0)
4399 {
4400 size_t sp;
4401
4402 blocklen = (remaining > 65535 ? 65535 : remaining);
4403 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4404 mspace = add_traceframe_block (tframe, sp);
4405 if (mspace == NULL)
4406 return 1;
4407 /* Identify block as a memory block. */
4408 *mspace = 'M';
4409 ++mspace;
4410 /* Record address and size. */
4411 memcpy (mspace, &from, sizeof (from));
4412 mspace += sizeof (from);
4413 memcpy (mspace, &blocklen, sizeof (blocklen));
4414 mspace += sizeof (blocklen);
4415 /* Record the memory block proper. */
4416 read_inferior_memory (from, mspace, blocklen);
4417 trace_debug ("%d bytes recorded", blocklen);
4418 remaining -= blocklen;
4419 from += blocklen;
4420 }
4421 return 0;
4422}
4423
4424/* Record the value of a trace state variable. */
4425
4426static int
4427agent_tsv_read (struct traceframe *tframe, int n)
4428{
4429 unsigned char *vspace;
4430 LONGEST val;
4431
4432 vspace = add_traceframe_block (tframe,
4433 1 + sizeof (n) + sizeof (LONGEST));
4434 if (vspace == NULL)
4435 return 1;
4436 /* Identify block as a variable. */
4437 *vspace = 'V';
4438 /* Record variable's number and value. */
4439 memcpy (vspace + 1, &n, sizeof (n));
4440 val = get_trace_state_variable_value (n);
4441 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4442 trace_debug ("Variable %d recorded", n);
4443 return 0;
4444}
4445
fa593d66
PA
4446#ifndef IN_PROCESS_AGENT
4447
219f2f23
PA
4448static unsigned char *
4449traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4450 int tfnum, char type_wanted)
4451{
4452 unsigned char *dataptr;
4453
4454 if (datasize == 0)
4455 {
4456 trace_debug ("traceframe %d has no data", tfnum);
4457 return NULL;
4458 }
4459
4460 /* Iterate through a traceframe's blocks, looking for a block of the
4461 requested type. */
4462 for (dataptr = database;
4463 dataptr < database + datasize;
4464 /* nothing */)
4465 {
4466 char blocktype;
4467 unsigned short mlen;
4468
4469 if (dataptr == trace_buffer_wrap)
4470 {
4471 /* Adjust to reflect wrapping part of the frame around to
4472 the beginning. */
4473 datasize = dataptr - database;
4474 dataptr = database = trace_buffer_lo;
4475 }
4476 blocktype = *dataptr++;
4477
4478 if (type_wanted == blocktype)
4479 return dataptr;
4480
4481 switch (blocktype)
4482 {
4483 case 'R':
4484 /* Skip over the registers block. */
4485 dataptr += register_cache_size ();
4486 break;
4487 case 'M':
4488 /* Skip over the memory block. */
4489 dataptr += sizeof (CORE_ADDR);
4490 memcpy (&mlen, dataptr, sizeof (mlen));
4491 dataptr += (sizeof (mlen) + mlen);
4492 break;
219f2f23
PA
4493 case 'V':
4494 /* Skip over the TSV block. */
4495 dataptr += (sizeof (int) + sizeof (LONGEST));
4496 break;
4497 default:
4498 trace_debug ("traceframe %d has unknown block type 0x%x",
4499 tfnum, blocktype);
4500 return NULL;
4501 }
4502 }
4503
4504 return NULL;
4505}
4506
4507static unsigned char *
4508traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4509{
4510 unsigned char *regblock;
4511
4512 regblock = traceframe_find_block_type (tframe->data,
4513 tframe->data_size,
4514 tfnum, 'R');
4515
4516 if (regblock == NULL)
4517 trace_debug ("traceframe %d has no register data", tfnum);
4518
4519 return regblock;
4520}
4521
4522/* Get registers from a traceframe. */
4523
4524int
4525fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4526{
4527 unsigned char *dataptr;
4528 struct tracepoint *tpoint;
4529 struct traceframe *tframe;
4530
4531 tframe = find_traceframe (tfnum);
4532
4533 if (tframe == NULL)
4534 {
4535 trace_debug ("traceframe %d not found", tfnum);
4536 return 1;
4537 }
4538
4539 dataptr = traceframe_find_regblock (tframe, tfnum);
4540 if (dataptr == NULL)
4541 {
4542 /* We don't like making up numbers, but GDB has all manner of
4543 troubles when the target says there are no registers. */
4544 supply_regblock (regcache, NULL);
4545
4546 /* We can generally guess at a PC, although this will be
4547 misleading for while-stepping frames and multi-location
4548 tracepoints. */
4549 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4550 if (tpoint != NULL)
4551 regcache_write_pc (regcache, tpoint->address);
4552 }
4553 else
4554 supply_regblock (regcache, dataptr);
4555
4556 return 0;
4557}
4558
4559static CORE_ADDR
4560traceframe_get_pc (struct traceframe *tframe)
4561{
4562 struct regcache regcache;
4563 unsigned char *dataptr;
4564
4565 dataptr = traceframe_find_regblock (tframe, -1);
4566 if (dataptr == NULL)
4567 return 0;
4568
4569 init_register_cache (&regcache, dataptr);
4570 return regcache_read_pc (&regcache);
4571}
4572
4573/* Read a requested block of memory from a trace frame. */
4574
4575int
4576traceframe_read_mem (int tfnum, CORE_ADDR addr,
4577 unsigned char *buf, ULONGEST length,
4578 ULONGEST *nbytes)
4579{
4580 struct traceframe *tframe;
4581 unsigned char *database, *dataptr;
4582 unsigned int datasize;
4583 CORE_ADDR maddr;
4584 unsigned short mlen;
4585
4586 trace_debug ("traceframe_read_mem");
4587
4588 tframe = find_traceframe (tfnum);
4589
4590 if (!tframe)
4591 {
4592 trace_debug ("traceframe %d not found", tfnum);
4593 return 1;
4594 }
4595
4596 datasize = tframe->data_size;
4597 database = dataptr = &tframe->data[0];
4598
4599 /* Iterate through a traceframe's blocks, looking for memory. */
4600 while ((dataptr = traceframe_find_block_type (dataptr,
4601 datasize - (dataptr - database),
4602 tfnum, 'M')) != NULL)
4603 {
4604 memcpy (&maddr, dataptr, sizeof (maddr));
4605 dataptr += sizeof (maddr);
4606 memcpy (&mlen, dataptr, sizeof (mlen));
4607 dataptr += sizeof (mlen);
4608 trace_debug ("traceframe %d has %d bytes at %s",
4609 tfnum, mlen, paddress (maddr));
4610
4611 /* Check that requested data is in bounds. */
4612 if (maddr <= addr && (addr + length) <= (maddr + mlen))
4613 {
4614 /* Block includes the requested range, copy it out. */
4615 memcpy (buf, dataptr + (addr - maddr), length);
4616 *nbytes = length;
4617 return 0;
4618 }
4619
4620 /* Skip over this block. */
4621 dataptr += mlen;
4622 }
4623
4624 trace_debug ("traceframe %d has no memory data for the desired region",
4625 tfnum);
4626
4627 *nbytes = 0;
4628 return 0;
4629}
4630
4631static int
4632traceframe_read_tsv (int tsvnum, LONGEST *val)
4633{
4634 int tfnum;
4635 struct traceframe *tframe;
4636 unsigned char *database, *dataptr;
4637 unsigned int datasize;
4638 int vnum;
4639
4640 trace_debug ("traceframe_read_tsv");
4641
4642 tfnum = current_traceframe;
4643
4644 if (tfnum < 0)
4645 {
4646 trace_debug ("no current traceframe");
4647 return 1;
4648 }
4649
4650 tframe = find_traceframe (tfnum);
4651
4652 if (tframe == NULL)
4653 {
4654 trace_debug ("traceframe %d not found", tfnum);
4655 return 1;
4656 }
4657
4658 datasize = tframe->data_size;
4659 database = dataptr = &tframe->data[0];
4660
4661 /* Iterate through a traceframe's blocks, looking for the tsv. */
4662 while ((dataptr = traceframe_find_block_type (dataptr,
4663 datasize - (dataptr - database),
4664 tfnum, 'V')) != NULL)
4665 {
4666 memcpy (&vnum, dataptr, sizeof (vnum));
4667 dataptr += sizeof (vnum);
4668
4669 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
4670
4671 /* Check that this is the variable we want. */
4672 if (tsvnum == vnum)
4673 {
4674 memcpy (val, dataptr, sizeof (*val));
4675 return 0;
4676 }
4677
4678 /* Skip over this block. */
4679 dataptr += sizeof (LONGEST);
4680 }
4681
4682 trace_debug ("traceframe %d has no data for variable %d",
4683 tfnum, tsvnum);
4684 return 1;
4685}
4686
fa593d66
PA
4687/* Return the first fast tracepoint whose jump pad contains PC. */
4688
4689static struct tracepoint *
4690fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
4691{
4692 struct tracepoint *tpoint;
4693
4694 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4695 if (tpoint->type == fast_tracepoint)
4696 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
4697 return tpoint;
4698
4699 return NULL;
4700}
4701
4702/* Return GDBserver's tracepoint that matches the IP Agent's
4703 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
4704 address space. */
4705
4706static struct tracepoint *
4707fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
4708{
4709 struct tracepoint *tpoint;
4710
4711 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4712 if (tpoint->type == fast_tracepoint)
4713 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
4714 return tpoint;
4715
4716 return NULL;
4717}
4718
4719#endif
4720
4721/* The type of the object that is used to synchronize fast tracepoint
4722 collection. */
4723
4724typedef struct collecting_t
4725{
4726 /* The fast tracepoint number currently collecting. */
4727 uintptr_t tpoint;
4728
4729 /* A number that GDBserver can use to identify the thread that is
4730 presently holding the collect lock. This need not (and usually
4731 is not) the thread id, as getting the current thread ID usually
4732 requires a system call, which we want to avoid like the plague.
4733 Usually this is thread's TCB, found in the TLS (pseudo-)
4734 register, which is readable with a single insn on several
4735 architectures. */
4736 uintptr_t thread_area;
4737} collecting_t;
4738
4739#ifndef IN_PROCESS_AGENT
4740
4741void
4742force_unlock_trace_buffer (void)
4743{
4744 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
4745}
4746
4747/* Check if the thread identified by THREAD_AREA which is stopped at
4748 STOP_PC, is presently locking the fast tracepoint collection, and
4749 if so, gather some status of said collection. Returns 0 if the
4750 thread isn't collecting or in the jump pad at all. 1, if in the
4751 jump pad (or within gdb_collect) and hasn't executed the adjusted
4752 original insn yet (can set a breakpoint there and run to it). 2,
4753 if presently executing the adjusted original insn --- in which
4754 case, if we want to move the thread out of the jump pad, we need to
4755 single-step it until this function returns 0. */
4756
4757int
4758fast_tracepoint_collecting (CORE_ADDR thread_area,
4759 CORE_ADDR stop_pc,
4760 struct fast_tpoint_collect_status *status)
4761{
4762 CORE_ADDR ipa_collecting;
4763 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
4764 struct tracepoint *tpoint;
4765 int needs_breakpoint;
4766
4767 /* The thread THREAD_AREA is either:
4768
4769 0. not collecting at all, not within the jump pad, or within
4770 gdb_collect or one of its callees.
4771
4772 1. in the jump pad and haven't reached gdb_collect
4773
4774 2. within gdb_collect (out of the jump pad) (collect is set)
4775
4776 3. we're in the jump pad, after gdb_collect having returned,
4777 possibly executing the adjusted insns.
4778
4779 For cases 1 and 3, `collecting' may or not be set. The jump pad
4780 doesn't have any complicated jump logic, so we can tell if the
4781 thread is executing the adjust original insn or not by just
4782 matching STOP_PC with known jump pad addresses. If we it isn't
4783 yet executing the original insn, set a breakpoint there, and let
4784 the thread run to it, so to quickly step over a possible (many
4785 insns) gdb_collect call. Otherwise, or when the breakpoint is
4786 hit, only a few (small number of) insns are left to be executed
4787 in the jump pad. Single-step the thread until it leaves the
4788 jump pad. */
4789
4790 again:
4791 tpoint = NULL;
4792 needs_breakpoint = 0;
4793 trace_debug ("fast_tracepoint_collecting");
4794
4795 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
4796 &ipa_gdb_jump_pad_buffer))
4797 fatal ("error extracting `gdb_jump_pad_buffer'");
4798 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
4799 &ipa_gdb_jump_pad_buffer_end))
4800 fatal ("error extracting `gdb_jump_pad_buffer_end'");
4801
4802 if (ipa_gdb_jump_pad_buffer <= stop_pc && stop_pc < ipa_gdb_jump_pad_buffer_end)
4803 {
4804 /* We can tell which tracepoint(s) the thread is collecting by
4805 matching the jump pad address back to the tracepoint. */
4806 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
4807 if (tpoint == NULL)
4808 {
4809 warning ("in jump pad, but no matching tpoint?");
4810 return 0;
4811 }
4812 else
4813 {
4814 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
4815 "adj_insn(%s, %s)",
4816 tpoint->number, paddress (tpoint->address),
4817 paddress (tpoint->jump_pad),
4818 paddress (tpoint->jump_pad_end),
4819 paddress (tpoint->adjusted_insn_addr),
4820 paddress (tpoint->adjusted_insn_addr_end));
4821 }
4822
4823 /* Definitely in the jump pad. May or may not need
4824 fast-exit-jump-pad breakpoint. */
4825 if (tpoint->jump_pad <= stop_pc
4826 && stop_pc < tpoint->adjusted_insn_addr)
4827 needs_breakpoint = 1;
4828 }
4829 else
4830 {
4831 collecting_t ipa_collecting_obj;
4832
4833 /* If `collecting' is set/locked, then the THREAD_AREA thread
4834 may or not be the one holding the lock. We have to read the
4835 lock to find out. */
4836
4837 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
4838 &ipa_collecting))
4839 {
4840 trace_debug ("fast_tracepoint_collecting:"
4841 " failed reading 'collecting' in the inferior");
4842 return 0;
4843 }
4844
4845 if (!ipa_collecting)
4846 {
4847 trace_debug ("fast_tracepoint_collecting: not collecting"
4848 " (and nobody is).");
4849 return 0;
4850 }
4851
4852 /* Some thread is collecting. Check which. */
4853 if (read_inferior_memory (ipa_collecting,
4854 (unsigned char *) &ipa_collecting_obj,
4855 sizeof (ipa_collecting_obj)) != 0)
4856 goto again;
4857
4858 if (ipa_collecting_obj.thread_area != thread_area)
4859 {
4860 trace_debug ("fast_tracepoint_collecting: not collecting "
4861 "(another thread is)");
4862 return 0;
4863 }
4864
4865 tpoint
4866 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
4867 if (tpoint == NULL)
4868 {
4869 warning ("fast_tracepoint_collecting: collecting, "
4870 "but tpoint %s not found?",
4871 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
4872 return 0;
4873 }
4874
4875 /* The thread is within `gdb_collect', skip over the rest of
4876 fast tracepoint collection quickly using a breakpoint. */
4877 needs_breakpoint = 1;
4878 }
4879
4880 /* The caller wants a bit of status detail. */
4881 if (status != NULL)
4882 {
4883 status->tpoint_num = tpoint->number;
4884 status->tpoint_addr = tpoint->address;
4885 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
4886 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
4887 }
4888
4889 if (needs_breakpoint)
4890 {
4891 /* Hasn't executed the original instruction yet. Set breakpoint
4892 there, and wait till it's hit, then single-step until exiting
4893 the jump pad. */
4894
4895 trace_debug ("\
4896fast_tracepoint_collecting, returning continue-until-break at %s",
4897 paddress (tpoint->adjusted_insn_addr));
4898
4899 return 1; /* continue */
4900 }
4901 else
4902 {
4903 /* Just single-step until exiting the jump pad. */
4904
4905 trace_debug ("fast_tracepoint_collecting, returning "
4906 "need-single-step (%s-%s)",
4907 paddress (tpoint->adjusted_insn_addr),
4908 paddress (tpoint->adjusted_insn_addr_end));
4909
4910 return 2; /* single-step */
4911 }
4912}
4913
4914#endif
4915
4916#ifdef IN_PROCESS_AGENT
4917
4918/* The global fast tracepoint collect lock. Points to a collecting_t
4919 object built on the stack by the jump pad, if presently locked;
4920 NULL if it isn't locked. Note that this lock *must* be set while
4921 executing any *function other than the jump pad. See
4922 fast_tracepoint_collecting. */
4923static collecting_t * ATTR_USED collecting;
4924
4925/* This routine, called from the jump pad (in asm) is designed to be
4926 called from the jump pads of fast tracepoints, thus it is on the
4927 critical path. */
4928
4929IP_AGENT_EXPORT void ATTR_USED
4930gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
4931{
4932 struct fast_tracepoint_ctx ctx;
4933
4934 /* Don't do anything until the trace run is completely set up. */
4935 if (!tracing)
4936 return;
4937
4938 ctx.base.type = fast_tracepoint;
4939 ctx.regs = regs;
4940 ctx.regcache_initted = 0;
4941 ctx.tpoint = tpoint;
4942
4943 /* Wrap the regblock in a register cache (in the stack, we don't
4944 want to malloc here). */
4945 ctx.regspace = alloca (register_cache_size ());
4946 if (ctx.regspace == NULL)
4947 {
4948 trace_debug ("Trace buffer block allocation failed, skipping");
4949 return;
4950 }
4951
4952 /* Test the condition if present, and collect if true. */
4953 if (tpoint->cond == NULL
4954 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4955 tpoint))
4956 {
4957 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4958 tpoint->address, tpoint);
4959
4960 /* Note that this will cause original insns to be written back
4961 to where we jumped from, but that's OK because we're jumping
4962 back to the next whole instruction. This will go badly if
4963 instruction restoration is not atomic though. */
4964 if (stopping_tracepoint
4965 || trace_buffer_is_full
4966 || expr_eval_result != expr_eval_no_error)
4967 stop_tracing ();
4968 }
4969 else
4970 {
4971 /* If there was a condition and it evaluated to false, the only
4972 way we would stop tracing is if there was an error during
4973 condition expression evaluation. */
4974 if (expr_eval_result != expr_eval_no_error)
4975 stop_tracing ();
4976 }
4977}
4978
4979#endif
4980
4981#ifndef IN_PROCESS_AGENT
4982
6a271cae
PA
4983/* Bytecode compilation. */
4984
4985CORE_ADDR current_insn_ptr;
4986
4987int emit_error;
4988
4989struct bytecode_address
4990{
4991 int pc;
4992 CORE_ADDR address;
4993 int goto_pc;
4994 /* Offset and size of field to be modified in the goto block. */
4995 int from_offset, from_size;
4996 struct bytecode_address *next;
4997} *bytecode_address_table;
4998
4999CORE_ADDR
5000get_raw_reg_func_addr (void)
5001{
5002 return ipa_sym_addrs.addr_get_raw_reg;
5003}
5004
5005static void
5006emit_prologue (void)
5007{
5008 target_emit_ops ()->emit_prologue ();
5009}
5010
5011static void
5012emit_epilogue (void)
5013{
5014 target_emit_ops ()->emit_epilogue ();
5015}
5016
5017static void
5018emit_add (void)
5019{
5020 target_emit_ops ()->emit_add ();
5021}
5022
5023static void
5024emit_sub (void)
5025{
5026 target_emit_ops ()->emit_sub ();
5027}
5028
5029static void
5030emit_mul (void)
5031{
5032 target_emit_ops ()->emit_mul ();
5033}
5034
5035static void
5036emit_lsh (void)
5037{
5038 target_emit_ops ()->emit_lsh ();
5039}
5040
5041static void
5042emit_rsh_signed (void)
5043{
5044 target_emit_ops ()->emit_rsh_signed ();
5045}
5046
5047static void
5048emit_rsh_unsigned (void)
5049{
5050 target_emit_ops ()->emit_rsh_unsigned ();
5051}
5052
5053static void
5054emit_ext (int arg)
5055{
5056 target_emit_ops ()->emit_ext (arg);
5057}
5058
5059static void
5060emit_log_not (void)
5061{
5062 target_emit_ops ()->emit_log_not ();
5063}
5064
5065static void
5066emit_bit_and (void)
5067{
5068 target_emit_ops ()->emit_bit_and ();
5069}
5070
5071static void
5072emit_bit_or (void)
5073{
5074 target_emit_ops ()->emit_bit_or ();
5075}
5076
5077static void
5078emit_bit_xor (void)
5079{
5080 target_emit_ops ()->emit_bit_xor ();
5081}
5082
5083static void
5084emit_bit_not (void)
5085{
5086 target_emit_ops ()->emit_bit_not ();
5087}
5088
5089static void
5090emit_equal (void)
5091{
5092 target_emit_ops ()->emit_equal ();
5093}
5094
5095static void
5096emit_less_signed (void)
5097{
5098 target_emit_ops ()->emit_less_signed ();
5099}
5100
5101static void
5102emit_less_unsigned (void)
5103{
5104 target_emit_ops ()->emit_less_unsigned ();
5105}
5106
5107static void
5108emit_ref (int size)
5109{
5110 target_emit_ops ()->emit_ref (size);
5111}
5112
5113static void
5114emit_if_goto (int *offset_p, int *size_p)
5115{
5116 target_emit_ops ()->emit_if_goto (offset_p, size_p);
5117}
5118
5119static void
5120emit_goto (int *offset_p, int *size_p)
5121{
5122 target_emit_ops ()->emit_goto (offset_p, size_p);
5123}
5124
5125static void
5126write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
5127{
5128 target_emit_ops ()->write_goto_address (from, to, size);
5129}
5130
5131static void
4e29fb54 5132emit_const (LONGEST num)
6a271cae
PA
5133{
5134 target_emit_ops ()->emit_const (num);
5135}
5136
5137static void
5138emit_reg (int reg)
5139{
5140 target_emit_ops ()->emit_reg (reg);
5141}
5142
5143static void
5144emit_pop (void)
5145{
5146 target_emit_ops ()->emit_pop ();
5147}
5148
5149static void
5150emit_stack_flush (void)
5151{
5152 target_emit_ops ()->emit_stack_flush ();
5153}
5154
5155static void
5156emit_zero_ext (int arg)
5157{
5158 target_emit_ops ()->emit_zero_ext (arg);
5159}
5160
5161static void
5162emit_swap (void)
5163{
5164 target_emit_ops ()->emit_swap ();
5165}
5166
5167static void
5168emit_stack_adjust (int n)
5169{
5170 target_emit_ops ()->emit_stack_adjust (n);
5171}
5172
5173/* FN's prototype is `LONGEST(*fn)(int)'. */
5174
5175static void
5176emit_int_call_1 (CORE_ADDR fn, int arg1)
5177{
5178 target_emit_ops ()->emit_int_call_1 (fn, arg1);
5179}
5180
4e29fb54 5181/* FN's prototype is `void(*fn)(int,LONGEST)'. */
6a271cae
PA
5182
5183static void
5184emit_void_call_2 (CORE_ADDR fn, int arg1)
5185{
5186 target_emit_ops ()->emit_void_call_2 (fn, arg1);
5187}
5188
5189static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
5190
5191static void
5192compile_tracepoint_condition (struct tracepoint *tpoint, CORE_ADDR *jump_entry)
5193{
5194 CORE_ADDR entry_point = *jump_entry;
5195 enum eval_result_type err;
5196
5197 trace_debug ("Starting condition compilation for tracepoint %d\n",
5198 tpoint->number);
5199
5200 /* Initialize the global pointer to the code being built. */
5201 current_insn_ptr = *jump_entry;
5202
5203 emit_prologue ();
5204
5205 err = compile_bytecodes (tpoint->cond);
5206
5207 if (err == expr_eval_no_error)
5208 {
5209 emit_epilogue ();
5210
5211 /* Record the beginning of the compiled code. */
5212 tpoint->compiled_cond = entry_point;
5213
5214 trace_debug ("Condition compilation for tracepoint %d complete\n",
5215 tpoint->number);
5216 }
5217 else
5218 {
5219 /* Leave the unfinished code in situ, but don't point to it. */
5220
5221 tpoint->compiled_cond = 0;
5222
5223 trace_debug ("Condition compilation for tracepoint %d failed, "
5224 "error code %d",
5225 tpoint->number, err);
5226 }
5227
5228 /* Update the code pointer passed in. Note that we do this even if
5229 the compile fails, so that we can look at the partial results
5230 instead of letting them be overwritten. */
5231 *jump_entry = current_insn_ptr;
5232
5233 /* Leave a gap, to aid dump decipherment. */
5234 *jump_entry += 16;
5235}
5236
5237/* Given an agent expression, turn it into native code. */
5238
5239static enum eval_result_type
5240compile_bytecodes (struct agent_expr *aexpr)
5241{
5242 int pc = 0;
5243 int done = 0;
5244 unsigned char op;
5245 int arg;
5246 /* This is only used to build 64-bit value for constants. */
5247 ULONGEST top;
5248 struct bytecode_address *aentry, *aentry2;
5249
5250#define UNHANDLED \
5251 do \
5252 { \
5253 trace_debug ("Cannot compile op 0x%x\n", op); \
5254 return expr_eval_unhandled_opcode; \
5255 } while (0)
5256
5257 if (aexpr->length == 0)
5258 {
5259 trace_debug ("empty agent expression\n");
5260 return expr_eval_empty_expression;
5261 }
5262
5263 bytecode_address_table = NULL;
5264
5265 while (!done)
5266 {
5267 op = aexpr->bytes[pc];
5268
5269 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
5270
5271 /* Record the compiled-code address of the bytecode, for use by
5272 jump instructions. */
5273 aentry = xmalloc (sizeof (struct bytecode_address));
5274 aentry->pc = pc;
5275 aentry->address = current_insn_ptr;
5276 aentry->goto_pc = -1;
5277 aentry->from_offset = aentry->from_size = 0;
5278 aentry->next = bytecode_address_table;
5279 bytecode_address_table = aentry;
5280
5281 ++pc;
5282
5283 emit_error = 0;
5284
5285 switch (op)
5286 {
5287 case gdb_agent_op_add:
5288 emit_add ();
5289 break;
5290
5291 case gdb_agent_op_sub:
5292 emit_sub ();
5293 break;
5294
5295 case gdb_agent_op_mul:
5296 emit_mul ();
5297 break;
5298
5299 case gdb_agent_op_div_signed:
5300 UNHANDLED;
5301 break;
5302
5303 case gdb_agent_op_div_unsigned:
5304 UNHANDLED;
5305 break;
5306
5307 case gdb_agent_op_rem_signed:
5308 UNHANDLED;
5309 break;
5310
5311 case gdb_agent_op_rem_unsigned:
5312 UNHANDLED;
5313 break;
5314
5315 case gdb_agent_op_lsh:
5316 emit_lsh ();
5317 break;
5318
5319 case gdb_agent_op_rsh_signed:
5320 emit_rsh_signed ();
5321 break;
5322
5323 case gdb_agent_op_rsh_unsigned:
5324 emit_rsh_unsigned ();
5325 break;
5326
5327 case gdb_agent_op_trace:
5328 UNHANDLED;
5329 break;
5330
5331 case gdb_agent_op_trace_quick:
5332 UNHANDLED;
5333 break;
5334
5335 case gdb_agent_op_log_not:
5336 emit_log_not ();
5337 break;
5338
5339 case gdb_agent_op_bit_and:
5340 emit_bit_and ();
5341 break;
5342
5343 case gdb_agent_op_bit_or:
5344 emit_bit_or ();
5345 break;
5346
5347 case gdb_agent_op_bit_xor:
5348 emit_bit_xor ();
5349 break;
5350
5351 case gdb_agent_op_bit_not:
5352 emit_bit_not ();
5353 break;
5354
5355 case gdb_agent_op_equal:
5356 emit_equal ();
5357 break;
5358
5359 case gdb_agent_op_less_signed:
5360 emit_less_signed ();
5361 break;
5362
5363 case gdb_agent_op_less_unsigned:
5364 emit_less_unsigned ();
5365 break;
5366
5367 case gdb_agent_op_ext:
5368 arg = aexpr->bytes[pc++];
5369 if (arg < (sizeof (LONGEST) * 8))
5370 emit_ext (arg);
5371 break;
5372
5373 case gdb_agent_op_ref8:
5374 emit_ref (1);
5375 break;
5376
5377 case gdb_agent_op_ref16:
5378 emit_ref (2);
5379 break;
5380
5381 case gdb_agent_op_ref32:
5382 emit_ref (4);
5383 break;
5384
5385 case gdb_agent_op_ref64:
5386 emit_ref (8);
5387 break;
5388
5389 case gdb_agent_op_if_goto:
5390 arg = aexpr->bytes[pc++];
5391 arg = (arg << 8) + aexpr->bytes[pc++];
5392 aentry->goto_pc = arg;
5393 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
5394 break;
5395
5396 case gdb_agent_op_goto:
5397 arg = aexpr->bytes[pc++];
5398 arg = (arg << 8) + aexpr->bytes[pc++];
5399 aentry->goto_pc = arg;
5400 emit_goto (&(aentry->from_offset), &(aentry->from_size));
5401 break;
5402
5403 case gdb_agent_op_const8:
5404 emit_stack_flush ();
5405 top = aexpr->bytes[pc++];
5406 emit_const (top);
5407 break;
5408
5409 case gdb_agent_op_const16:
5410 emit_stack_flush ();
5411 top = aexpr->bytes[pc++];
5412 top = (top << 8) + aexpr->bytes[pc++];
5413 emit_const (top);
5414 break;
5415
5416 case gdb_agent_op_const32:
5417 emit_stack_flush ();
5418 top = aexpr->bytes[pc++];
5419 top = (top << 8) + aexpr->bytes[pc++];
5420 top = (top << 8) + aexpr->bytes[pc++];
5421 top = (top << 8) + aexpr->bytes[pc++];
5422 emit_const (top);
5423 break;
5424
5425 case gdb_agent_op_const64:
5426 emit_stack_flush ();
5427 top = aexpr->bytes[pc++];
5428 top = (top << 8) + aexpr->bytes[pc++];
5429 top = (top << 8) + aexpr->bytes[pc++];
5430 top = (top << 8) + aexpr->bytes[pc++];
5431 top = (top << 8) + aexpr->bytes[pc++];
5432 top = (top << 8) + aexpr->bytes[pc++];
5433 top = (top << 8) + aexpr->bytes[pc++];
5434 top = (top << 8) + aexpr->bytes[pc++];
5435 emit_const (top);
5436 break;
5437
5438 case gdb_agent_op_reg:
5439 emit_stack_flush ();
5440 arg = aexpr->bytes[pc++];
5441 arg = (arg << 8) + aexpr->bytes[pc++];
5442 emit_reg (arg);
5443 break;
5444
5445 case gdb_agent_op_end:
5446 trace_debug ("At end of expression\n");
5447
5448 /* Assume there is one stack element left, and that it is
5449 cached in "top" where emit_epilogue can get to it. */
5450 emit_stack_adjust (1);
5451
5452 done = 1;
5453 break;
5454
5455 case gdb_agent_op_dup:
5456 /* In our design, dup is equivalent to stack flushing. */
5457 emit_stack_flush ();
5458 break;
5459
5460 case gdb_agent_op_pop:
5461 emit_pop ();
5462 break;
5463
5464 case gdb_agent_op_zero_ext:
5465 arg = aexpr->bytes[pc++];
5466 if (arg < (sizeof (LONGEST) * 8))
5467 emit_zero_ext (arg);
5468 break;
5469
5470 case gdb_agent_op_swap:
5471 emit_swap ();
5472 break;
5473
5474 case gdb_agent_op_getv:
5475 emit_stack_flush ();
5476 arg = aexpr->bytes[pc++];
5477 arg = (arg << 8) + aexpr->bytes[pc++];
5478 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
5479 arg);
5480 break;
5481
5482 case gdb_agent_op_setv:
5483 arg = aexpr->bytes[pc++];
5484 arg = (arg << 8) + aexpr->bytes[pc++];
5485 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
5486 arg);
5487 break;
5488
5489 case gdb_agent_op_tracev:
5490 UNHANDLED;
5491 break;
5492
5493 /* GDB never (currently) generates any of these ops. */
5494 case gdb_agent_op_float:
5495 case gdb_agent_op_ref_float:
5496 case gdb_agent_op_ref_double:
5497 case gdb_agent_op_ref_long_double:
5498 case gdb_agent_op_l_to_d:
5499 case gdb_agent_op_d_to_l:
5500 case gdb_agent_op_trace16:
5501 UNHANDLED;
5502 break;
5503
5504 default:
5505 trace_debug ("Agent expression op 0x%x not recognized\n", op);
5506 /* Don't struggle on, things will just get worse. */
5507 return expr_eval_unrecognized_opcode;
5508 }
5509
5510 /* This catches errors that occur in target-specific code
5511 emission. */
5512 if (emit_error)
5513 {
5514 trace_debug ("Error %d while emitting code for %s\n",
5515 emit_error, gdb_agent_op_names[op]);
5516 return expr_eval_unhandled_opcode;
5517 }
5518
5519 trace_debug ("Op %s compiled\n", gdb_agent_op_names[op]);
5520 }
5521
5522 /* Now fill in real addresses as goto destinations. */
5523 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
5524 {
5525 int written = 0;
5526
5527 if (aentry->goto_pc < 0)
5528 continue;
5529
5530 /* Find the location that we are going to, and call back into
5531 target-specific code to write the actual address or
5532 displacement. */
5533 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
5534 {
5535 if (aentry2->pc == aentry->goto_pc)
5536 {
5537 trace_debug ("Want to jump from %s to %s\n",
5538 paddress (aentry->address),
5539 paddress (aentry2->address));
5540 write_goto_address (aentry->address + aentry->from_offset,
5541 aentry2->address, aentry->from_size);
5542 written = 1;
5543 break;
5544 }
5545 }
5546
5547 /* Error out if we didn't find a destination. */
5548 if (!written)
5549 {
5550 trace_debug ("Destination of goto %d not found\n",
5551 aentry->goto_pc);
5552 return expr_eval_invalid_goto;
5553 }
5554 }
5555
5556 return expr_eval_no_error;
5557}
5558
fa593d66
PA
5559/* We'll need to adjust these when we consider bi-arch setups, and big
5560 endian machines. */
5561
5562static int
5563write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5564{
5565 return write_inferior_memory (where,
5566 (unsigned char *) &ptr, sizeof (void *));
5567}
5568
5569/* The base pointer of the IPA's heap. This is the only memory the
5570 IPA is allowed to use. The IPA should _not_ call the inferior's
5571 `malloc' during operation. That'd be slow, and, most importantly,
5572 it may not be safe. We may be collecting a tracepoint in a signal
5573 handler, for example. */
5574static CORE_ADDR target_tp_heap;
5575
5576/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5577 to 8 bytes. */
5578
5579static CORE_ADDR
5580target_malloc (ULONGEST size)
5581{
5582 CORE_ADDR ptr;
5583
5584 if (target_tp_heap == 0)
5585 {
5586 /* We have the pointer *address*, need what it points to. */
5587 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5588 &target_tp_heap))
5589 fatal ("could get target heap head pointer");
5590 }
5591
5592 ptr = target_tp_heap;
5593 target_tp_heap += size;
5594
5595 /* Pad to 8-byte alignment. */
5596 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5597
5598 return ptr;
5599}
5600
5601static CORE_ADDR
5602download_agent_expr (struct agent_expr *expr)
5603{
5604 CORE_ADDR expr_addr;
5605 CORE_ADDR expr_bytes;
5606
5607 expr_addr = target_malloc (sizeof (*expr));
5608 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5609
5610 expr_bytes = target_malloc (expr->length);
5611 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5612 expr_bytes);
5613 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5614
5615 return expr_addr;
5616}
5617
5618/* Align V up to N bits. */
5619#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5620
5621static void
5622download_tracepoints (void)
5623{
5624 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5625 struct tracepoint *tpoint;
5626
5627 /* Start out empty. */
5628 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
5629
5630 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5631 {
5632 struct tracepoint target_tracepoint;
5633
5634 if (tpoint->type != fast_tracepoint)
5635 continue;
5636
6a271cae
PA
5637 /* Maybe download a compiled condition. */
5638 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5639 {
5640 CORE_ADDR jentry, jump_entry;
5641
5642 jentry = jump_entry = get_jump_space_head ();
5643
5644 if (tpoint->cond != NULL)
5645 {
5646 /* Pad to 8-byte alignment. (needed?) */
5647 /* Actually this should be left for the target to
5648 decide. */
5649 jentry = UALIGN (jentry, 8);
5650
5651 compile_tracepoint_condition (tpoint, &jentry);
5652 }
5653
5654 /* Pad to 8-byte alignment. */
5655 jentry = UALIGN (jentry, 8);
5656 claim_jump_space (jentry - jump_entry);
5657 }
5658
fa593d66
PA
5659 target_tracepoint = *tpoint;
5660
5661 prev_tpptr = tpptr;
5662 tpptr = target_malloc (sizeof (*tpoint));
5663 tpoint->obj_addr_on_target = tpptr;
5664
5665 if (tpoint == tracepoints)
5666 {
5667 /* First object in list, set the head pointer in the
5668 inferior. */
5669 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
5670 }
5671 else
5672 {
5673 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
5674 next),
5675 tpptr);
5676 }
5677
5678 /* Write the whole object. We'll fix up its pointers in a bit.
5679 Assume no next for now. This is fixed up above on the next
5680 iteration, if there's any. */
5681 target_tracepoint.next = NULL;
5682 /* Need to clear this here too, since we're downloading the
5683 tracepoints before clearing our own copy. */
5684 target_tracepoint.hit_count = 0;
5685
5686 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5687 sizeof (target_tracepoint));
5688
5689 if (tpoint->cond)
5690 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5691 cond),
5692 download_agent_expr (tpoint->cond));
5693
5694 if (tpoint->numactions)
5695 {
5696 int i;
5697 CORE_ADDR actions_array;
5698
5699 /* The pointers array. */
5700 actions_array
5701 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5702 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5703 actions),
5704 actions_array);
5705
5706 /* Now for each pointer, download the action. */
5707 for (i = 0; i < tpoint->numactions; i++)
5708 {
5709 CORE_ADDR ipa_action = 0;
5710 struct tracepoint_action *action = tpoint->actions[i];
5711
5712 switch (action->type)
5713 {
5714 case 'M':
5715 ipa_action
5716 = target_malloc (sizeof (struct collect_memory_action));
5717 write_inferior_memory (ipa_action,
5718 (unsigned char *) action,
5719 sizeof (struct collect_memory_action));
5720 break;
5721 case 'R':
5722 ipa_action
5723 = target_malloc (sizeof (struct collect_registers_action));
5724 write_inferior_memory (ipa_action,
5725 (unsigned char *) action,
5726 sizeof (struct collect_registers_action));
5727 break;
5728 case 'X':
5729 {
5730 CORE_ADDR expr;
5731 struct eval_expr_action *eaction
5732 = (struct eval_expr_action *) action;
5733
5734 ipa_action = target_malloc (sizeof (*eaction));
5735 write_inferior_memory (ipa_action,
5736 (unsigned char *) eaction,
5737 sizeof (*eaction));
5738
5739 expr = download_agent_expr (eaction->expr);
5740 write_inferior_data_ptr
5741 (ipa_action + offsetof (struct eval_expr_action, expr),
5742 expr);
5743 break;
5744 }
5745 default:
5746 trace_debug ("unknown trace action '%c', ignoring",
5747 action->type);
5748 break;
5749 }
5750
5751 if (ipa_action != 0)
5752 write_inferior_data_ptr
5753 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5754 ipa_action);
5755 }
5756 }
5757 }
5758}
5759
5760static void
5761download_trace_state_variables (void)
5762{
5763 CORE_ADDR ptr = 0, prev_ptr = 0;
5764 struct trace_state_variable *tsv;
5765
5766 /* Start out empty. */
5767 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5768
5769 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5770 {
5771 struct trace_state_variable target_tsv;
5772
5773 /* TSV's with a getter have been initialized equally in both the
5774 inferior and GDBserver. Skip them. */
5775 if (tsv->getter != NULL)
5776 continue;
5777
5778 target_tsv = *tsv;
5779
5780 prev_ptr = ptr;
5781 ptr = target_malloc (sizeof (*tsv));
5782
5783 if (tsv == trace_state_variables)
5784 {
5785 /* First object in list, set the head pointer in the
5786 inferior. */
5787
5788 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5789 ptr);
5790 }
5791 else
5792 {
5793 write_inferior_data_ptr (prev_ptr
5794 + offsetof (struct trace_state_variable,
5795 next),
5796 ptr);
5797 }
5798
5799 /* Write the whole object. We'll fix up its pointers in a bit.
5800 Assume no next, fixup when needed. */
5801 target_tsv.next = NULL;
5802
5803 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5804 sizeof (target_tsv));
5805
5806 if (tsv->name != NULL)
5807 {
5808 size_t size = strlen (tsv->name) + 1;
5809 CORE_ADDR name_addr = target_malloc (size);
5810 write_inferior_memory (name_addr,
5811 (unsigned char *) tsv->name, size);
5812 write_inferior_data_ptr (ptr
5813 + offsetof (struct trace_state_variable,
5814 name),
5815 name_addr);
5816 }
5817
5818 if (tsv->getter != NULL)
5819 {
5820 fatal ("what to do with these?");
5821 }
5822 }
5823
5824 if (prev_ptr != 0)
5825 {
5826 /* Fixup the next pointer in the last item in the list. */
5827 write_inferior_data_ptr (prev_ptr + offsetof (struct trace_state_variable,
5828 next), 0);
5829 }
5830}
5831
5832/* Upload complete trace frames out of the IP Agent's trace buffer
5833 into GDBserver's trace buffer. This always uploads either all or
5834 no trace frames. This is the counter part of
5835 `trace_alloc_trace_buffer'. See its description of the atomic
5836 synching mechanism. */
5837
5838static void
5839upload_fast_traceframes (void)
5840{
5841 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5842 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5843 CORE_ADDR tf;
5844 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5845 unsigned int curr_tbctrl_idx;
5846 unsigned int ipa_trace_buffer_ctrl_curr;
5847 unsigned int ipa_trace_buffer_ctrl_curr_old;
5848 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5849 struct breakpoint *about_to_request_buffer_space_bkpt;
5850 CORE_ADDR ipa_trace_buffer_lo;
5851 CORE_ADDR ipa_trace_buffer_hi;
5852
5853 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5854 &ipa_traceframe_read_count_racy))
5855 {
5856 /* This will happen in most targets if the current thread is
5857 running. */
5858 return;
5859 }
5860
5861 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5862 &ipa_traceframe_write_count_racy))
5863 return;
5864
5865 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5866 ipa_traceframe_write_count_racy - ipa_traceframe_read_count_racy,
5867 ipa_traceframe_write_count_racy, ipa_traceframe_read_count_racy);
5868
5869 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5870 return;
5871
5872 about_to_request_buffer_space_bkpt
5873 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5874 NULL);
5875
5876 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5877 &ipa_trace_buffer_ctrl_curr))
5878 return;
5879
5880 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
5881
5882 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
5883
5884 {
5885 unsigned int prev, counter;
5886
5887 /* Update the token, with new counters, and the GDBserver stamp
5888 bit. Alway reuse the current TBC index. */
5889 prev = ipa_trace_buffer_ctrl_curr & 0x0007ff00;
5890 counter = (prev + 0x100) & 0x0007ff00;
5891
5892 ipa_trace_buffer_ctrl_curr = (0x80000000
5893 | (prev << 12)
5894 | counter
5895 | curr_tbctrl_idx);
5896 }
5897
5898 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5899 ipa_trace_buffer_ctrl_curr))
5900 return;
5901
5902 trace_debug ("Lib: Committed %08x -> %08x",
5903 ipa_trace_buffer_ctrl_curr_old,
5904 ipa_trace_buffer_ctrl_curr);
5905
5906 /* Re-read these, now that we've installed the
5907 `about_to_request_buffer_space' breakpoint/lock. A thread could
5908 have finished a traceframe between the last read of these
5909 counters and setting the breakpoint above. If we start
5910 uploading, we never want to leave this function with
5911 traceframe_read_count != 0, otherwise, GDBserver could end up
5912 incrementing the counter tokens more than once (due to event loop
5913 nesting), which would break the IP agent's "effective" detection
5914 (see trace_alloc_trace_buffer). */
5915 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5916 &ipa_traceframe_read_count))
5917 return;
5918 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5919 &ipa_traceframe_write_count))
5920 return;
5921
5922 if (debug_threads)
5923 {
5924 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5925 ipa_traceframe_write_count - ipa_traceframe_read_count,
5926 ipa_traceframe_write_count, ipa_traceframe_read_count);
5927
5928 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
5929 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
5930 trace_debug ("note that ipa_traceframe_count's parts changed");
5931 }
5932
5933 /* Get the address of the current TBC object (the IP agent has an
5934 array of 3 such objects). The index is stored in the TBC
5935 token. */
5936 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
5937 ipa_trace_buffer_ctrl_addr
5938 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
5939
5940 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
5941 (unsigned char *) &ipa_trace_buffer_ctrl,
5942 sizeof (struct ipa_trace_buffer_control)))
5943 return;
5944
5945 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
5946 &ipa_trace_buffer_lo))
5947 return;
5948 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
5949 &ipa_trace_buffer_hi))
5950 return;
5951
5952 /* Offsets are easier to grok for debugging than raw addresses,
5953 especially for the small trace buffer sizes that are useful for
5954 testing. */
5955 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
5956 "endfree=%d wrap=%d hi=%d",
5957 curr_tbctrl_idx,
5958 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
5959 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
5960 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
5961 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
5962 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
5963
5964 /* Note that the IPA's buffer is always circular. */
5965
5966#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
5967
5968#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
5969 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
5970
5971#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
5972 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
5973 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
5974 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
5975 : 0))
5976
5977 tf = IPA_FIRST_TRACEFRAME ();
5978
5979 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
5980 {
5981 struct tracepoint *tpoint;
5982 struct traceframe *tframe;
5983 unsigned char *block;
5984 struct traceframe ipa_tframe;
5985
5986 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
5987 offsetof (struct traceframe, data)))
5988 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
5989
5990 if (ipa_tframe.tpnum == 0)
5991 fatal ("Uploading: No (more) fast traceframes, but "
5992 "ipa_traceframe_count == %u??\n",
5993 ipa_traceframe_write_count - ipa_traceframe_read_count);
5994
5995 /* Note that this will be incorrect for multi-location
5996 tracepoints... */
5997 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
5998
5999 tframe = add_traceframe (tpoint);
6000 if (tframe == NULL)
6001 {
6002 trace_buffer_is_full = 1;
6003 trace_debug ("Uploading: trace buffer is full");
6004 }
6005 else
6006 {
6007 /* Copy the whole set of blocks in one go for now. FIXME:
6008 split this in smaller blocks. */
6009 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6010 if (block != NULL)
6011 {
6012 if (read_inferior_memory (tf + offsetof (struct traceframe, data),
6013 block, ipa_tframe.data_size))
6014 error ("Uploading: Couldn't read traceframe data at %s\n",
6015 paddress (tf + offsetof (struct traceframe, data)));
6016 }
6017
6018 trace_debug ("Uploading: traceframe didn't fit");
6019 finish_traceframe (tframe);
6020 }
6021
6022 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6023
6024 /* If we freed the traceframe that wrapped around, go back
6025 to the non-wrap case. */
6026 if (tf < ipa_trace_buffer_ctrl.start)
6027 {
6028 trace_debug ("Lib: Discarding past the wraparound");
6029 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6030 }
6031 ipa_trace_buffer_ctrl.start = tf;
6032 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6033 ++ipa_traceframe_read_count;
6034
6035 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6036 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6037 {
6038 trace_debug ("Lib: buffer is fully empty. "
6039 "Trace buffer [%d] start=%d free=%d endfree=%d",
6040 curr_tbctrl_idx,
6041 (int) (ipa_trace_buffer_ctrl.start
6042 - ipa_trace_buffer_lo),
6043 (int) (ipa_trace_buffer_ctrl.free
6044 - ipa_trace_buffer_lo),
6045 (int) (ipa_trace_buffer_ctrl.end_free
6046 - ipa_trace_buffer_lo));
6047
6048 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6049 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6050 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6051 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6052 }
6053
6054 trace_debug ("Uploaded a traceframe\n"
6055 "Lib: Trace buffer [%d] start=%d free=%d "
6056 "endfree=%d wrap=%d hi=%d",
6057 curr_tbctrl_idx,
6058 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6059 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6060 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6061 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6062 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6063 }
6064
6065 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6066 (unsigned char *) &ipa_trace_buffer_ctrl,
6067 sizeof (struct ipa_trace_buffer_control)))
6068 return;
6069
6070 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6071 ipa_traceframe_read_count);
6072
6073 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6074
6075 pause_all (1);
6076 cancel_breakpoints ();
6077
6078 delete_breakpoint (about_to_request_buffer_space_bkpt);
6079 about_to_request_buffer_space_bkpt = NULL;
6080
6081 unpause_all (1);
6082
6083 if (trace_buffer_is_full)
6084 stop_tracing ();
6085}
6086#endif
6087
6088#ifdef IN_PROCESS_AGENT
6089
6090#include <sys/mman.h>
6091#include <fcntl.h>
6092
6093IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6094IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6095IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
6096
6097static void __attribute__ ((constructor))
6098initialize_tracepoint_ftlib (void)
6099{
6100 initialize_tracepoint ();
6101}
6102
6103#endif
6104
219f2f23
PA
6105static LONGEST
6106tsv_get_timestamp (void)
6107{
6108 struct timeval tv;
6109
6110 if (gettimeofday (&tv, 0) != 0)
6111 return -1;
6112 else
6113 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6114}
6115
6116void
6117initialize_tracepoint (void)
6118{
6119 /* There currently no way to change the buffer size. */
6120 const int sizeOfBuffer = 5 * 1024 * 1024;
6121 unsigned char *buf = xmalloc (sizeOfBuffer);
6122 init_trace_buffer (buf, sizeOfBuffer);
6123
6124 /* Wire trace state variable 1 to be the timestamp. This will be
6125 uploaded to GDB upon connection and become one of its trace state
6126 variables. (In case you're wondering, if GDB already has a trace
6127 variable numbered 1, it will be renumbered.) */
fa593d66 6128 create_trace_state_variable (1, 0);
219f2f23
PA
6129 set_trace_state_variable_name (1, "trace_timestamp");
6130 set_trace_state_variable_getter (1, tsv_get_timestamp);
fa593d66
PA
6131
6132#ifdef IN_PROCESS_AGENT
6133 {
6134 int pagesize;
6135 pagesize = sysconf (_SC_PAGE_SIZE);
6136 if (pagesize == -1)
6137 fatal ("sysconf");
6138
6139 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
6140
6141 /* Allocate scratch buffer aligned on a page boundary. */
6142 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
6143 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
6144
6145 /* Make it writable and executable. */
6146 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
6147 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
6148 fatal ("\
6149initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
6150 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
6151 }
6152
6153 initialize_low_tracepoint ();
6154#endif
219f2f23 6155}
This page took 0.290328 seconds and 4 git commands to generate.