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