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