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