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