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