message.txt
[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
6a271cae
PA
3926 if (tpoint->compiled_cond)
3927 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
3928 else
3929 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
219f2f23
PA
3930
3931 if (err != expr_eval_no_error)
3932 {
3933 record_tracepoint_error (tpoint, "condition", err);
3934 /* The error case must return false. */
3935 return 0;
3936 }
3937
3938 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
3939 tpoint->number, paddress (tpoint->address),
3940 pulongest (value));
3941 return (value ? 1 : 0);
3942}
3943
fa593d66
PA
3944#ifndef IN_PROCESS_AGENT
3945
219f2f23
PA
3946/* The packet form of an agent expression consists of an 'X', number
3947 of bytes in expression, a comma, and then the bytes. */
3948
3949static struct agent_expr *
3950parse_agent_expr (char **actparm)
3951{
3952 char *act = *actparm;
3953 ULONGEST xlen;
3954 struct agent_expr *aexpr;
3955
3956 ++act; /* skip the X */
3957 act = unpack_varlen_hex (act, &xlen);
3958 ++act; /* skip a comma */
3959 aexpr = xmalloc (sizeof (struct agent_expr));
3960 aexpr->length = xlen;
3961 aexpr->bytes = xmalloc (xlen);
3962 convert_ascii_to_int (act, aexpr->bytes, xlen);
3963 *actparm = act + (xlen * 2);
3964 return aexpr;
3965}
3966
3967/* Convert the bytes of an agent expression back into hex digits, so
3968 they can be printed or uploaded. This allocates the buffer,
3969 callers should free when they are done with it. */
3970
3971static char *
3972unparse_agent_expr (struct agent_expr *aexpr)
3973{
3974 char *rslt;
3975
3976 rslt = xmalloc (2 * aexpr->length + 1);
3977 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
3978 return rslt;
3979}
3980
fa593d66
PA
3981#endif
3982
219f2f23
PA
3983/* The agent expression evaluator, as specified by the GDB docs. It
3984 returns 0 if everything went OK, and a nonzero error code
3985 otherwise. */
3986
3987static enum eval_result_type
3988eval_agent_expr (struct tracepoint_hit_ctx *ctx,
3989 struct traceframe *tframe,
3990 struct agent_expr *aexpr,
3991 ULONGEST *rslt)
3992{
3993 int pc = 0;
3994#define STACK_MAX 100
3995 ULONGEST stack[STACK_MAX], top;
3996 int sp = 0;
3997 unsigned char op;
3998 int arg;
3999
4000 /* This union is a convenient way to convert representations. For
4001 now, assume a standard architecture where the hardware integer
4002 types have 8, 16, 32, 64 bit types. A more robust solution would
4003 be to import stdint.h from gnulib. */
4004 union
4005 {
4006 union
4007 {
4008 unsigned char bytes[1];
4009 unsigned char val;
4010 } u8;
4011 union
4012 {
4013 unsigned char bytes[2];
4014 unsigned short val;
4015 } u16;
4016 union
4017 {
4018 unsigned char bytes[4];
4019 unsigned int val;
4020 } u32;
4021 union
4022 {
4023 unsigned char bytes[8];
4024 ULONGEST val;
4025 } u64;
4026 } cnv;
4027
4028 if (aexpr->length == 0)
4029 {
4030 trace_debug ("empty agent expression");
4031 return expr_eval_empty_expression;
4032 }
4033
4034 /* Cache the stack top in its own variable. Much of the time we can
4035 operate on this variable, rather than dinking with the stack. It
4036 needs to be copied to the stack when sp changes. */
4037 top = 0;
4038
4039 while (1)
4040 {
4041 op = aexpr->bytes[pc++];
4042
4043 trace_debug ("About to interpret byte 0x%x", op);
4044
4045 switch (op)
4046 {
4047 case gdb_agent_op_add:
4048 top += stack[--sp];
4049 break;
4050
4051 case gdb_agent_op_sub:
4052 top = stack[--sp] - top;
4053 break;
4054
4055 case gdb_agent_op_mul:
4056 top *= stack[--sp];
4057 break;
4058
4059 case gdb_agent_op_div_signed:
4060 if (top == 0)
4061 {
4062 trace_debug ("Attempted to divide by zero");
4063 return expr_eval_divide_by_zero;
4064 }
4065 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4066 break;
4067
4068 case gdb_agent_op_div_unsigned:
4069 if (top == 0)
4070 {
4071 trace_debug ("Attempted to divide by zero");
4072 return expr_eval_divide_by_zero;
4073 }
4074 top = stack[--sp] / top;
4075 break;
4076
4077 case gdb_agent_op_rem_signed:
4078 if (top == 0)
4079 {
4080 trace_debug ("Attempted to divide by zero");
4081 return expr_eval_divide_by_zero;
4082 }
4083 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4084 break;
4085
4086 case gdb_agent_op_rem_unsigned:
4087 if (top == 0)
4088 {
4089 trace_debug ("Attempted to divide by zero");
4090 return expr_eval_divide_by_zero;
4091 }
4092 top = stack[--sp] % top;
4093 break;
4094
4095 case gdb_agent_op_lsh:
4096 top = stack[--sp] << top;
4097 break;
4098
4099 case gdb_agent_op_rsh_signed:
4100 top = ((LONGEST) stack[--sp]) >> top;
4101 break;
4102
4103 case gdb_agent_op_rsh_unsigned:
4104 top = stack[--sp] >> top;
4105 break;
4106
4107 case gdb_agent_op_trace:
4108 agent_mem_read (tframe,
4109 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4110 if (--sp >= 0)
4111 top = stack[sp];
4112 break;
4113
4114 case gdb_agent_op_trace_quick:
4115 arg = aexpr->bytes[pc++];
4116 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4117 break;
4118
4119 case gdb_agent_op_log_not:
4120 top = !top;
4121 break;
4122
4123 case gdb_agent_op_bit_and:
4124 top &= stack[--sp];
4125 break;
4126
4127 case gdb_agent_op_bit_or:
4128 top |= stack[--sp];
4129 break;
4130
4131 case gdb_agent_op_bit_xor:
4132 top ^= stack[--sp];
4133 break;
4134
4135 case gdb_agent_op_bit_not:
4136 top = ~top;
4137 break;
4138
4139 case gdb_agent_op_equal:
4140 top = (stack[--sp] == top);
4141 break;
4142
4143 case gdb_agent_op_less_signed:
4144 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4145 break;
4146
4147 case gdb_agent_op_less_unsigned:
4148 top = (stack[--sp] < top);
4149 break;
4150
4151 case gdb_agent_op_ext:
4152 arg = aexpr->bytes[pc++];
4153 if (arg < (sizeof (LONGEST) * 8))
4154 {
4155 LONGEST mask = 1 << (arg - 1);
4156 top &= ((LONGEST) 1 << arg) - 1;
4157 top = (top ^ mask) - mask;
4158 }
4159 break;
4160
4161 case gdb_agent_op_ref8:
4162 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4163 top = cnv.u8.val;
4164 break;
4165
4166 case gdb_agent_op_ref16:
4167 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4168 top = cnv.u16.val;
4169 break;
4170
4171 case gdb_agent_op_ref32:
4172 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4173 top = cnv.u32.val;
4174 break;
4175
4176 case gdb_agent_op_ref64:
4177 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4178 top = cnv.u64.val;
4179 break;
4180
4181 case gdb_agent_op_if_goto:
4182 if (top)
4183 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4184 else
4185 pc += 2;
4186 if (--sp >= 0)
4187 top = stack[sp];
4188 break;
4189
4190 case gdb_agent_op_goto:
4191 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4192 break;
4193
4194 case gdb_agent_op_const8:
4195 /* Flush the cached stack top. */
4196 stack[sp++] = top;
4197 top = aexpr->bytes[pc++];
4198 break;
4199
4200 case gdb_agent_op_const16:
4201 /* Flush the cached stack top. */
4202 stack[sp++] = top;
4203 top = aexpr->bytes[pc++];
4204 top = (top << 8) + aexpr->bytes[pc++];
4205 break;
4206
4207 case gdb_agent_op_const32:
4208 /* Flush the cached stack top. */
4209 stack[sp++] = top;
4210 top = aexpr->bytes[pc++];
4211 top = (top << 8) + aexpr->bytes[pc++];
4212 top = (top << 8) + aexpr->bytes[pc++];
4213 top = (top << 8) + aexpr->bytes[pc++];
4214 break;
4215
4216 case gdb_agent_op_const64:
4217 /* Flush the cached stack top. */
4218 stack[sp++] = top;
4219 top = aexpr->bytes[pc++];
4220 top = (top << 8) + aexpr->bytes[pc++];
4221 top = (top << 8) + aexpr->bytes[pc++];
4222 top = (top << 8) + aexpr->bytes[pc++];
4223 top = (top << 8) + aexpr->bytes[pc++];
4224 top = (top << 8) + aexpr->bytes[pc++];
4225 top = (top << 8) + aexpr->bytes[pc++];
4226 top = (top << 8) + aexpr->bytes[pc++];
4227 break;
4228
4229 case gdb_agent_op_reg:
4230 /* Flush the cached stack top. */
4231 stack[sp++] = top;
4232 arg = aexpr->bytes[pc++];
4233 arg = (arg << 8) + aexpr->bytes[pc++];
4234 {
4235 int regnum = arg;
4236 struct regcache *regcache;
4237
4238 regcache = get_context_regcache (ctx);
4239
4240 switch (register_size (regnum))
4241 {
4242 case 8:
4243 collect_register (regcache, regnum, cnv.u64.bytes);
4244 top = cnv.u64.val;
4245 break;
4246 case 4:
4247 collect_register (regcache, regnum, cnv.u32.bytes);
4248 top = cnv.u32.val;
4249 break;
4250 case 2:
4251 collect_register (regcache, regnum, cnv.u16.bytes);
4252 top = cnv.u16.val;
4253 break;
4254 case 1:
4255 collect_register (regcache, regnum, cnv.u8.bytes);
4256 top = cnv.u8.val;
4257 break;
4258 default:
4259 internal_error (__FILE__, __LINE__,
4260 "unhandled register size");
4261 }
4262 }
4263 break;
4264
4265 case gdb_agent_op_end:
4266 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4267 sp, pulongest (top));
4268 if (rslt)
4269 {
4270 if (sp <= 0)
4271 {
4272 /* This should be an error */
4273 trace_debug ("Stack is empty, nothing to return");
4274 return expr_eval_empty_stack;
4275 }
4276 *rslt = top;
4277 }
4278 return expr_eval_no_error;
4279
4280 case gdb_agent_op_dup:
4281 stack[sp++] = top;
4282 break;
4283
4284 case gdb_agent_op_pop:
4285 if (--sp >= 0)
4286 top = stack[sp];
4287 break;
4288
4289 case gdb_agent_op_zero_ext:
4290 arg = aexpr->bytes[pc++];
4291 if (arg < (sizeof (LONGEST) * 8))
4292 top &= ((LONGEST) 1 << arg) - 1;
4293 break;
4294
4295 case gdb_agent_op_swap:
4296 /* Interchange top two stack elements, making sure top gets
4297 copied back onto stack. */
4298 stack[sp] = top;
4299 top = stack[sp - 1];
4300 stack[sp - 1] = stack[sp];
4301 break;
4302
4303 case gdb_agent_op_getv:
4304 /* Flush the cached stack top. */
4305 stack[sp++] = top;
4306 arg = aexpr->bytes[pc++];
4307 arg = (arg << 8) + aexpr->bytes[pc++];
4308 top = get_trace_state_variable_value (arg);
4309 break;
4310
4311 case gdb_agent_op_setv:
4312 arg = aexpr->bytes[pc++];
4313 arg = (arg << 8) + aexpr->bytes[pc++];
4314 set_trace_state_variable_value (arg, top);
4315 /* Note that we leave the value on the stack, for the
4316 benefit of later/enclosing expressions. */
4317 break;
4318
4319 case gdb_agent_op_tracev:
4320 arg = aexpr->bytes[pc++];
4321 arg = (arg << 8) + aexpr->bytes[pc++];
4322 agent_tsv_read (tframe, arg);
4323 break;
4324
4325 /* GDB never (currently) generates any of these ops. */
4326 case gdb_agent_op_float:
4327 case gdb_agent_op_ref_float:
4328 case gdb_agent_op_ref_double:
4329 case gdb_agent_op_ref_long_double:
4330 case gdb_agent_op_l_to_d:
4331 case gdb_agent_op_d_to_l:
4332 case gdb_agent_op_trace16:
4333 trace_debug ("Agent expression op 0x%x valid, but not handled",
4334 op);
4335 /* If ever GDB generates any of these, we don't have the
4336 option of ignoring. */
4337 return 1;
4338
4339 default:
4340 trace_debug ("Agent expression op 0x%x not recognized", op);
4341 /* Don't struggle on, things will just get worse. */
4342 return expr_eval_unrecognized_opcode;
4343 }
4344
4345 /* Check for stack badness. */
4346 if (sp >= (STACK_MAX - 1))
4347 {
4348 trace_debug ("Expression stack overflow");
4349 return expr_eval_stack_overflow;
4350 }
4351
4352 if (sp < 0)
4353 {
4354 trace_debug ("Expression stack underflow");
4355 return expr_eval_stack_underflow;
4356 }
4357
4358 trace_debug ("Op %s -> sp=%d, top=0x%s",
4359 gdb_agent_op_names[op], sp, pulongest (top));
4360 }
4361}
4362
4363/* Do memory copies for bytecodes. */
4364/* Do the recording of memory blocks for actions and bytecodes. */
4365
4366static int
4367agent_mem_read (struct traceframe *tframe,
4368 unsigned char *to, CORE_ADDR from, ULONGEST len)
4369{
4370 unsigned char *mspace;
4371 ULONGEST remaining = len;
4372 unsigned short blocklen;
4373
4374 /* If a 'to' buffer is specified, use it. */
4375 if (to != NULL)
4376 {
4377 read_inferior_memory (from, to, len);
4378 return 0;
4379 }
4380
4381 /* Otherwise, create a new memory block in the trace buffer. */
4382 while (remaining > 0)
4383 {
4384 size_t sp;
4385
4386 blocklen = (remaining > 65535 ? 65535 : remaining);
4387 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4388 mspace = add_traceframe_block (tframe, sp);
4389 if (mspace == NULL)
4390 return 1;
4391 /* Identify block as a memory block. */
4392 *mspace = 'M';
4393 ++mspace;
4394 /* Record address and size. */
4395 memcpy (mspace, &from, sizeof (from));
4396 mspace += sizeof (from);
4397 memcpy (mspace, &blocklen, sizeof (blocklen));
4398 mspace += sizeof (blocklen);
4399 /* Record the memory block proper. */
4400 read_inferior_memory (from, mspace, blocklen);
4401 trace_debug ("%d bytes recorded", blocklen);
4402 remaining -= blocklen;
4403 from += blocklen;
4404 }
4405 return 0;
4406}
4407
4408/* Record the value of a trace state variable. */
4409
4410static int
4411agent_tsv_read (struct traceframe *tframe, int n)
4412{
4413 unsigned char *vspace;
4414 LONGEST val;
4415
4416 vspace = add_traceframe_block (tframe,
4417 1 + sizeof (n) + sizeof (LONGEST));
4418 if (vspace == NULL)
4419 return 1;
4420 /* Identify block as a variable. */
4421 *vspace = 'V';
4422 /* Record variable's number and value. */
4423 memcpy (vspace + 1, &n, sizeof (n));
4424 val = get_trace_state_variable_value (n);
4425 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4426 trace_debug ("Variable %d recorded", n);
4427 return 0;
4428}
4429
fa593d66
PA
4430#ifndef IN_PROCESS_AGENT
4431
219f2f23
PA
4432static unsigned char *
4433traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4434 int tfnum, char type_wanted)
4435{
4436 unsigned char *dataptr;
4437
4438 if (datasize == 0)
4439 {
4440 trace_debug ("traceframe %d has no data", tfnum);
4441 return NULL;
4442 }
4443
4444 /* Iterate through a traceframe's blocks, looking for a block of the
4445 requested type. */
4446 for (dataptr = database;
4447 dataptr < database + datasize;
4448 /* nothing */)
4449 {
4450 char blocktype;
4451 unsigned short mlen;
4452
4453 if (dataptr == trace_buffer_wrap)
4454 {
4455 /* Adjust to reflect wrapping part of the frame around to
4456 the beginning. */
4457 datasize = dataptr - database;
4458 dataptr = database = trace_buffer_lo;
4459 }
4460 blocktype = *dataptr++;
4461
4462 if (type_wanted == blocktype)
4463 return dataptr;
4464
4465 switch (blocktype)
4466 {
4467 case 'R':
4468 /* Skip over the registers block. */
4469 dataptr += register_cache_size ();
4470 break;
4471 case 'M':
4472 /* Skip over the memory block. */
4473 dataptr += sizeof (CORE_ADDR);
4474 memcpy (&mlen, dataptr, sizeof (mlen));
4475 dataptr += (sizeof (mlen) + mlen);
4476 break;
219f2f23
PA
4477 case 'V':
4478 /* Skip over the TSV block. */
4479 dataptr += (sizeof (int) + sizeof (LONGEST));
4480 break;
4481 default:
4482 trace_debug ("traceframe %d has unknown block type 0x%x",
4483 tfnum, blocktype);
4484 return NULL;
4485 }
4486 }
4487
4488 return NULL;
4489}
4490
4491static unsigned char *
4492traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4493{
4494 unsigned char *regblock;
4495
4496 regblock = traceframe_find_block_type (tframe->data,
4497 tframe->data_size,
4498 tfnum, 'R');
4499
4500 if (regblock == NULL)
4501 trace_debug ("traceframe %d has no register data", tfnum);
4502
4503 return regblock;
4504}
4505
4506/* Get registers from a traceframe. */
4507
4508int
4509fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4510{
4511 unsigned char *dataptr;
4512 struct tracepoint *tpoint;
4513 struct traceframe *tframe;
4514
4515 tframe = find_traceframe (tfnum);
4516
4517 if (tframe == NULL)
4518 {
4519 trace_debug ("traceframe %d not found", tfnum);
4520 return 1;
4521 }
4522
4523 dataptr = traceframe_find_regblock (tframe, tfnum);
4524 if (dataptr == NULL)
4525 {
4526 /* We don't like making up numbers, but GDB has all manner of
4527 troubles when the target says there are no registers. */
4528 supply_regblock (regcache, NULL);
4529
4530 /* We can generally guess at a PC, although this will be
4531 misleading for while-stepping frames and multi-location
4532 tracepoints. */
4533 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4534 if (tpoint != NULL)
4535 regcache_write_pc (regcache, tpoint->address);
4536 }
4537 else
4538 supply_regblock (regcache, dataptr);
4539
4540 return 0;
4541}
4542
4543static CORE_ADDR
4544traceframe_get_pc (struct traceframe *tframe)
4545{
4546 struct regcache regcache;
4547 unsigned char *dataptr;
4548
4549 dataptr = traceframe_find_regblock (tframe, -1);
4550 if (dataptr == NULL)
4551 return 0;
4552
4553 init_register_cache (&regcache, dataptr);
4554 return regcache_read_pc (&regcache);
4555}
4556
4557/* Read a requested block of memory from a trace frame. */
4558
4559int
4560traceframe_read_mem (int tfnum, CORE_ADDR addr,
4561 unsigned char *buf, ULONGEST length,
4562 ULONGEST *nbytes)
4563{
4564 struct traceframe *tframe;
4565 unsigned char *database, *dataptr;
4566 unsigned int datasize;
4567 CORE_ADDR maddr;
4568 unsigned short mlen;
4569
4570 trace_debug ("traceframe_read_mem");
4571
4572 tframe = find_traceframe (tfnum);
4573
4574 if (!tframe)
4575 {
4576 trace_debug ("traceframe %d not found", tfnum);
4577 return 1;
4578 }
4579
4580 datasize = tframe->data_size;
4581 database = dataptr = &tframe->data[0];
4582
4583 /* Iterate through a traceframe's blocks, looking for memory. */
4584 while ((dataptr = traceframe_find_block_type (dataptr,
4585 datasize - (dataptr - database),
4586 tfnum, 'M')) != NULL)
4587 {
4588 memcpy (&maddr, dataptr, sizeof (maddr));
4589 dataptr += sizeof (maddr);
4590 memcpy (&mlen, dataptr, sizeof (mlen));
4591 dataptr += sizeof (mlen);
4592 trace_debug ("traceframe %d has %d bytes at %s",
4593 tfnum, mlen, paddress (maddr));
4594
4595 /* Check that requested data is in bounds. */
4596 if (maddr <= addr && (addr + length) <= (maddr + mlen))
4597 {
4598 /* Block includes the requested range, copy it out. */
4599 memcpy (buf, dataptr + (addr - maddr), length);
4600 *nbytes = length;
4601 return 0;
4602 }
4603
4604 /* Skip over this block. */
4605 dataptr += mlen;
4606 }
4607
4608 trace_debug ("traceframe %d has no memory data for the desired region",
4609 tfnum);
4610
4611 *nbytes = 0;
4612 return 0;
4613}
4614
4615static int
4616traceframe_read_tsv (int tsvnum, LONGEST *val)
4617{
4618 int tfnum;
4619 struct traceframe *tframe;
4620 unsigned char *database, *dataptr;
4621 unsigned int datasize;
4622 int vnum;
4623
4624 trace_debug ("traceframe_read_tsv");
4625
4626 tfnum = current_traceframe;
4627
4628 if (tfnum < 0)
4629 {
4630 trace_debug ("no current traceframe");
4631 return 1;
4632 }
4633
4634 tframe = find_traceframe (tfnum);
4635
4636 if (tframe == NULL)
4637 {
4638 trace_debug ("traceframe %d not found", tfnum);
4639 return 1;
4640 }
4641
4642 datasize = tframe->data_size;
4643 database = dataptr = &tframe->data[0];
4644
4645 /* Iterate through a traceframe's blocks, looking for the tsv. */
4646 while ((dataptr = traceframe_find_block_type (dataptr,
4647 datasize - (dataptr - database),
4648 tfnum, 'V')) != NULL)
4649 {
4650 memcpy (&vnum, dataptr, sizeof (vnum));
4651 dataptr += sizeof (vnum);
4652
4653 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
4654
4655 /* Check that this is the variable we want. */
4656 if (tsvnum == vnum)
4657 {
4658 memcpy (val, dataptr, sizeof (*val));
4659 return 0;
4660 }
4661
4662 /* Skip over this block. */
4663 dataptr += sizeof (LONGEST);
4664 }
4665
4666 trace_debug ("traceframe %d has no data for variable %d",
4667 tfnum, tsvnum);
4668 return 1;
4669}
4670
fa593d66
PA
4671/* Return the first fast tracepoint whose jump pad contains PC. */
4672
4673static struct tracepoint *
4674fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
4675{
4676 struct tracepoint *tpoint;
4677
4678 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4679 if (tpoint->type == fast_tracepoint)
4680 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
4681 return tpoint;
4682
4683 return NULL;
4684}
4685
4686/* Return GDBserver's tracepoint that matches the IP Agent's
4687 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
4688 address space. */
4689
4690static struct tracepoint *
4691fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
4692{
4693 struct tracepoint *tpoint;
4694
4695 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4696 if (tpoint->type == fast_tracepoint)
4697 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
4698 return tpoint;
4699
4700 return NULL;
4701}
4702
4703#endif
4704
4705/* The type of the object that is used to synchronize fast tracepoint
4706 collection. */
4707
4708typedef struct collecting_t
4709{
4710 /* The fast tracepoint number currently collecting. */
4711 uintptr_t tpoint;
4712
4713 /* A number that GDBserver can use to identify the thread that is
4714 presently holding the collect lock. This need not (and usually
4715 is not) the thread id, as getting the current thread ID usually
4716 requires a system call, which we want to avoid like the plague.
4717 Usually this is thread's TCB, found in the TLS (pseudo-)
4718 register, which is readable with a single insn on several
4719 architectures. */
4720 uintptr_t thread_area;
4721} collecting_t;
4722
4723#ifndef IN_PROCESS_AGENT
4724
4725void
4726force_unlock_trace_buffer (void)
4727{
4728 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
4729}
4730
4731/* Check if the thread identified by THREAD_AREA which is stopped at
4732 STOP_PC, is presently locking the fast tracepoint collection, and
4733 if so, gather some status of said collection. Returns 0 if the
4734 thread isn't collecting or in the jump pad at all. 1, if in the
4735 jump pad (or within gdb_collect) and hasn't executed the adjusted
4736 original insn yet (can set a breakpoint there and run to it). 2,
4737 if presently executing the adjusted original insn --- in which
4738 case, if we want to move the thread out of the jump pad, we need to
4739 single-step it until this function returns 0. */
4740
4741int
4742fast_tracepoint_collecting (CORE_ADDR thread_area,
4743 CORE_ADDR stop_pc,
4744 struct fast_tpoint_collect_status *status)
4745{
4746 CORE_ADDR ipa_collecting;
4747 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
4748 struct tracepoint *tpoint;
4749 int needs_breakpoint;
4750
4751 /* The thread THREAD_AREA is either:
4752
4753 0. not collecting at all, not within the jump pad, or within
4754 gdb_collect or one of its callees.
4755
4756 1. in the jump pad and haven't reached gdb_collect
4757
4758 2. within gdb_collect (out of the jump pad) (collect is set)
4759
4760 3. we're in the jump pad, after gdb_collect having returned,
4761 possibly executing the adjusted insns.
4762
4763 For cases 1 and 3, `collecting' may or not be set. The jump pad
4764 doesn't have any complicated jump logic, so we can tell if the
4765 thread is executing the adjust original insn or not by just
4766 matching STOP_PC with known jump pad addresses. If we it isn't
4767 yet executing the original insn, set a breakpoint there, and let
4768 the thread run to it, so to quickly step over a possible (many
4769 insns) gdb_collect call. Otherwise, or when the breakpoint is
4770 hit, only a few (small number of) insns are left to be executed
4771 in the jump pad. Single-step the thread until it leaves the
4772 jump pad. */
4773
4774 again:
4775 tpoint = NULL;
4776 needs_breakpoint = 0;
4777 trace_debug ("fast_tracepoint_collecting");
4778
4779 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
4780 &ipa_gdb_jump_pad_buffer))
4781 fatal ("error extracting `gdb_jump_pad_buffer'");
4782 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
4783 &ipa_gdb_jump_pad_buffer_end))
4784 fatal ("error extracting `gdb_jump_pad_buffer_end'");
4785
4786 if (ipa_gdb_jump_pad_buffer <= stop_pc && stop_pc < ipa_gdb_jump_pad_buffer_end)
4787 {
4788 /* We can tell which tracepoint(s) the thread is collecting by
4789 matching the jump pad address back to the tracepoint. */
4790 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
4791 if (tpoint == NULL)
4792 {
4793 warning ("in jump pad, but no matching tpoint?");
4794 return 0;
4795 }
4796 else
4797 {
4798 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
4799 "adj_insn(%s, %s)",
4800 tpoint->number, paddress (tpoint->address),
4801 paddress (tpoint->jump_pad),
4802 paddress (tpoint->jump_pad_end),
4803 paddress (tpoint->adjusted_insn_addr),
4804 paddress (tpoint->adjusted_insn_addr_end));
4805 }
4806
4807 /* Definitely in the jump pad. May or may not need
4808 fast-exit-jump-pad breakpoint. */
4809 if (tpoint->jump_pad <= stop_pc
4810 && stop_pc < tpoint->adjusted_insn_addr)
4811 needs_breakpoint = 1;
4812 }
4813 else
4814 {
4815 collecting_t ipa_collecting_obj;
4816
4817 /* If `collecting' is set/locked, then the THREAD_AREA thread
4818 may or not be the one holding the lock. We have to read the
4819 lock to find out. */
4820
4821 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
4822 &ipa_collecting))
4823 {
4824 trace_debug ("fast_tracepoint_collecting:"
4825 " failed reading 'collecting' in the inferior");
4826 return 0;
4827 }
4828
4829 if (!ipa_collecting)
4830 {
4831 trace_debug ("fast_tracepoint_collecting: not collecting"
4832 " (and nobody is).");
4833 return 0;
4834 }
4835
4836 /* Some thread is collecting. Check which. */
4837 if (read_inferior_memory (ipa_collecting,
4838 (unsigned char *) &ipa_collecting_obj,
4839 sizeof (ipa_collecting_obj)) != 0)
4840 goto again;
4841
4842 if (ipa_collecting_obj.thread_area != thread_area)
4843 {
4844 trace_debug ("fast_tracepoint_collecting: not collecting "
4845 "(another thread is)");
4846 return 0;
4847 }
4848
4849 tpoint
4850 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
4851 if (tpoint == NULL)
4852 {
4853 warning ("fast_tracepoint_collecting: collecting, "
4854 "but tpoint %s not found?",
4855 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
4856 return 0;
4857 }
4858
4859 /* The thread is within `gdb_collect', skip over the rest of
4860 fast tracepoint collection quickly using a breakpoint. */
4861 needs_breakpoint = 1;
4862 }
4863
4864 /* The caller wants a bit of status detail. */
4865 if (status != NULL)
4866 {
4867 status->tpoint_num = tpoint->number;
4868 status->tpoint_addr = tpoint->address;
4869 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
4870 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
4871 }
4872
4873 if (needs_breakpoint)
4874 {
4875 /* Hasn't executed the original instruction yet. Set breakpoint
4876 there, and wait till it's hit, then single-step until exiting
4877 the jump pad. */
4878
4879 trace_debug ("\
4880fast_tracepoint_collecting, returning continue-until-break at %s",
4881 paddress (tpoint->adjusted_insn_addr));
4882
4883 return 1; /* continue */
4884 }
4885 else
4886 {
4887 /* Just single-step until exiting the jump pad. */
4888
4889 trace_debug ("fast_tracepoint_collecting, returning "
4890 "need-single-step (%s-%s)",
4891 paddress (tpoint->adjusted_insn_addr),
4892 paddress (tpoint->adjusted_insn_addr_end));
4893
4894 return 2; /* single-step */
4895 }
4896}
4897
4898#endif
4899
4900#ifdef IN_PROCESS_AGENT
4901
4902/* The global fast tracepoint collect lock. Points to a collecting_t
4903 object built on the stack by the jump pad, if presently locked;
4904 NULL if it isn't locked. Note that this lock *must* be set while
4905 executing any *function other than the jump pad. See
4906 fast_tracepoint_collecting. */
4907static collecting_t * ATTR_USED collecting;
4908
4909/* This routine, called from the jump pad (in asm) is designed to be
4910 called from the jump pads of fast tracepoints, thus it is on the
4911 critical path. */
4912
4913IP_AGENT_EXPORT void ATTR_USED
4914gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
4915{
4916 struct fast_tracepoint_ctx ctx;
4917
4918 /* Don't do anything until the trace run is completely set up. */
4919 if (!tracing)
4920 return;
4921
4922 ctx.base.type = fast_tracepoint;
4923 ctx.regs = regs;
4924 ctx.regcache_initted = 0;
4925 ctx.tpoint = tpoint;
4926
4927 /* Wrap the regblock in a register cache (in the stack, we don't
4928 want to malloc here). */
4929 ctx.regspace = alloca (register_cache_size ());
4930 if (ctx.regspace == NULL)
4931 {
4932 trace_debug ("Trace buffer block allocation failed, skipping");
4933 return;
4934 }
4935
4936 /* Test the condition if present, and collect if true. */
4937 if (tpoint->cond == NULL
4938 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4939 tpoint))
4940 {
4941 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4942 tpoint->address, tpoint);
4943
4944 /* Note that this will cause original insns to be written back
4945 to where we jumped from, but that's OK because we're jumping
4946 back to the next whole instruction. This will go badly if
4947 instruction restoration is not atomic though. */
4948 if (stopping_tracepoint
4949 || trace_buffer_is_full
4950 || expr_eval_result != expr_eval_no_error)
4951 stop_tracing ();
4952 }
4953 else
4954 {
4955 /* If there was a condition and it evaluated to false, the only
4956 way we would stop tracing is if there was an error during
4957 condition expression evaluation. */
4958 if (expr_eval_result != expr_eval_no_error)
4959 stop_tracing ();
4960 }
4961}
4962
4963#endif
4964
4965#ifndef IN_PROCESS_AGENT
4966
6a271cae
PA
4967/* Bytecode compilation. */
4968
4969CORE_ADDR current_insn_ptr;
4970
4971int emit_error;
4972
4973struct bytecode_address
4974{
4975 int pc;
4976 CORE_ADDR address;
4977 int goto_pc;
4978 /* Offset and size of field to be modified in the goto block. */
4979 int from_offset, from_size;
4980 struct bytecode_address *next;
4981} *bytecode_address_table;
4982
4983CORE_ADDR
4984get_raw_reg_func_addr (void)
4985{
4986 return ipa_sym_addrs.addr_get_raw_reg;
4987}
4988
4989static void
4990emit_prologue (void)
4991{
4992 target_emit_ops ()->emit_prologue ();
4993}
4994
4995static void
4996emit_epilogue (void)
4997{
4998 target_emit_ops ()->emit_epilogue ();
4999}
5000
5001static void
5002emit_add (void)
5003{
5004 target_emit_ops ()->emit_add ();
5005}
5006
5007static void
5008emit_sub (void)
5009{
5010 target_emit_ops ()->emit_sub ();
5011}
5012
5013static void
5014emit_mul (void)
5015{
5016 target_emit_ops ()->emit_mul ();
5017}
5018
5019static void
5020emit_lsh (void)
5021{
5022 target_emit_ops ()->emit_lsh ();
5023}
5024
5025static void
5026emit_rsh_signed (void)
5027{
5028 target_emit_ops ()->emit_rsh_signed ();
5029}
5030
5031static void
5032emit_rsh_unsigned (void)
5033{
5034 target_emit_ops ()->emit_rsh_unsigned ();
5035}
5036
5037static void
5038emit_ext (int arg)
5039{
5040 target_emit_ops ()->emit_ext (arg);
5041}
5042
5043static void
5044emit_log_not (void)
5045{
5046 target_emit_ops ()->emit_log_not ();
5047}
5048
5049static void
5050emit_bit_and (void)
5051{
5052 target_emit_ops ()->emit_bit_and ();
5053}
5054
5055static void
5056emit_bit_or (void)
5057{
5058 target_emit_ops ()->emit_bit_or ();
5059}
5060
5061static void
5062emit_bit_xor (void)
5063{
5064 target_emit_ops ()->emit_bit_xor ();
5065}
5066
5067static void
5068emit_bit_not (void)
5069{
5070 target_emit_ops ()->emit_bit_not ();
5071}
5072
5073static void
5074emit_equal (void)
5075{
5076 target_emit_ops ()->emit_equal ();
5077}
5078
5079static void
5080emit_less_signed (void)
5081{
5082 target_emit_ops ()->emit_less_signed ();
5083}
5084
5085static void
5086emit_less_unsigned (void)
5087{
5088 target_emit_ops ()->emit_less_unsigned ();
5089}
5090
5091static void
5092emit_ref (int size)
5093{
5094 target_emit_ops ()->emit_ref (size);
5095}
5096
5097static void
5098emit_if_goto (int *offset_p, int *size_p)
5099{
5100 target_emit_ops ()->emit_if_goto (offset_p, size_p);
5101}
5102
5103static void
5104emit_goto (int *offset_p, int *size_p)
5105{
5106 target_emit_ops ()->emit_goto (offset_p, size_p);
5107}
5108
5109static void
5110write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
5111{
5112 target_emit_ops ()->write_goto_address (from, to, size);
5113}
5114
5115static void
4e29fb54 5116emit_const (LONGEST num)
6a271cae
PA
5117{
5118 target_emit_ops ()->emit_const (num);
5119}
5120
5121static void
5122emit_reg (int reg)
5123{
5124 target_emit_ops ()->emit_reg (reg);
5125}
5126
5127static void
5128emit_pop (void)
5129{
5130 target_emit_ops ()->emit_pop ();
5131}
5132
5133static void
5134emit_stack_flush (void)
5135{
5136 target_emit_ops ()->emit_stack_flush ();
5137}
5138
5139static void
5140emit_zero_ext (int arg)
5141{
5142 target_emit_ops ()->emit_zero_ext (arg);
5143}
5144
5145static void
5146emit_swap (void)
5147{
5148 target_emit_ops ()->emit_swap ();
5149}
5150
5151static void
5152emit_stack_adjust (int n)
5153{
5154 target_emit_ops ()->emit_stack_adjust (n);
5155}
5156
5157/* FN's prototype is `LONGEST(*fn)(int)'. */
5158
5159static void
5160emit_int_call_1 (CORE_ADDR fn, int arg1)
5161{
5162 target_emit_ops ()->emit_int_call_1 (fn, arg1);
5163}
5164
4e29fb54 5165/* FN's prototype is `void(*fn)(int,LONGEST)'. */
6a271cae
PA
5166
5167static void
5168emit_void_call_2 (CORE_ADDR fn, int arg1)
5169{
5170 target_emit_ops ()->emit_void_call_2 (fn, arg1);
5171}
5172
5173static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
5174
5175static void
5176compile_tracepoint_condition (struct tracepoint *tpoint, CORE_ADDR *jump_entry)
5177{
5178 CORE_ADDR entry_point = *jump_entry;
5179 enum eval_result_type err;
5180
5181 trace_debug ("Starting condition compilation for tracepoint %d\n",
5182 tpoint->number);
5183
5184 /* Initialize the global pointer to the code being built. */
5185 current_insn_ptr = *jump_entry;
5186
5187 emit_prologue ();
5188
5189 err = compile_bytecodes (tpoint->cond);
5190
5191 if (err == expr_eval_no_error)
5192 {
5193 emit_epilogue ();
5194
5195 /* Record the beginning of the compiled code. */
5196 tpoint->compiled_cond = entry_point;
5197
5198 trace_debug ("Condition compilation for tracepoint %d complete\n",
5199 tpoint->number);
5200 }
5201 else
5202 {
5203 /* Leave the unfinished code in situ, but don't point to it. */
5204
5205 tpoint->compiled_cond = 0;
5206
5207 trace_debug ("Condition compilation for tracepoint %d failed, "
5208 "error code %d",
5209 tpoint->number, err);
5210 }
5211
5212 /* Update the code pointer passed in. Note that we do this even if
5213 the compile fails, so that we can look at the partial results
5214 instead of letting them be overwritten. */
5215 *jump_entry = current_insn_ptr;
5216
5217 /* Leave a gap, to aid dump decipherment. */
5218 *jump_entry += 16;
5219}
5220
5221/* Given an agent expression, turn it into native code. */
5222
5223static enum eval_result_type
5224compile_bytecodes (struct agent_expr *aexpr)
5225{
5226 int pc = 0;
5227 int done = 0;
5228 unsigned char op;
5229 int arg;
5230 /* This is only used to build 64-bit value for constants. */
5231 ULONGEST top;
5232 struct bytecode_address *aentry, *aentry2;
5233
5234#define UNHANDLED \
5235 do \
5236 { \
5237 trace_debug ("Cannot compile op 0x%x\n", op); \
5238 return expr_eval_unhandled_opcode; \
5239 } while (0)
5240
5241 if (aexpr->length == 0)
5242 {
5243 trace_debug ("empty agent expression\n");
5244 return expr_eval_empty_expression;
5245 }
5246
5247 bytecode_address_table = NULL;
5248
5249 while (!done)
5250 {
5251 op = aexpr->bytes[pc];
5252
5253 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
5254
5255 /* Record the compiled-code address of the bytecode, for use by
5256 jump instructions. */
5257 aentry = xmalloc (sizeof (struct bytecode_address));
5258 aentry->pc = pc;
5259 aentry->address = current_insn_ptr;
5260 aentry->goto_pc = -1;
5261 aentry->from_offset = aentry->from_size = 0;
5262 aentry->next = bytecode_address_table;
5263 bytecode_address_table = aentry;
5264
5265 ++pc;
5266
5267 emit_error = 0;
5268
5269 switch (op)
5270 {
5271 case gdb_agent_op_add:
5272 emit_add ();
5273 break;
5274
5275 case gdb_agent_op_sub:
5276 emit_sub ();
5277 break;
5278
5279 case gdb_agent_op_mul:
5280 emit_mul ();
5281 break;
5282
5283 case gdb_agent_op_div_signed:
5284 UNHANDLED;
5285 break;
5286
5287 case gdb_agent_op_div_unsigned:
5288 UNHANDLED;
5289 break;
5290
5291 case gdb_agent_op_rem_signed:
5292 UNHANDLED;
5293 break;
5294
5295 case gdb_agent_op_rem_unsigned:
5296 UNHANDLED;
5297 break;
5298
5299 case gdb_agent_op_lsh:
5300 emit_lsh ();
5301 break;
5302
5303 case gdb_agent_op_rsh_signed:
5304 emit_rsh_signed ();
5305 break;
5306
5307 case gdb_agent_op_rsh_unsigned:
5308 emit_rsh_unsigned ();
5309 break;
5310
5311 case gdb_agent_op_trace:
5312 UNHANDLED;
5313 break;
5314
5315 case gdb_agent_op_trace_quick:
5316 UNHANDLED;
5317 break;
5318
5319 case gdb_agent_op_log_not:
5320 emit_log_not ();
5321 break;
5322
5323 case gdb_agent_op_bit_and:
5324 emit_bit_and ();
5325 break;
5326
5327 case gdb_agent_op_bit_or:
5328 emit_bit_or ();
5329 break;
5330
5331 case gdb_agent_op_bit_xor:
5332 emit_bit_xor ();
5333 break;
5334
5335 case gdb_agent_op_bit_not:
5336 emit_bit_not ();
5337 break;
5338
5339 case gdb_agent_op_equal:
5340 emit_equal ();
5341 break;
5342
5343 case gdb_agent_op_less_signed:
5344 emit_less_signed ();
5345 break;
5346
5347 case gdb_agent_op_less_unsigned:
5348 emit_less_unsigned ();
5349 break;
5350
5351 case gdb_agent_op_ext:
5352 arg = aexpr->bytes[pc++];
5353 if (arg < (sizeof (LONGEST) * 8))
5354 emit_ext (arg);
5355 break;
5356
5357 case gdb_agent_op_ref8:
5358 emit_ref (1);
5359 break;
5360
5361 case gdb_agent_op_ref16:
5362 emit_ref (2);
5363 break;
5364
5365 case gdb_agent_op_ref32:
5366 emit_ref (4);
5367 break;
5368
5369 case gdb_agent_op_ref64:
5370 emit_ref (8);
5371 break;
5372
5373 case gdb_agent_op_if_goto:
5374 arg = aexpr->bytes[pc++];
5375 arg = (arg << 8) + aexpr->bytes[pc++];
5376 aentry->goto_pc = arg;
5377 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
5378 break;
5379
5380 case gdb_agent_op_goto:
5381 arg = aexpr->bytes[pc++];
5382 arg = (arg << 8) + aexpr->bytes[pc++];
5383 aentry->goto_pc = arg;
5384 emit_goto (&(aentry->from_offset), &(aentry->from_size));
5385 break;
5386
5387 case gdb_agent_op_const8:
5388 emit_stack_flush ();
5389 top = aexpr->bytes[pc++];
5390 emit_const (top);
5391 break;
5392
5393 case gdb_agent_op_const16:
5394 emit_stack_flush ();
5395 top = aexpr->bytes[pc++];
5396 top = (top << 8) + aexpr->bytes[pc++];
5397 emit_const (top);
5398 break;
5399
5400 case gdb_agent_op_const32:
5401 emit_stack_flush ();
5402 top = aexpr->bytes[pc++];
5403 top = (top << 8) + aexpr->bytes[pc++];
5404 top = (top << 8) + aexpr->bytes[pc++];
5405 top = (top << 8) + aexpr->bytes[pc++];
5406 emit_const (top);
5407 break;
5408
5409 case gdb_agent_op_const64:
5410 emit_stack_flush ();
5411 top = aexpr->bytes[pc++];
5412 top = (top << 8) + aexpr->bytes[pc++];
5413 top = (top << 8) + aexpr->bytes[pc++];
5414 top = (top << 8) + aexpr->bytes[pc++];
5415 top = (top << 8) + aexpr->bytes[pc++];
5416 top = (top << 8) + aexpr->bytes[pc++];
5417 top = (top << 8) + aexpr->bytes[pc++];
5418 top = (top << 8) + aexpr->bytes[pc++];
5419 emit_const (top);
5420 break;
5421
5422 case gdb_agent_op_reg:
5423 emit_stack_flush ();
5424 arg = aexpr->bytes[pc++];
5425 arg = (arg << 8) + aexpr->bytes[pc++];
5426 emit_reg (arg);
5427 break;
5428
5429 case gdb_agent_op_end:
5430 trace_debug ("At end of expression\n");
5431
5432 /* Assume there is one stack element left, and that it is
5433 cached in "top" where emit_epilogue can get to it. */
5434 emit_stack_adjust (1);
5435
5436 done = 1;
5437 break;
5438
5439 case gdb_agent_op_dup:
5440 /* In our design, dup is equivalent to stack flushing. */
5441 emit_stack_flush ();
5442 break;
5443
5444 case gdb_agent_op_pop:
5445 emit_pop ();
5446 break;
5447
5448 case gdb_agent_op_zero_ext:
5449 arg = aexpr->bytes[pc++];
5450 if (arg < (sizeof (LONGEST) * 8))
5451 emit_zero_ext (arg);
5452 break;
5453
5454 case gdb_agent_op_swap:
5455 emit_swap ();
5456 break;
5457
5458 case gdb_agent_op_getv:
5459 emit_stack_flush ();
5460 arg = aexpr->bytes[pc++];
5461 arg = (arg << 8) + aexpr->bytes[pc++];
5462 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
5463 arg);
5464 break;
5465
5466 case gdb_agent_op_setv:
5467 arg = aexpr->bytes[pc++];
5468 arg = (arg << 8) + aexpr->bytes[pc++];
5469 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
5470 arg);
5471 break;
5472
5473 case gdb_agent_op_tracev:
5474 UNHANDLED;
5475 break;
5476
5477 /* GDB never (currently) generates any of these ops. */
5478 case gdb_agent_op_float:
5479 case gdb_agent_op_ref_float:
5480 case gdb_agent_op_ref_double:
5481 case gdb_agent_op_ref_long_double:
5482 case gdb_agent_op_l_to_d:
5483 case gdb_agent_op_d_to_l:
5484 case gdb_agent_op_trace16:
5485 UNHANDLED;
5486 break;
5487
5488 default:
5489 trace_debug ("Agent expression op 0x%x not recognized\n", op);
5490 /* Don't struggle on, things will just get worse. */
5491 return expr_eval_unrecognized_opcode;
5492 }
5493
5494 /* This catches errors that occur in target-specific code
5495 emission. */
5496 if (emit_error)
5497 {
5498 trace_debug ("Error %d while emitting code for %s\n",
5499 emit_error, gdb_agent_op_names[op]);
5500 return expr_eval_unhandled_opcode;
5501 }
5502
5503 trace_debug ("Op %s compiled\n", gdb_agent_op_names[op]);
5504 }
5505
5506 /* Now fill in real addresses as goto destinations. */
5507 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
5508 {
5509 int written = 0;
5510
5511 if (aentry->goto_pc < 0)
5512 continue;
5513
5514 /* Find the location that we are going to, and call back into
5515 target-specific code to write the actual address or
5516 displacement. */
5517 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
5518 {
5519 if (aentry2->pc == aentry->goto_pc)
5520 {
5521 trace_debug ("Want to jump from %s to %s\n",
5522 paddress (aentry->address),
5523 paddress (aentry2->address));
5524 write_goto_address (aentry->address + aentry->from_offset,
5525 aentry2->address, aentry->from_size);
5526 written = 1;
5527 break;
5528 }
5529 }
5530
5531 /* Error out if we didn't find a destination. */
5532 if (!written)
5533 {
5534 trace_debug ("Destination of goto %d not found\n",
5535 aentry->goto_pc);
5536 return expr_eval_invalid_goto;
5537 }
5538 }
5539
5540 return expr_eval_no_error;
5541}
5542
fa593d66
PA
5543/* We'll need to adjust these when we consider bi-arch setups, and big
5544 endian machines. */
5545
5546static int
5547write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5548{
5549 return write_inferior_memory (where,
5550 (unsigned char *) &ptr, sizeof (void *));
5551}
5552
5553/* The base pointer of the IPA's heap. This is the only memory the
5554 IPA is allowed to use. The IPA should _not_ call the inferior's
5555 `malloc' during operation. That'd be slow, and, most importantly,
5556 it may not be safe. We may be collecting a tracepoint in a signal
5557 handler, for example. */
5558static CORE_ADDR target_tp_heap;
5559
5560/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5561 to 8 bytes. */
5562
5563static CORE_ADDR
5564target_malloc (ULONGEST size)
5565{
5566 CORE_ADDR ptr;
5567
5568 if (target_tp_heap == 0)
5569 {
5570 /* We have the pointer *address*, need what it points to. */
5571 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5572 &target_tp_heap))
5573 fatal ("could get target heap head pointer");
5574 }
5575
5576 ptr = target_tp_heap;
5577 target_tp_heap += size;
5578
5579 /* Pad to 8-byte alignment. */
5580 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5581
5582 return ptr;
5583}
5584
5585static CORE_ADDR
5586download_agent_expr (struct agent_expr *expr)
5587{
5588 CORE_ADDR expr_addr;
5589 CORE_ADDR expr_bytes;
5590
5591 expr_addr = target_malloc (sizeof (*expr));
5592 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5593
5594 expr_bytes = target_malloc (expr->length);
5595 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5596 expr_bytes);
5597 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5598
5599 return expr_addr;
5600}
5601
5602/* Align V up to N bits. */
5603#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5604
5605static void
5606download_tracepoints (void)
5607{
5608 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5609 struct tracepoint *tpoint;
5610
5611 /* Start out empty. */
5612 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
5613
5614 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5615 {
5616 struct tracepoint target_tracepoint;
5617
5618 if (tpoint->type != fast_tracepoint)
5619 continue;
5620
6a271cae
PA
5621 /* Maybe download a compiled condition. */
5622 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5623 {
5624 CORE_ADDR jentry, jump_entry;
5625
5626 jentry = jump_entry = get_jump_space_head ();
5627
5628 if (tpoint->cond != NULL)
5629 {
5630 /* Pad to 8-byte alignment. (needed?) */
5631 /* Actually this should be left for the target to
5632 decide. */
5633 jentry = UALIGN (jentry, 8);
5634
5635 compile_tracepoint_condition (tpoint, &jentry);
5636 }
5637
5638 /* Pad to 8-byte alignment. */
5639 jentry = UALIGN (jentry, 8);
5640 claim_jump_space (jentry - jump_entry);
5641 }
5642
fa593d66
PA
5643 target_tracepoint = *tpoint;
5644
5645 prev_tpptr = tpptr;
5646 tpptr = target_malloc (sizeof (*tpoint));
5647 tpoint->obj_addr_on_target = tpptr;
5648
5649 if (tpoint == tracepoints)
5650 {
5651 /* First object in list, set the head pointer in the
5652 inferior. */
5653 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
5654 }
5655 else
5656 {
5657 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
5658 next),
5659 tpptr);
5660 }
5661
5662 /* Write the whole object. We'll fix up its pointers in a bit.
5663 Assume no next for now. This is fixed up above on the next
5664 iteration, if there's any. */
5665 target_tracepoint.next = NULL;
5666 /* Need to clear this here too, since we're downloading the
5667 tracepoints before clearing our own copy. */
5668 target_tracepoint.hit_count = 0;
5669
5670 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5671 sizeof (target_tracepoint));
5672
5673 if (tpoint->cond)
5674 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5675 cond),
5676 download_agent_expr (tpoint->cond));
5677
5678 if (tpoint->numactions)
5679 {
5680 int i;
5681 CORE_ADDR actions_array;
5682
5683 /* The pointers array. */
5684 actions_array
5685 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5686 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5687 actions),
5688 actions_array);
5689
5690 /* Now for each pointer, download the action. */
5691 for (i = 0; i < tpoint->numactions; i++)
5692 {
5693 CORE_ADDR ipa_action = 0;
5694 struct tracepoint_action *action = tpoint->actions[i];
5695
5696 switch (action->type)
5697 {
5698 case 'M':
5699 ipa_action
5700 = target_malloc (sizeof (struct collect_memory_action));
5701 write_inferior_memory (ipa_action,
5702 (unsigned char *) action,
5703 sizeof (struct collect_memory_action));
5704 break;
5705 case 'R':
5706 ipa_action
5707 = target_malloc (sizeof (struct collect_registers_action));
5708 write_inferior_memory (ipa_action,
5709 (unsigned char *) action,
5710 sizeof (struct collect_registers_action));
5711 break;
5712 case 'X':
5713 {
5714 CORE_ADDR expr;
5715 struct eval_expr_action *eaction
5716 = (struct eval_expr_action *) action;
5717
5718 ipa_action = target_malloc (sizeof (*eaction));
5719 write_inferior_memory (ipa_action,
5720 (unsigned char *) eaction,
5721 sizeof (*eaction));
5722
5723 expr = download_agent_expr (eaction->expr);
5724 write_inferior_data_ptr
5725 (ipa_action + offsetof (struct eval_expr_action, expr),
5726 expr);
5727 break;
5728 }
5729 default:
5730 trace_debug ("unknown trace action '%c', ignoring",
5731 action->type);
5732 break;
5733 }
5734
5735 if (ipa_action != 0)
5736 write_inferior_data_ptr
5737 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5738 ipa_action);
5739 }
5740 }
5741 }
5742}
5743
5744static void
5745download_trace_state_variables (void)
5746{
5747 CORE_ADDR ptr = 0, prev_ptr = 0;
5748 struct trace_state_variable *tsv;
5749
5750 /* Start out empty. */
5751 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5752
5753 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5754 {
5755 struct trace_state_variable target_tsv;
5756
5757 /* TSV's with a getter have been initialized equally in both the
5758 inferior and GDBserver. Skip them. */
5759 if (tsv->getter != NULL)
5760 continue;
5761
5762 target_tsv = *tsv;
5763
5764 prev_ptr = ptr;
5765 ptr = target_malloc (sizeof (*tsv));
5766
5767 if (tsv == trace_state_variables)
5768 {
5769 /* First object in list, set the head pointer in the
5770 inferior. */
5771
5772 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5773 ptr);
5774 }
5775 else
5776 {
5777 write_inferior_data_ptr (prev_ptr
5778 + offsetof (struct trace_state_variable,
5779 next),
5780 ptr);
5781 }
5782
5783 /* Write the whole object. We'll fix up its pointers in a bit.
5784 Assume no next, fixup when needed. */
5785 target_tsv.next = NULL;
5786
5787 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5788 sizeof (target_tsv));
5789
5790 if (tsv->name != NULL)
5791 {
5792 size_t size = strlen (tsv->name) + 1;
5793 CORE_ADDR name_addr = target_malloc (size);
5794 write_inferior_memory (name_addr,
5795 (unsigned char *) tsv->name, size);
5796 write_inferior_data_ptr (ptr
5797 + offsetof (struct trace_state_variable,
5798 name),
5799 name_addr);
5800 }
5801
5802 if (tsv->getter != NULL)
5803 {
5804 fatal ("what to do with these?");
5805 }
5806 }
5807
5808 if (prev_ptr != 0)
5809 {
5810 /* Fixup the next pointer in the last item in the list. */
5811 write_inferior_data_ptr (prev_ptr + offsetof (struct trace_state_variable,
5812 next), 0);
5813 }
5814}
5815
5816/* Upload complete trace frames out of the IP Agent's trace buffer
5817 into GDBserver's trace buffer. This always uploads either all or
5818 no trace frames. This is the counter part of
5819 `trace_alloc_trace_buffer'. See its description of the atomic
5820 synching mechanism. */
5821
5822static void
5823upload_fast_traceframes (void)
5824{
5825 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5826 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5827 CORE_ADDR tf;
5828 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5829 unsigned int curr_tbctrl_idx;
5830 unsigned int ipa_trace_buffer_ctrl_curr;
5831 unsigned int ipa_trace_buffer_ctrl_curr_old;
5832 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5833 struct breakpoint *about_to_request_buffer_space_bkpt;
5834 CORE_ADDR ipa_trace_buffer_lo;
5835 CORE_ADDR ipa_trace_buffer_hi;
5836
5837 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5838 &ipa_traceframe_read_count_racy))
5839 {
5840 /* This will happen in most targets if the current thread is
5841 running. */
5842 return;
5843 }
5844
5845 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5846 &ipa_traceframe_write_count_racy))
5847 return;
5848
5849 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5850 ipa_traceframe_write_count_racy - ipa_traceframe_read_count_racy,
5851 ipa_traceframe_write_count_racy, ipa_traceframe_read_count_racy);
5852
5853 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5854 return;
5855
5856 about_to_request_buffer_space_bkpt
5857 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5858 NULL);
5859
5860 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5861 &ipa_trace_buffer_ctrl_curr))
5862 return;
5863
5864 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
5865
5866 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
5867
5868 {
5869 unsigned int prev, counter;
5870
5871 /* Update the token, with new counters, and the GDBserver stamp
5872 bit. Alway reuse the current TBC index. */
5873 prev = ipa_trace_buffer_ctrl_curr & 0x0007ff00;
5874 counter = (prev + 0x100) & 0x0007ff00;
5875
5876 ipa_trace_buffer_ctrl_curr = (0x80000000
5877 | (prev << 12)
5878 | counter
5879 | curr_tbctrl_idx);
5880 }
5881
5882 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5883 ipa_trace_buffer_ctrl_curr))
5884 return;
5885
5886 trace_debug ("Lib: Committed %08x -> %08x",
5887 ipa_trace_buffer_ctrl_curr_old,
5888 ipa_trace_buffer_ctrl_curr);
5889
5890 /* Re-read these, now that we've installed the
5891 `about_to_request_buffer_space' breakpoint/lock. A thread could
5892 have finished a traceframe between the last read of these
5893 counters and setting the breakpoint above. If we start
5894 uploading, we never want to leave this function with
5895 traceframe_read_count != 0, otherwise, GDBserver could end up
5896 incrementing the counter tokens more than once (due to event loop
5897 nesting), which would break the IP agent's "effective" detection
5898 (see trace_alloc_trace_buffer). */
5899 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5900 &ipa_traceframe_read_count))
5901 return;
5902 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5903 &ipa_traceframe_write_count))
5904 return;
5905
5906 if (debug_threads)
5907 {
5908 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5909 ipa_traceframe_write_count - ipa_traceframe_read_count,
5910 ipa_traceframe_write_count, ipa_traceframe_read_count);
5911
5912 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
5913 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
5914 trace_debug ("note that ipa_traceframe_count's parts changed");
5915 }
5916
5917 /* Get the address of the current TBC object (the IP agent has an
5918 array of 3 such objects). The index is stored in the TBC
5919 token. */
5920 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
5921 ipa_trace_buffer_ctrl_addr
5922 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
5923
5924 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
5925 (unsigned char *) &ipa_trace_buffer_ctrl,
5926 sizeof (struct ipa_trace_buffer_control)))
5927 return;
5928
5929 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
5930 &ipa_trace_buffer_lo))
5931 return;
5932 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
5933 &ipa_trace_buffer_hi))
5934 return;
5935
5936 /* Offsets are easier to grok for debugging than raw addresses,
5937 especially for the small trace buffer sizes that are useful for
5938 testing. */
5939 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
5940 "endfree=%d wrap=%d hi=%d",
5941 curr_tbctrl_idx,
5942 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
5943 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
5944 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
5945 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
5946 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
5947
5948 /* Note that the IPA's buffer is always circular. */
5949
5950#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
5951
5952#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
5953 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
5954
5955#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
5956 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
5957 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
5958 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
5959 : 0))
5960
5961 tf = IPA_FIRST_TRACEFRAME ();
5962
5963 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
5964 {
5965 struct tracepoint *tpoint;
5966 struct traceframe *tframe;
5967 unsigned char *block;
5968 struct traceframe ipa_tframe;
5969
5970 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
5971 offsetof (struct traceframe, data)))
5972 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
5973
5974 if (ipa_tframe.tpnum == 0)
5975 fatal ("Uploading: No (more) fast traceframes, but "
5976 "ipa_traceframe_count == %u??\n",
5977 ipa_traceframe_write_count - ipa_traceframe_read_count);
5978
5979 /* Note that this will be incorrect for multi-location
5980 tracepoints... */
5981 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
5982
5983 tframe = add_traceframe (tpoint);
5984 if (tframe == NULL)
5985 {
5986 trace_buffer_is_full = 1;
5987 trace_debug ("Uploading: trace buffer is full");
5988 }
5989 else
5990 {
5991 /* Copy the whole set of blocks in one go for now. FIXME:
5992 split this in smaller blocks. */
5993 block = add_traceframe_block (tframe, ipa_tframe.data_size);
5994 if (block != NULL)
5995 {
5996 if (read_inferior_memory (tf + offsetof (struct traceframe, data),
5997 block, ipa_tframe.data_size))
5998 error ("Uploading: Couldn't read traceframe data at %s\n",
5999 paddress (tf + offsetof (struct traceframe, data)));
6000 }
6001
6002 trace_debug ("Uploading: traceframe didn't fit");
6003 finish_traceframe (tframe);
6004 }
6005
6006 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6007
6008 /* If we freed the traceframe that wrapped around, go back
6009 to the non-wrap case. */
6010 if (tf < ipa_trace_buffer_ctrl.start)
6011 {
6012 trace_debug ("Lib: Discarding past the wraparound");
6013 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6014 }
6015 ipa_trace_buffer_ctrl.start = tf;
6016 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6017 ++ipa_traceframe_read_count;
6018
6019 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6020 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6021 {
6022 trace_debug ("Lib: buffer is fully empty. "
6023 "Trace buffer [%d] start=%d free=%d endfree=%d",
6024 curr_tbctrl_idx,
6025 (int) (ipa_trace_buffer_ctrl.start
6026 - ipa_trace_buffer_lo),
6027 (int) (ipa_trace_buffer_ctrl.free
6028 - ipa_trace_buffer_lo),
6029 (int) (ipa_trace_buffer_ctrl.end_free
6030 - ipa_trace_buffer_lo));
6031
6032 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6033 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6034 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6035 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6036 }
6037
6038 trace_debug ("Uploaded a traceframe\n"
6039 "Lib: Trace buffer [%d] start=%d free=%d "
6040 "endfree=%d wrap=%d hi=%d",
6041 curr_tbctrl_idx,
6042 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6043 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6044 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6045 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6046 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6047 }
6048
6049 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6050 (unsigned char *) &ipa_trace_buffer_ctrl,
6051 sizeof (struct ipa_trace_buffer_control)))
6052 return;
6053
6054 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6055 ipa_traceframe_read_count);
6056
6057 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6058
6059 pause_all (1);
6060 cancel_breakpoints ();
6061
6062 delete_breakpoint (about_to_request_buffer_space_bkpt);
6063 about_to_request_buffer_space_bkpt = NULL;
6064
6065 unpause_all (1);
6066
6067 if (trace_buffer_is_full)
6068 stop_tracing ();
6069}
6070#endif
6071
6072#ifdef IN_PROCESS_AGENT
6073
6074#include <sys/mman.h>
6075#include <fcntl.h>
6076
6077IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6078IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6079IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
6080
6081static void __attribute__ ((constructor))
6082initialize_tracepoint_ftlib (void)
6083{
6084 initialize_tracepoint ();
6085}
6086
6087#endif
6088
219f2f23
PA
6089static LONGEST
6090tsv_get_timestamp (void)
6091{
6092 struct timeval tv;
6093
6094 if (gettimeofday (&tv, 0) != 0)
6095 return -1;
6096 else
6097 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6098}
6099
6100void
6101initialize_tracepoint (void)
6102{
6103 /* There currently no way to change the buffer size. */
6104 const int sizeOfBuffer = 5 * 1024 * 1024;
6105 unsigned char *buf = xmalloc (sizeOfBuffer);
6106 init_trace_buffer (buf, sizeOfBuffer);
6107
6108 /* Wire trace state variable 1 to be the timestamp. This will be
6109 uploaded to GDB upon connection and become one of its trace state
6110 variables. (In case you're wondering, if GDB already has a trace
6111 variable numbered 1, it will be renumbered.) */
fa593d66 6112 create_trace_state_variable (1, 0);
219f2f23
PA
6113 set_trace_state_variable_name (1, "trace_timestamp");
6114 set_trace_state_variable_getter (1, tsv_get_timestamp);
fa593d66
PA
6115
6116#ifdef IN_PROCESS_AGENT
6117 {
6118 int pagesize;
6119 pagesize = sysconf (_SC_PAGE_SIZE);
6120 if (pagesize == -1)
6121 fatal ("sysconf");
6122
6123 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
6124
6125 /* Allocate scratch buffer aligned on a page boundary. */
6126 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
6127 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
6128
6129 /* Make it writable and executable. */
6130 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
6131 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
6132 fatal ("\
6133initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
6134 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
6135 }
6136
6137 initialize_low_tracepoint ();
6138#endif
219f2f23 6139}
This page took 0.358736 seconds and 4 git commands to generate.