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