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