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