]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/tracepoint.c
df378ff4bb854d3442bfd3288d62062add968739
[thirdparty/binutils-gdb.git] / gdb / gdbserver / tracepoint.c
1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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"
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/time.h>
24
25 static void trace_debug_1 (const char *, ...) ATTR_FORMAT (printf, 1, 2);
26
27 static void
28 trace_debug_1 (const char *fmt, ...)
29 {
30 char buf[1024];
31 va_list ap;
32
33 va_start (ap, fmt);
34 vsprintf (buf, fmt, ap);
35 fprintf (stderr, "gdbserver/tracepoint: %s\n", buf);
36 va_end (ap);
37 }
38
39 #define trace_debug(FMT, args...) \
40 do { \
41 if (debug_threads) \
42 trace_debug_1 ((FMT), ##args); \
43 } while (0)
44
45 static int
46 tracepoint_handler (CORE_ADDR address)
47 {
48 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
49 paddress (address));
50 return 0;
51 }
52
53 /* This enum must exactly match what is documented in
54 gdb/doc/agentexpr.texi, including all the numerical values. */
55
56 enum gdb_agent_op
57 {
58 gdb_agent_op_float = 0x01,
59 gdb_agent_op_add = 0x02,
60 gdb_agent_op_sub = 0x03,
61 gdb_agent_op_mul = 0x04,
62 gdb_agent_op_div_signed = 0x05,
63 gdb_agent_op_div_unsigned = 0x06,
64 gdb_agent_op_rem_signed = 0x07,
65 gdb_agent_op_rem_unsigned = 0x08,
66 gdb_agent_op_lsh = 0x09,
67 gdb_agent_op_rsh_signed = 0x0a,
68 gdb_agent_op_rsh_unsigned = 0x0b,
69 gdb_agent_op_trace = 0x0c,
70 gdb_agent_op_trace_quick = 0x0d,
71 gdb_agent_op_log_not = 0x0e,
72 gdb_agent_op_bit_and = 0x0f,
73 gdb_agent_op_bit_or = 0x10,
74 gdb_agent_op_bit_xor = 0x11,
75 gdb_agent_op_bit_not = 0x12,
76 gdb_agent_op_equal = 0x13,
77 gdb_agent_op_less_signed = 0x14,
78 gdb_agent_op_less_unsigned = 0x15,
79 gdb_agent_op_ext = 0x16,
80 gdb_agent_op_ref8 = 0x17,
81 gdb_agent_op_ref16 = 0x18,
82 gdb_agent_op_ref32 = 0x19,
83 gdb_agent_op_ref64 = 0x1a,
84 gdb_agent_op_ref_float = 0x1b,
85 gdb_agent_op_ref_double = 0x1c,
86 gdb_agent_op_ref_long_double = 0x1d,
87 gdb_agent_op_l_to_d = 0x1e,
88 gdb_agent_op_d_to_l = 0x1f,
89 gdb_agent_op_if_goto = 0x20,
90 gdb_agent_op_goto = 0x21,
91 gdb_agent_op_const8 = 0x22,
92 gdb_agent_op_const16 = 0x23,
93 gdb_agent_op_const32 = 0x24,
94 gdb_agent_op_const64 = 0x25,
95 gdb_agent_op_reg = 0x26,
96 gdb_agent_op_end = 0x27,
97 gdb_agent_op_dup = 0x28,
98 gdb_agent_op_pop = 0x29,
99 gdb_agent_op_zero_ext = 0x2a,
100 gdb_agent_op_swap = 0x2b,
101 gdb_agent_op_getv = 0x2c,
102 gdb_agent_op_setv = 0x2d,
103 gdb_agent_op_tracev = 0x2e,
104 gdb_agent_op_trace16 = 0x30,
105 gdb_agent_op_last
106 };
107
108 static const char *gdb_agent_op_names [gdb_agent_op_last] =
109 {
110 "?undef?",
111 "float",
112 "add",
113 "sub",
114 "mul",
115 "div_signed",
116 "div_unsigned",
117 "rem_signed",
118 "rem_unsigned",
119 "lsh",
120 "rsh_signed",
121 "rsh_unsigned",
122 "trace",
123 "trace_quick",
124 "log_not",
125 "bit_and",
126 "bit_or",
127 "bit_xor",
128 "bit_not",
129 "equal",
130 "less_signed",
131 "less_unsigned",
132 "ext",
133 "ref8",
134 "ref16",
135 "ref32",
136 "ref64",
137 "ref_float",
138 "ref_double",
139 "ref_long_double",
140 "l_to_d",
141 "d_to_l",
142 "if_goto",
143 "goto",
144 "const8",
145 "const16",
146 "const32",
147 "const64",
148 "reg",
149 "end",
150 "dup",
151 "pop",
152 "zero_ext",
153 "swap",
154 "getv",
155 "setv",
156 "tracev",
157 "?undef?",
158 "trace16",
159 };
160
161 struct agent_expr
162 {
163 int length;
164
165 unsigned char *bytes;
166 };
167
168 /* Base action. Concrete actions inherit this. */
169
170 struct tracepoint_action
171 {
172 char type;
173 };
174
175 /* An 'M' (collect memory) action. */
176 struct collect_memory_action
177 {
178 struct tracepoint_action base;
179
180 ULONGEST addr;
181 ULONGEST len;
182 int basereg;
183 };
184
185 /* An 'R' (collect registers) action. */
186
187 struct collect_registers_action
188 {
189 struct tracepoint_action base;
190 };
191
192 /* An 'X' (evaluate expression) action. */
193
194 struct eval_expr_action
195 {
196 struct tracepoint_action base;
197
198 struct agent_expr *expr;
199 };
200
201 /* An 'L' (collect static trace data) action. */
202 struct collect_static_trace_data_action
203 {
204 struct tracepoint_action base;
205 };
206
207 /* This structure describes a piece of the source-level definition of
208 the tracepoint. The contents are not interpreted by the target,
209 but preserved verbatim for uploading upon reconnection. */
210
211 struct source_string
212 {
213 /* The type of string, such as "cond" for a conditional. */
214 char *type;
215
216 /* The source-level string itself. For the sake of target
217 debugging, we store it in plaintext, even though it is always
218 transmitted in hex. */
219 char *str;
220
221 /* Link to the next one in the list. We link them in the order
222 received, in case some make up an ordered list of commands or
223 some such. */
224 struct source_string *next;
225 };
226
227 struct tracepoint_hit_ctx;
228
229 /* The definition of a tracepoint. */
230
231 /* Tracepoints may have multiple locations, each at a different
232 address. This can occur with optimizations, template
233 instantiation, etc. Since the locations may be in different
234 scopes, the conditions and actions may be different for each
235 location. Our target version of tracepoints is more like GDB's
236 notion of "breakpoint locations", but we have almost nothing that
237 is not per-location, so we bother having two kinds of objects. The
238 key consequence is that numbers are not unique, and that it takes
239 both number and address to identify a tracepoint uniquely. */
240
241 struct tracepoint
242 {
243 /* The number of the tracepoint, as specified by GDB. Several
244 tracepoint objects here may share a number. */
245 int number;
246
247 /* Address at which the tracepoint is supposed to trigger. Several
248 tracepoints may share an address. */
249 CORE_ADDR address;
250
251 /* True if the tracepoint is currently enabled. */
252 int enabled;
253
254 /* The number of single steps that will be performed after each
255 tracepoint hit. */
256 long step_count;
257
258 /* The number of times the tracepoint may be hit before it will
259 terminate the entire tracing run. */
260 long pass_count;
261
262 /* Pointer to the agent expression that is the tracepoint's
263 conditional, or NULL if the tracepoint is unconditional. */
264 struct agent_expr *cond;
265
266 /* The list of actions to take when the tracepoint triggers. */
267 int numactions;
268 struct tracepoint_action **actions;
269 /* Same, but in string/packet form. */
270 char **actions_str;
271
272 /* The list of actions to take while in a stepping loop. */
273 int num_step_actions;
274 struct tracepoint_action **step_actions;
275 /* Same, but in string/packet form. */
276 char **step_actions_str;
277
278 /* Count of the times we've hit this tracepoint during the run.
279 Note that while-stepping steps are not counted as "hits". */
280 long hit_count;
281
282 /* The collection of strings that describe the tracepoint as it was
283 entered into GDB. These are not used by the target, but are
284 reported back to GDB upon reconnection. */
285 struct source_string *source_strings;
286
287 /* Handle returned by the breakpoint module when we inserted the
288 trap. NULL if we haven't inserted it yet. */
289 void *handle;
290
291 /* Link to the next tracepoint in the list. */
292 struct tracepoint *next;
293 };
294
295 /* Given `while-stepping', a thread may be collecting data for more
296 than one tracepoint simultaneously. On the other hand, the same
297 tracepoint with a while-stepping action may be hit by more than one
298 thread simultaneously (but not quite, each thread could be handling
299 a different step). Each thread holds a list of these objects,
300 representing the current step of each while-stepping action being
301 collected. */
302
303 struct wstep_state
304 {
305 struct wstep_state *next;
306
307 /* The tracepoint number. */
308 int tp_number;
309 /* The tracepoint's address. */
310 CORE_ADDR tp_address;
311
312 /* The number of the current step in this 'while-stepping'
313 action. */
314 long current_step;
315 };
316
317 /* The linked list of all tracepoints. */
318
319 static struct tracepoint *tracepoints;
320
321 /* Pointer to the last tracepoint in the list, new tracepoints are
322 linked in at the end. */
323
324 static struct tracepoint *last_tracepoint;
325
326 /* The first tracepoint to exceed its pass count. */
327
328 static struct tracepoint *stopping_tracepoint;
329
330 /* True if the trace buffer is full or otherwise no longer usable. */
331
332 static int trace_buffer_is_full;
333
334 /* Enumeration of the different kinds of things that can happen during
335 agent expression evaluation. */
336
337 enum eval_result_type
338 {
339 expr_eval_no_error,
340 expr_eval_empty_expression,
341 expr_eval_empty_stack,
342 expr_eval_stack_overflow,
343 expr_eval_stack_underflow,
344 expr_eval_unhandled_opcode,
345 expr_eval_unrecognized_opcode,
346 expr_eval_divide_by_zero,
347 expr_eval_invalid_goto
348 };
349
350 static enum eval_result_type expr_eval_result = expr_eval_no_error;
351
352 static const char *eval_result_names[] =
353 {
354 "terror:in the attic", /* this should never be reported */
355 "terror:empty expression",
356 "terror:empty stack",
357 "terror:stack overflow",
358 "terror:stack underflow",
359 "terror:unhandled opcode",
360 "terror:unrecognized opcode",
361 "terror:divide by zero"
362 };
363
364 /* The tracepoint in which the error occurred. */
365
366 static struct tracepoint *error_tracepoint;
367
368 struct trace_state_variable
369 {
370 /* This is the name of the variable as used in GDB. The target
371 doesn't use the name, but needs to have it for saving and
372 reconnection purposes. */
373 char *name;
374
375 /* This number identifies the variable uniquely. Numbers may be
376 assigned either by the target (in the case of builtin variables),
377 or by GDB, and are presumed unique during the course of a trace
378 experiment. */
379 int number;
380
381 /* The variable's initial value, a 64-bit signed integer always. */
382 LONGEST initial_value;
383
384 /* The variable's value, a 64-bit signed integer always. */
385 LONGEST value;
386
387 /* Pointer to a getter function, used to supply computed values. */
388 LONGEST (*getter) (void);
389
390 /* Link to the next variable. */
391 struct trace_state_variable *next;
392 };
393
394 /* Linked list of all trace state variables. */
395
396 static struct trace_state_variable *trace_state_variables;
397
398 /* The results of tracing go into a fixed-size space known as the
399 "trace buffer". Because usage follows a limited number of
400 patterns, we manage it ourselves rather than with malloc. Basic
401 rules are that we create only one trace frame at a time, each is
402 variable in size, they are never moved once created, and we only
403 discard if we are doing a circular buffer, and then only the oldest
404 ones. Each trace frame includes its own size, so we don't need to
405 link them together, and the trace frame number is relative to the
406 first one, so we don't need to record numbers. A trace frame also
407 records the number of the tracepoint that created it. The data
408 itself is a series of blocks, each introduced by a single character
409 and with a defined format. Each type of block has enough
410 type/length info to allow scanners to jump quickly from one block
411 to the next without reading each byte in the block. */
412
413 /* Trace buffer management would be simple - advance a free pointer
414 from beginning to end, then stop - were it not for the circular
415 buffer option, which is a useful way to prevent a trace run from
416 stopping prematurely because the buffer filled up. In the circular
417 case, the location of the first trace frame (trace_buffer_start)
418 moves as old trace frames are discarded. Also, since we grow trace
419 frames incrementally as actions are performed, we wrap around to
420 the beginning of the trace buffer. This is per-block, so each
421 block within a trace frame remains contiguous. Things get messy
422 when the wrapped-around trace frame is the one being discarded; the
423 free space ends up in two parts at opposite ends of the buffer. */
424
425 #ifndef ATTR_PACKED
426 # if defined(__GNUC__)
427 # define ATTR_PACKED __attribute__ ((packed))
428 # else
429 # define ATTR_PACKED /* nothing */
430 # endif
431 #endif
432
433 /* The data collected at a tracepoint hit. This object should be as
434 small as possible, since there may be a great many of them. We do
435 not need to keep a frame number, because they are all sequential
436 and there are no deletions; so the Nth frame in the buffer is
437 always frame number N. */
438
439 struct traceframe
440 {
441 /* Number of the tracepoint that collected this traceframe. A value
442 of 0 indicates the current end of the trace buffer. We make this
443 a 16-bit field because it's never going to happen that GDB's
444 numbering of tracepoints reaches 32,000. */
445 int tpnum : 16;
446
447 /* The size of the data in this trace frame. We limit this to 32
448 bits, even on a 64-bit target, because it's just implausible that
449 one is validly going to collect 4 gigabytes of data at a single
450 tracepoint hit. */
451 unsigned int data_size : 32;
452
453 /* The base of the trace data, which is contiguous from this point. */
454 unsigned char data[0];
455
456 } ATTR_PACKED traceframe_t;
457
458 /* The traceframe to be used as the source of data to send back to
459 GDB. A value of -1 means to get data from the live program. */
460
461 int current_traceframe = -1;
462
463 /* This flag is true if the trace buffer is circular, meaning that
464 when it fills, the oldest trace frames are discarded in order to
465 make room. */
466
467 static int circular_trace_buffer;
468
469 /* Pointer to the block of memory that traceframes all go into. */
470
471 static unsigned char *trace_buffer_lo;
472
473 /* Pointer to the end of the trace buffer, more precisely to the byte
474 after the end of the buffer. */
475
476 static unsigned char *trace_buffer_hi;
477
478 /* Pointer to the first trace frame in the buffer. In the
479 non-circular case, this is equal to trace_buffer_lo, otherwise it
480 moves around in the buffer. */
481
482 static unsigned char *trace_buffer_start;
483
484 /* Pointer to the free part of the trace buffer. Note that we clear
485 several bytes at and after this pointer, so that traceframe
486 scans/searches terminate properly. */
487
488 static unsigned char *trace_buffer_free;
489
490 /* Pointer to the byte after the end of the free part. Note that this
491 may be smaller than trace_buffer_free in the circular case, and
492 means that the free part is in two pieces. Initially it is equal
493 to trace_buffer_hi, then is generally equivalent to
494 trace_buffer_start. */
495
496 static unsigned char *trace_buffer_end_free;
497
498 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
499 this is the point at which the trace data breaks, and resumes at
500 trace_buffer_lo. */
501
502 static unsigned char *trace_buffer_wrap;
503
504 /* Macro that returns a pointer to the first traceframe in the buffer. */
505
506 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
507
508 /* Macro that returns a pointer to the next traceframe in the buffer.
509 If the computed location is beyond the wraparound point, subtract
510 the offset of the wraparound. */
511
512 #define NEXT_TRACEFRAME_1(TF) \
513 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
514
515 #define NEXT_TRACEFRAME(TF) \
516 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
517 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
518 ? (trace_buffer_wrap - trace_buffer_lo) \
519 : 0)))
520
521 /* The difference between these counters represents the total number
522 of complete traceframes present in the trace buffer. */
523
524 static unsigned int traceframe_write_count;
525 static unsigned int traceframe_read_count;
526
527 /* Convenience macro. */
528
529 #define traceframe_count \
530 ((unsigned int) (traceframe_write_count - traceframe_read_count))
531
532 /* The count of all traceframes created in the current run, including
533 ones that were discarded to make room. */
534
535 static int traceframes_created;
536
537 /* Read-only regions are address ranges whose contents don't change,
538 and so can be read from target memory even while looking at a trace
539 frame. Without these, disassembly for instance will likely fail,
540 because the program code is not usually collected into a trace
541 frame. This data structure does not need to be very complicated or
542 particularly efficient, it's only going to be used occasionally,
543 and only by some commands. */
544
545 struct readonly_region
546 {
547 /* The bounds of the region. */
548 CORE_ADDR start, end;
549
550 /* Link to the next one. */
551 struct readonly_region *next;
552 };
553
554 /* Linked list of readonly regions. This list stays in effect from
555 one tstart to the next. */
556
557 static struct readonly_region *readonly_regions;
558
559 /* The global that controls tracing overall. */
560
561 static int tracing;
562
563 /* The reason for the last tracing run to have stopped. We initialize
564 to a distinct string so that GDB can distinguish between "stopped
565 after running" and "stopped because never run" cases. */
566
567 static const char *tracing_stop_reason = "tnotrun";
568
569 static int tracing_stop_tpnum;
570
571 /* Functions local to this file. */
572
573 /* Base "class" for tracepoint type specific data to be passed down to
574 collect_data_at_tracepoint. */
575 struct tracepoint_hit_ctx
576 {
577 /* empty */
578 };
579
580 /* Trap tracepoint specific data to be passed down to
581 collect_data_at_tracepoint. */
582
583 struct trap_tracepoint_ctx
584 {
585 struct tracepoint_hit_ctx base;
586
587 struct regcache *regcache;
588 };
589
590 static struct agent_expr *parse_agent_expr (char **actparm);
591 static char *unparse_agent_expr (struct agent_expr *aexpr);
592 static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx,
593 struct traceframe *tframe,
594 struct agent_expr *aexpr,
595 ULONGEST *rslt);
596
597 static int agent_mem_read (struct traceframe *tframe,
598 unsigned char *to, CORE_ADDR from, ULONGEST len);
599 static int agent_tsv_read (struct traceframe *tframe, int n);
600
601 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
602 static int traceframe_read_tsv (int num, LONGEST *val);
603
604 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
605 struct tracepoint *tpoint);
606
607 static void clear_readonly_regions (void);
608 static void clear_installed_tracepoints (void);
609
610 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
611 CORE_ADDR stop_pc,
612 struct tracepoint *tpoint);
613
614 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
615 CORE_ADDR stop_pc,
616 struct tracepoint *tpoint, int current_step);
617
618 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
619 CORE_ADDR stop_pc,
620 struct tracepoint *tpoint,
621 struct traceframe *tframe,
622 struct tracepoint_action *taction);
623
624 /* Record that an error occurred during expression evaluation. */
625
626 static void
627 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
628 enum eval_result_type rtype)
629 {
630 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
631 tpoint->number, paddress (tpoint->address), which, rtype);
632
633 expr_eval_result = rtype;
634 error_tracepoint = tpoint;
635 }
636
637 /* Trace buffer management. */
638
639 static void
640 clear_trace_buffer (void)
641 {
642 trace_buffer_start = trace_buffer_lo;
643 trace_buffer_free = trace_buffer_lo;
644 trace_buffer_end_free = trace_buffer_hi;
645 trace_buffer_wrap = trace_buffer_hi;
646 /* A traceframe with zeroed fields marks the end of trace data. */
647 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
648 ((struct traceframe *) trace_buffer_free)->data_size = 0;
649 traceframe_read_count = traceframe_write_count = 0;
650 traceframes_created = 0;
651 }
652
653 static void
654 init_trace_buffer (unsigned char *buf, int bufsize)
655 {
656 trace_buffer_lo = buf;
657 trace_buffer_hi = trace_buffer_lo + bufsize;
658
659 clear_trace_buffer ();
660 }
661
662 /* Carve out a piece of the trace buffer, returning NULL in case of
663 failure. */
664
665 static void *
666 trace_buffer_alloc (size_t amt)
667 {
668 unsigned char *rslt;
669 struct traceframe *oldest;
670 unsigned char *new_start;
671
672 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
673 (long) amt, (long) sizeof (struct traceframe));
674
675 /* Account for the EOB marker. */
676 amt += sizeof (struct traceframe);
677
678 /* Offsets are easier to grok for debugging than raw addresses,
679 especially for the small trace buffer sizes that are useful for
680 testing. */
681 trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d",
682 (int) (trace_buffer_start - trace_buffer_lo),
683 (int) (trace_buffer_free - trace_buffer_lo),
684 (int) (trace_buffer_end_free - trace_buffer_lo),
685 (int) (trace_buffer_wrap - trace_buffer_lo),
686 (int) (trace_buffer_hi - trace_buffer_lo));
687
688 /* The algorithm here is to keep trying to get a contiguous block of
689 the requested size, possibly discarding older traceframes to free
690 up space. Since free space might come in one or two pieces,
691 depending on whether discarded traceframes wrapped around at the
692 high end of the buffer, we test both pieces after each
693 discard. */
694 while (1)
695 {
696 /* First, if we have two free parts, try the upper one first. */
697 if (trace_buffer_end_free < trace_buffer_free)
698 {
699 if (trace_buffer_free + amt <= trace_buffer_hi)
700 /* We have enough in the upper part. */
701 break;
702 else
703 {
704 /* Our high part of free space wasn't enough. Give up
705 on it for now, set wraparound. We will recover the
706 space later, if/when the wrapped-around traceframe is
707 discarded. */
708 trace_debug ("Upper part too small, setting wraparound");
709 trace_buffer_wrap = trace_buffer_free;
710 trace_buffer_free = trace_buffer_lo;
711 }
712 }
713
714 /* The normal case. */
715 if (trace_buffer_free + amt <= trace_buffer_end_free)
716 break;
717
718 /* If we're here, then neither part is big enough, and
719 non-circular trace buffers are now full. */
720 if (!circular_trace_buffer)
721 {
722 trace_debug ("Not enough space in the trace buffer");
723 return NULL;
724 }
725
726 trace_debug ("Need more space in the trace buffer");
727
728 /* If we have a circular buffer, we can try discarding the
729 oldest traceframe and see if that helps. */
730 oldest = FIRST_TRACEFRAME ();
731 if (oldest->tpnum == 0)
732 {
733 /* Not good; we have no traceframes to free. Perhaps we're
734 asking for a block that is larger than the buffer? In
735 any case, give up. */
736 trace_debug ("No traceframes to discard");
737 return NULL;
738 }
739
740 --traceframe_write_count;
741
742 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
743 /* If we freed the traceframe that wrapped around, go back
744 to the non-wrap case. */
745 if (new_start < trace_buffer_start)
746 {
747 trace_debug ("Discarding past the wraparound");
748 trace_buffer_wrap = trace_buffer_hi;
749 }
750 trace_buffer_start = new_start;
751 trace_buffer_end_free = trace_buffer_start;
752
753 trace_debug ("Discarded a traceframe\n"
754 "Trace buffer, start=%d free=%d endfree=%d wrap=%d hi=%d",
755 (int) (trace_buffer_start - trace_buffer_lo),
756 (int) (trace_buffer_free - trace_buffer_lo),
757 (int) (trace_buffer_end_free - trace_buffer_lo),
758 (int) (trace_buffer_wrap - trace_buffer_lo),
759 (int) (trace_buffer_hi - trace_buffer_lo));
760
761 /* Now go back around the loop. The discard might have resulted
762 in either one or two pieces of free space, so we want to try
763 both before freeing any more traceframes. */
764 }
765
766 /* If we get here, we know we can provide the asked-for space. */
767
768 rslt = trace_buffer_free;
769
770 /* Adjust the request back down, now that we know we have space for
771 the marker. */
772 trace_buffer_free += (amt - sizeof (struct traceframe));
773
774 /* We have a new piece of the trace buffer. Hurray! */
775
776 /* Add an EOB marker just past this allocation. */
777 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
778 ((struct traceframe *) trace_buffer_free)->data_size = 0;
779
780 /* Adjust the request back down, now that we know we have space for
781 the marker. */
782 amt -= sizeof (struct traceframe);
783
784 if (debug_threads)
785 {
786 trace_debug ("Allocated %d bytes", (int) amt);
787 trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d",
788 (int) (trace_buffer_start - trace_buffer_lo),
789 (int) (trace_buffer_free - trace_buffer_lo),
790 (int) (trace_buffer_end_free - trace_buffer_lo),
791 (int) (trace_buffer_wrap - trace_buffer_lo),
792 (int) (trace_buffer_hi - trace_buffer_lo));
793 }
794
795 return rslt;
796 }
797
798 /* Return the total free space. This is not necessarily the largest
799 block we can allocate, because of the two-part case. */
800
801 static int
802 free_space (void)
803 {
804 if (trace_buffer_free <= trace_buffer_end_free)
805 return trace_buffer_end_free - trace_buffer_free;
806 else
807 return ((trace_buffer_end_free - trace_buffer_lo)
808 + (trace_buffer_hi - trace_buffer_free));
809 }
810
811 /* An 'S' in continuation packets indicates remainder are for
812 while-stepping. */
813
814 static int seen_step_action_flag;
815
816 /* Create a tracepoint (location) with given number and address. */
817
818 static struct tracepoint *
819 add_tracepoint (int num, CORE_ADDR addr)
820 {
821 struct tracepoint *tpoint;
822
823 tpoint = xmalloc (sizeof (struct tracepoint));
824 tpoint->number = num;
825 tpoint->address = addr;
826 tpoint->numactions = 0;
827 tpoint->actions = NULL;
828 tpoint->actions_str = NULL;
829 tpoint->cond = NULL;
830 tpoint->num_step_actions = 0;
831 tpoint->step_actions = NULL;
832 tpoint->step_actions_str = NULL;
833 tpoint->source_strings = NULL;
834 tpoint->handle = NULL;
835 tpoint->next = NULL;
836
837 if (!last_tracepoint)
838 tracepoints = tpoint;
839 else
840 last_tracepoint->next = tpoint;
841 last_tracepoint = tpoint;
842
843 seen_step_action_flag = 0;
844
845 return tpoint;
846 }
847
848 /* Return the tracepoint with the given number and address, or NULL. */
849
850 static struct tracepoint *
851 find_tracepoint (int id, CORE_ADDR addr)
852 {
853 struct tracepoint *tpoint;
854
855 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
856 if (tpoint->number == id && tpoint->address == addr)
857 return tpoint;
858
859 return NULL;
860 }
861
862 /* There may be several tracepoints with the same number (because they
863 are "locations", in GDB parlance); return the next one after the
864 given tracepoint, or search from the beginning of the list if the
865 first argument is NULL. */
866
867 static struct tracepoint *
868 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
869 {
870 struct tracepoint *tpoint;
871
872 if (prev_tp)
873 tpoint = prev_tp->next;
874 else
875 tpoint = tracepoints;
876 for (; tpoint; tpoint = tpoint->next)
877 if (tpoint->number == num)
878 return tpoint;
879
880 return NULL;
881 }
882
883 static char *
884 save_string (const char *str, size_t len)
885 {
886 char *s;
887
888 s = xmalloc (len + 1);
889 memcpy (s, str, len);
890 s[len] = '\0';
891
892 return s;
893 }
894
895 /* Append another action to perform when the tracepoint triggers. */
896
897 static void
898 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
899 {
900 char *act;
901
902 if (*packet == 'S')
903 {
904 seen_step_action_flag = 1;
905 ++packet;
906 }
907
908 act = packet;
909
910 while (*act)
911 {
912 char *act_start = act;
913 struct tracepoint_action *action = NULL;
914
915 switch (*act)
916 {
917 case 'M':
918 {
919 struct collect_memory_action *maction;
920 ULONGEST basereg;
921 int is_neg;
922
923 maction = xmalloc (sizeof *maction);
924 maction->base.type = *act;
925 action = &maction->base;
926
927 ++act;
928 is_neg = (*act == '-');
929 if (*act == '-')
930 ++act;
931 act = unpack_varlen_hex (act, &basereg);
932 ++act;
933 act = unpack_varlen_hex (act, &maction->addr);
934 ++act;
935 act = unpack_varlen_hex (act, &maction->len);
936 maction->basereg = (is_neg
937 ? - (int) basereg
938 : (int) basereg);
939 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
940 pulongest (maction->len),
941 paddress (maction->addr), maction->basereg);
942 break;
943 }
944 case 'R':
945 {
946 struct collect_registers_action *raction;
947
948 raction = xmalloc (sizeof *raction);
949 raction->base.type = *act;
950 action = &raction->base;
951
952 trace_debug ("Want to collect registers");
953 ++act;
954 /* skip past hex digits of mask for now */
955 while (isxdigit(*act))
956 ++act;
957 break;
958 }
959 case 'S':
960 trace_debug ("Unexpected step action, ignoring");
961 ++act;
962 break;
963 case 'X':
964 {
965 struct eval_expr_action *xaction;
966
967 xaction = xmalloc (sizeof (*xaction));
968 xaction->base.type = *act;
969 action = &xaction->base;
970
971 trace_debug ("Want to evaluate expression");
972 xaction->expr = parse_agent_expr (&act);
973 break;
974 }
975 default:
976 trace_debug ("unknown trace action '%c', ignoring...", *act);
977 break;
978 case '-':
979 break;
980 }
981
982 if (action == NULL)
983 break;
984
985 if (seen_step_action_flag)
986 {
987 tpoint->num_step_actions++;
988
989 tpoint->step_actions
990 = xrealloc (tpoint->step_actions,
991 (sizeof (*tpoint->step_actions)
992 * tpoint->num_step_actions));
993 tpoint->step_actions_str
994 = xrealloc (tpoint->step_actions_str,
995 (sizeof (*tpoint->step_actions_str)
996 * tpoint->num_step_actions));
997 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
998 tpoint->step_actions_str[tpoint->num_step_actions - 1]
999 = save_string (act_start, act - act_start);
1000 }
1001 else
1002 {
1003 tpoint->numactions++;
1004 tpoint->actions
1005 = xrealloc (tpoint->actions,
1006 sizeof (*tpoint->actions) * tpoint->numactions);
1007 tpoint->actions_str
1008 = xrealloc (tpoint->actions_str,
1009 sizeof (*tpoint->actions_str) * tpoint->numactions);
1010 tpoint->actions[tpoint->numactions - 1] = action;
1011 tpoint->actions_str[tpoint->numactions - 1]
1012 = save_string (act_start, act - act_start);
1013 }
1014 }
1015 }
1016
1017 /* Find or create a trace state variable with the given number. */
1018
1019 static struct trace_state_variable *
1020 get_trace_state_variable (int num)
1021 {
1022 struct trace_state_variable *tsv;
1023
1024 /* Search for an existing variable. */
1025 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1026 if (tsv->number == num)
1027 return tsv;
1028
1029 return NULL;
1030 }
1031
1032 /* Find or create a trace state variable with the given number. */
1033
1034 static struct trace_state_variable *
1035 create_trace_state_variable (int num)
1036 {
1037 struct trace_state_variable *tsv;
1038
1039 tsv = get_trace_state_variable (num);
1040 if (tsv != NULL)
1041 return tsv;
1042
1043 /* Create a new variable. */
1044 tsv = xmalloc (sizeof (struct trace_state_variable));
1045 tsv->number = num;
1046 tsv->initial_value = 0;
1047 tsv->value = 0;
1048 tsv->getter = NULL;
1049 tsv->name = NULL;
1050 tsv->next = trace_state_variables;
1051 trace_state_variables = tsv;
1052
1053 return tsv;
1054 }
1055
1056 static LONGEST
1057 get_trace_state_variable_value (int num)
1058 {
1059 struct trace_state_variable *tsv;
1060
1061 tsv = get_trace_state_variable (num);
1062
1063 if (!tsv)
1064 {
1065 trace_debug ("No trace state variable %d, skipping value get", num);
1066 return 0;
1067 }
1068
1069 /* Call a getter function if we have one. While it's tempting to
1070 set up something to only call the getter once per tracepoint hit,
1071 it could run afoul of thread races. Better to let the getter
1072 handle it directly, if necessary to worry about it. */
1073 if (tsv->getter)
1074 tsv->value = (tsv->getter) ();
1075
1076 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1077 num, plongest (tsv->value));
1078
1079 return tsv->value;
1080 }
1081
1082 static void
1083 set_trace_state_variable_value (int num, LONGEST val)
1084 {
1085 struct trace_state_variable *tsv;
1086
1087 tsv = get_trace_state_variable (num);
1088
1089 if (!tsv)
1090 {
1091 trace_debug ("No trace state variable %d, skipping value set", num);
1092 return;
1093 }
1094
1095 tsv->value = val;
1096 }
1097
1098 static void
1099 set_trace_state_variable_name (int num, const char *name)
1100 {
1101 struct trace_state_variable *tsv;
1102
1103 tsv = get_trace_state_variable (num);
1104
1105 if (!tsv)
1106 {
1107 trace_debug ("No trace state variable %d, skipping name set", num);
1108 return;
1109 }
1110
1111 tsv->name = (char *) name;
1112 }
1113
1114 static void
1115 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
1116 {
1117 struct trace_state_variable *tsv;
1118
1119 tsv = get_trace_state_variable (num);
1120
1121 if (!tsv)
1122 {
1123 trace_debug ("No trace state variable %d, skipping getter set", num);
1124 return;
1125 }
1126
1127 tsv->getter = getter;
1128 }
1129
1130 /* Add a raw traceframe for the given tracepoint. */
1131
1132 static struct traceframe *
1133 add_traceframe (struct tracepoint *tpoint)
1134 {
1135 struct traceframe *tframe;
1136
1137 tframe = trace_buffer_alloc (sizeof (struct traceframe));
1138
1139 if (tframe == NULL)
1140 return NULL;
1141
1142 tframe->tpnum = tpoint->number;
1143 tframe->data_size = 0;
1144
1145 return tframe;
1146 }
1147
1148 /* Add a block to the traceframe currently being worked on. */
1149
1150 static unsigned char *
1151 add_traceframe_block (struct traceframe *tframe, int amt)
1152 {
1153 unsigned char *block;
1154
1155 if (!tframe)
1156 return NULL;
1157
1158 block = trace_buffer_alloc (amt);
1159
1160 if (!block)
1161 return NULL;
1162
1163 tframe->data_size += amt;
1164
1165 return block;
1166 }
1167
1168 /* Flag that the current traceframe is finished. */
1169
1170 static void
1171 finish_traceframe (struct traceframe *tframe)
1172 {
1173 ++traceframe_write_count;
1174 ++traceframes_created;
1175 }
1176
1177 /* Given a traceframe number NUM, find the NUMth traceframe in the
1178 buffer. */
1179
1180 static struct traceframe *
1181 find_traceframe (int num)
1182 {
1183 struct traceframe *tframe;
1184 int tfnum = 0;
1185
1186 for (tframe = FIRST_TRACEFRAME ();
1187 tframe->tpnum != 0;
1188 tframe = NEXT_TRACEFRAME (tframe))
1189 {
1190 if (tfnum == num)
1191 return tframe;
1192 ++tfnum;
1193 }
1194
1195 return NULL;
1196 }
1197
1198 static CORE_ADDR
1199 get_traceframe_address (struct traceframe *tframe)
1200 {
1201 CORE_ADDR addr;
1202 struct tracepoint *tpoint;
1203
1204 addr = traceframe_get_pc (tframe);
1205
1206 if (addr)
1207 return addr;
1208
1209 /* Fallback strategy, will be incorrect for while-stepping frames
1210 and multi-location tracepoints. */
1211 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
1212 return tpoint->address;
1213 }
1214
1215 /* Search for the next traceframe whose address is inside or outside
1216 the given range. */
1217
1218 static struct traceframe *
1219 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
1220 int *tfnump)
1221 {
1222 struct traceframe *tframe;
1223 CORE_ADDR tfaddr;
1224
1225 *tfnump = current_traceframe + 1;
1226 tframe = find_traceframe (*tfnump);
1227 /* The search is not supposed to wrap around. */
1228 if (!tframe)
1229 {
1230 *tfnump = -1;
1231 return NULL;
1232 }
1233
1234 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
1235 {
1236 tfaddr = get_traceframe_address (tframe);
1237 if (inside_p
1238 ? (lo <= tfaddr && tfaddr <= hi)
1239 : (lo > tfaddr || tfaddr > hi))
1240 return tframe;
1241 ++*tfnump;
1242 }
1243
1244 *tfnump = -1;
1245 return NULL;
1246 }
1247
1248 /* Search for the next traceframe recorded by the given tracepoint.
1249 Note that for multi-location tracepoints, this will find whatever
1250 location appears first. */
1251
1252 static struct traceframe *
1253 find_next_traceframe_by_tracepoint (int num, int *tfnump)
1254 {
1255 struct traceframe *tframe;
1256
1257 *tfnump = current_traceframe + 1;
1258 tframe = find_traceframe (*tfnump);
1259 /* The search is not supposed to wrap around. */
1260 if (!tframe)
1261 {
1262 *tfnump = -1;
1263 return NULL;
1264 }
1265
1266 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
1267 {
1268 if (tframe->tpnum == num)
1269 return tframe;
1270 ++*tfnump;
1271 }
1272
1273 *tfnump = -1;
1274 return NULL;
1275 }
1276
1277 /* Clear all past trace state. */
1278
1279 static void
1280 cmd_qtinit (char *packet)
1281 {
1282 struct trace_state_variable *tsv, *prev, *next;
1283
1284 /* Make sure we don't try to read from a trace frame. */
1285 current_traceframe = -1;
1286
1287 trace_debug ("Initializing the trace");
1288
1289 clear_installed_tracepoints ();
1290 clear_readonly_regions ();
1291
1292 tracepoints = NULL;
1293 last_tracepoint = NULL;
1294
1295 /* Clear out any leftover trace state variables. Ones with target
1296 defined getters should be kept however. */
1297 prev = NULL;
1298 tsv = trace_state_variables;
1299 while (tsv)
1300 {
1301 trace_debug ("Looking at var %d", tsv->number);
1302 if (tsv->getter == NULL)
1303 {
1304 next = tsv->next;
1305 if (prev)
1306 prev->next = next;
1307 else
1308 trace_state_variables = next;
1309 trace_debug ("Deleting var %d", tsv->number);
1310 free (tsv);
1311 tsv = next;
1312 }
1313 else
1314 {
1315 prev = tsv;
1316 tsv = tsv->next;
1317 }
1318 }
1319
1320 clear_trace_buffer ();
1321
1322 write_ok (packet);
1323 }
1324
1325 /* Restore the program to its pre-tracing state. This routine may be called
1326 in error situations, so it needs to be careful about only restoring
1327 from known-valid bits. */
1328
1329 static void
1330 clear_installed_tracepoints (void)
1331 {
1332 struct tracepoint *tpoint;
1333 struct tracepoint *prev_stpoint;
1334
1335 prev_stpoint = NULL;
1336
1337 /* Restore any bytes overwritten by tracepoints. */
1338 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1339 {
1340 if (!tpoint->enabled)
1341 continue;
1342
1343 /* Catch the case where we might try to remove a tracepoint that
1344 was never actually installed. */
1345 if (tpoint->handle == NULL)
1346 {
1347 trace_debug ("Tracepoint %d at 0x%s was "
1348 "never installed, nothing to clear",
1349 tpoint->number, paddress (tpoint->address));
1350 continue;
1351 }
1352
1353 delete_breakpoint (tpoint->handle);
1354 tpoint->handle = NULL;
1355 }
1356 }
1357
1358 /* Parse a packet that defines a tracepoint. */
1359
1360 static void
1361 cmd_qtdp (char *own_buf)
1362 {
1363 int tppacket;
1364 ULONGEST num;
1365 ULONGEST addr;
1366 ULONGEST count;
1367 struct tracepoint *tpoint;
1368 char *actparm;
1369 char *packet = own_buf;
1370
1371 packet += strlen ("QTDP:");
1372
1373 /* A hyphen at the beginning marks a packet specifying actions for a
1374 tracepoint already supplied. */
1375 tppacket = 1;
1376 if (*packet == '-')
1377 {
1378 tppacket = 0;
1379 ++packet;
1380 }
1381 packet = unpack_varlen_hex (packet, &num);
1382 ++packet; /* skip a colon */
1383 packet = unpack_varlen_hex (packet, &addr);
1384 ++packet; /* skip a colon */
1385
1386 /* See if we already have this tracepoint. */
1387 tpoint = find_tracepoint (num, addr);
1388
1389 if (tppacket)
1390 {
1391 /* Duplicate tracepoints are never allowed. */
1392 if (tpoint)
1393 {
1394 trace_debug ("Tracepoint error: tracepoint %d"
1395 " at 0x%s already exists",
1396 (int) num, paddress (addr));
1397 write_enn (own_buf);
1398 return;
1399 }
1400
1401 tpoint = add_tracepoint (num, addr);
1402
1403 tpoint->enabled = (*packet == 'E');
1404 ++packet; /* skip 'E' */
1405 ++packet; /* skip a colon */
1406 packet = unpack_varlen_hex (packet, &count);
1407 tpoint->step_count = count;
1408 ++packet; /* skip a colon */
1409 packet = unpack_varlen_hex (packet, &count);
1410 tpoint->pass_count = count;
1411 /* See if we have any of the additional optional fields. */
1412 while (*packet == ':')
1413 {
1414 ++packet;
1415 if (*packet == 'X')
1416 {
1417 actparm = (char *) packet;
1418 tpoint->cond = parse_agent_expr (&actparm);
1419 packet = actparm;
1420 }
1421 else if (*packet == '-')
1422 break;
1423 else if (*packet == '\0')
1424 break;
1425 else
1426 trace_debug ("Unknown optional tracepoint field");
1427 }
1428 if (*packet == '-')
1429 trace_debug ("Also has actions\n");
1430
1431 trace_debug ("Defined tracepoint %d at 0x%s, "
1432 "enabled %d step %ld pass %ld",
1433 tpoint->number, paddress (tpoint->address),
1434 tpoint->enabled,
1435 tpoint->step_count, tpoint->pass_count);
1436 }
1437 else if (tpoint)
1438 add_tracepoint_action (tpoint, packet);
1439 else
1440 {
1441 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1442 (int) num, paddress (addr));
1443 write_enn (own_buf);
1444 return;
1445 }
1446
1447 write_ok (own_buf);
1448 }
1449
1450 static void
1451 cmd_qtdpsrc (char *own_buf)
1452 {
1453 ULONGEST num, addr, start, slen;
1454 struct tracepoint *tpoint;
1455 char *packet = own_buf;
1456 char *saved, *srctype, *src;
1457 size_t nbytes;
1458 struct source_string *last, *newlast;
1459
1460 packet += strlen ("QTDPsrc:");
1461
1462 packet = unpack_varlen_hex (packet, &num);
1463 ++packet; /* skip a colon */
1464 packet = unpack_varlen_hex (packet, &addr);
1465 ++packet; /* skip a colon */
1466
1467 /* See if we already have this tracepoint. */
1468 tpoint = find_tracepoint (num, addr);
1469
1470 if (!tpoint)
1471 {
1472 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1473 (int) num, paddress (addr));
1474 write_enn (own_buf);
1475 return;
1476 }
1477
1478 saved = packet;
1479 packet = strchr (packet, ':');
1480 srctype = xmalloc (packet - saved + 1);
1481 memcpy (srctype, saved, packet - saved);
1482 srctype[packet - saved] = '\0';
1483 ++packet;
1484 packet = unpack_varlen_hex (packet, &start);
1485 ++packet; /* skip a colon */
1486 packet = unpack_varlen_hex (packet, &slen);
1487 ++packet; /* skip a colon */
1488 src = xmalloc (slen + 1);
1489 nbytes = unhexify (src, packet, strlen (packet) / 2);
1490 src[nbytes] = '\0';
1491
1492 newlast = xmalloc (sizeof (struct source_string));
1493 newlast->type = srctype;
1494 newlast->str = src;
1495 newlast->next = NULL;
1496 /* Always add a source string to the end of the list;
1497 this keeps sequences of actions/commands in the right
1498 order. */
1499 if (tpoint->source_strings)
1500 {
1501 for (last = tpoint->source_strings; last->next; last = last->next)
1502 ;
1503 last->next = newlast;
1504 }
1505 else
1506 tpoint->source_strings = newlast;
1507
1508 write_ok (own_buf);
1509 }
1510
1511 static void
1512 cmd_qtdv (char *own_buf)
1513 {
1514 ULONGEST num, val, builtin;
1515 char *varname;
1516 size_t nbytes;
1517 struct trace_state_variable *tsv;
1518 char *packet = own_buf;
1519
1520 packet += strlen ("QTDV:");
1521
1522 packet = unpack_varlen_hex (packet, &num);
1523 ++packet; /* skip a colon */
1524 packet = unpack_varlen_hex (packet, &val);
1525 ++packet; /* skip a colon */
1526 packet = unpack_varlen_hex (packet, &builtin);
1527 ++packet; /* skip a colon */
1528
1529 nbytes = strlen (packet) / 2;
1530 varname = xmalloc (nbytes + 1);
1531 nbytes = unhexify (varname, packet, nbytes);
1532 varname[nbytes] = '\0';
1533
1534 tsv = create_trace_state_variable (num);
1535 tsv->initial_value = (LONGEST) val;
1536 tsv->name = varname;
1537
1538 set_trace_state_variable_value (num, (LONGEST) val);
1539
1540 write_ok (own_buf);
1541 }
1542
1543 static void
1544 cmd_qtv (char *own_buf)
1545 {
1546 ULONGEST num;
1547 LONGEST val;
1548 int err;
1549 char *packet = own_buf;
1550
1551 packet += strlen ("qTV:");
1552 packet = unpack_varlen_hex (packet, &num);
1553
1554 if (current_traceframe >= 0)
1555 {
1556 err = traceframe_read_tsv ((int) num, &val);
1557 if (err)
1558 {
1559 strcpy (own_buf, "U");
1560 return;
1561 }
1562 }
1563 /* Only make tsv's be undefined before the first trace run. After a
1564 trace run is over, the user might want to see the last value of
1565 the tsv, and it might not be available in a traceframe. */
1566 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
1567 {
1568 strcpy (own_buf, "U");
1569 return;
1570 }
1571 else
1572 val = get_trace_state_variable_value (num);
1573
1574 sprintf (own_buf, "V%s", phex_nz (val, 0));
1575 }
1576
1577 /* Clear out the list of readonly regions. */
1578
1579 static void
1580 clear_readonly_regions (void)
1581 {
1582 struct readonly_region *roreg;
1583
1584 while (readonly_regions)
1585 {
1586 roreg = readonly_regions;
1587 readonly_regions = readonly_regions->next;
1588 free (roreg);
1589 }
1590 }
1591
1592 /* Parse the collection of address ranges whose contents GDB believes
1593 to be unchanging and so can be read directly from target memory
1594 even while looking at a traceframe. */
1595
1596 static void
1597 cmd_qtro (char *own_buf)
1598 {
1599 ULONGEST start, end;
1600 struct readonly_region *roreg;
1601 char *packet = own_buf;
1602
1603 trace_debug ("Want to mark readonly regions");
1604
1605 clear_readonly_regions ();
1606
1607 packet += strlen ("QTro");
1608
1609 while (*packet == ':')
1610 {
1611 ++packet; /* skip a colon */
1612 packet = unpack_varlen_hex (packet, &start);
1613 ++packet; /* skip a comma */
1614 packet = unpack_varlen_hex (packet, &end);
1615 roreg = xmalloc (sizeof (struct readonly_region));
1616 roreg->start = start;
1617 roreg->end = end;
1618 roreg->next = readonly_regions;
1619 readonly_regions = roreg;
1620 trace_debug ("Added readonly region from 0x%s to 0x%s",
1621 paddress (roreg->start), paddress (roreg->end));
1622 }
1623
1624 write_ok (own_buf);
1625 }
1626
1627 /* Test to see if the given range is in our list of readonly ranges.
1628 We only test for being entirely within a range, GDB is not going to
1629 send a single memory packet that spans multiple regions. */
1630
1631 int
1632 in_readonly_region (CORE_ADDR addr, ULONGEST length)
1633 {
1634 struct readonly_region *roreg;
1635
1636 for (roreg = readonly_regions; roreg; roreg = roreg->next)
1637 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
1638 return 1;
1639
1640 return 0;
1641 }
1642
1643 static void
1644 cmd_qtstart (char *packet)
1645 {
1646 struct tracepoint *tpoint;
1647 int slow_tracepoint_count;
1648
1649 trace_debug ("Starting the trace");
1650
1651 slow_tracepoint_count = 0;
1652
1653 *packet = '\0';
1654
1655 /* Install tracepoints. */
1656 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1657 {
1658 /* Ensure all the hit counts start at zero. */
1659 tpoint->hit_count = 0;
1660
1661 if (!tpoint->enabled)
1662 continue;
1663
1664 ++slow_tracepoint_count;
1665
1666 /* Tracepoints are installed as memory breakpoints. Just go
1667 ahead and install the trap. The breakpoints module handles
1668 duplicated breakpoints, and the memory read routine handles
1669 un-patching traps from memory reads. */
1670 tpoint->handle = set_breakpoint_at (tpoint->address, tracepoint_handler);
1671
1672 /* Any failure is sufficient cause to give up. */
1673 if (tpoint->handle == NULL)
1674 break;
1675 }
1676
1677 /* Any error in tracepoint insertion is unacceptable; better to
1678 address the problem now, than end up with a useless or misleading
1679 trace run. */
1680 if (tpoint != NULL)
1681 {
1682 clear_installed_tracepoints ();
1683 if (*packet == '\0')
1684 write_enn (packet);
1685 return;
1686 }
1687
1688 stopping_tracepoint = NULL;
1689 trace_buffer_is_full = 0;
1690 expr_eval_result = expr_eval_no_error;
1691 error_tracepoint = NULL;
1692
1693 /* Tracing is now active, hits will now start being logged. */
1694 tracing = 1;
1695
1696 write_ok (packet);
1697 }
1698
1699 /* End a tracing run, filling in a stop reason to report back to GDB,
1700 and removing the tracepoints from the code. */
1701
1702 static void
1703 stop_tracing (void)
1704 {
1705 if (!tracing)
1706 {
1707 trace_debug ("Tracing is already off, ignoring");
1708 return;
1709 }
1710
1711 trace_debug ("Stopping the trace");
1712
1713 /* Stop logging. Tracepoints can still be hit, but they will not be
1714 recorded. */
1715 tracing = 0;
1716
1717 tracing_stop_reason = "t???";
1718 tracing_stop_tpnum = 0;
1719 if (stopping_tracepoint)
1720 {
1721 trace_debug ("Stopping the trace because "
1722 "tracepoint %d was hit %ld times",
1723 stopping_tracepoint->number,
1724 stopping_tracepoint->pass_count);
1725 tracing_stop_reason = "tpasscount";
1726 tracing_stop_tpnum = stopping_tracepoint->number;
1727 }
1728 else if (trace_buffer_is_full)
1729 {
1730 trace_debug ("Stopping the trace because the trace buffer is full");
1731 tracing_stop_reason = "tfull";
1732 }
1733 else if (expr_eval_result != expr_eval_no_error)
1734 {
1735 trace_debug ("Stopping the trace because of an expression eval error");
1736 tracing_stop_reason = eval_result_names[expr_eval_result];
1737 tracing_stop_tpnum = error_tracepoint->number;
1738 }
1739 else
1740 {
1741 trace_debug ("Stopping the trace because of a tstop command");
1742 tracing_stop_reason = "tstop";
1743 }
1744
1745 stopping_tracepoint = NULL;
1746 error_tracepoint = NULL;
1747
1748 /* Clear out the tracepoints. */
1749 clear_installed_tracepoints ();
1750 }
1751
1752 static void
1753 cmd_qtstop (char *packet)
1754 {
1755 stop_tracing ();
1756 write_ok (packet);
1757 }
1758
1759 static void
1760 cmd_qtframe (char *own_buf)
1761 {
1762 ULONGEST frame, pc, lo, hi, num;
1763 int tfnum, tpnum;
1764 struct traceframe *tframe;
1765 char *packet = own_buf;
1766
1767 packet += strlen ("QTFrame:");
1768
1769 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
1770 {
1771 packet += strlen ("pc:");
1772 packet = unpack_varlen_hex (packet, &pc);
1773 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
1774 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
1775 }
1776 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
1777 {
1778 packet += strlen ("range:");
1779 packet = unpack_varlen_hex (packet, &lo);
1780 ++packet;
1781 packet = unpack_varlen_hex (packet, &hi);
1782 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
1783 paddress (lo), paddress (hi));
1784 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
1785 }
1786 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
1787 {
1788 packet += strlen ("outside:");
1789 packet = unpack_varlen_hex (packet, &lo);
1790 ++packet;
1791 packet = unpack_varlen_hex (packet, &hi);
1792 trace_debug ("Want to find next traceframe "
1793 "outside the range 0x%s to 0x%s",
1794 paddress (lo), paddress (hi));
1795 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
1796 }
1797 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
1798 {
1799 packet += strlen ("tdp:");
1800 packet = unpack_varlen_hex (packet, &num);
1801 tpnum = (int) num;
1802 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
1803 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
1804 }
1805 else
1806 {
1807 unpack_varlen_hex (packet, &frame);
1808 tfnum = (int) frame;
1809 if (tfnum == -1)
1810 {
1811 trace_debug ("Want to stop looking at traceframes");
1812 current_traceframe = -1;
1813 write_ok (own_buf);
1814 return;
1815 }
1816 trace_debug ("Want to look at traceframe %d", tfnum);
1817 tframe = find_traceframe (tfnum);
1818 }
1819
1820 if (tframe)
1821 {
1822 current_traceframe = tfnum;
1823 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
1824 }
1825 else
1826 sprintf (own_buf, "F-1");
1827 }
1828
1829 static void
1830 cmd_qtstatus (char *packet)
1831 {
1832 char *stop_reason_rsp = NULL;
1833
1834 trace_debug ("Returning trace status as %d, stop reason %s",
1835 tracing, tracing_stop_reason);
1836
1837 stop_reason_rsp = (char *) tracing_stop_reason;
1838
1839 /* The user visible error string in terror needs to be hex encoded.
1840 We leave it as plain string in `tracepoint_stop_reason' to ease
1841 debugging. */
1842 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
1843 {
1844 const char *result_name;
1845 int hexstr_len;
1846 char *p;
1847
1848 result_name = stop_reason_rsp + strlen ("terror:");
1849 hexstr_len = strlen (result_name) * 2;
1850 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
1851 strcpy (p, "terror:");
1852 p += strlen (p);
1853 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
1854 }
1855
1856 sprintf (packet, "T%d;%s:%x;tframes:%x;tcreated:%x;tfree:%x;tsize:%s;circular:%d",
1857 tracing ? 1 : 0,
1858 stop_reason_rsp, tracing_stop_tpnum,
1859 traceframe_count, traceframes_created,
1860 free_space (),
1861 phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
1862 circular_trace_buffer);
1863 }
1864
1865 /* State variables to help return all the tracepoint bits. */
1866 static struct tracepoint *cur_tpoint;
1867 static int cur_action;
1868 static int cur_step_action;
1869 static struct source_string *cur_source_string;
1870 static struct trace_state_variable *cur_tsv;
1871
1872 /* Compose a response that is an imitation of the syntax by which the
1873 tracepoint was originally downloaded. */
1874
1875 static void
1876 response_tracepoint (char *packet, struct tracepoint *tpoint)
1877 {
1878 char *buf;
1879
1880 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
1881 paddress (tpoint->address),
1882 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
1883 tpoint->pass_count);
1884
1885 if (tpoint->cond)
1886 {
1887 buf = unparse_agent_expr (tpoint->cond);
1888 sprintf (packet + strlen (packet), ":X%x,%s",
1889 tpoint->cond->length, buf);
1890 free (buf);
1891 }
1892 }
1893
1894 /* Compose a response that is an imitation of the syntax by which the
1895 tracepoint action was originally downloaded (with the difference
1896 that due to the way we store the actions, this will output a packet
1897 per action, while GDB could have combined more than one action
1898 per-packet. */
1899
1900 static void
1901 response_action (char *packet, struct tracepoint *tpoint,
1902 char *taction, int step)
1903 {
1904 sprintf (packet, "%c%x:%s:%s",
1905 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
1906 taction);
1907 }
1908
1909 /* Compose a response that is an imitation of the syntax by which the
1910 tracepoint source piece was originally downloaded. */
1911
1912 static void
1913 response_source (char *packet,
1914 struct tracepoint *tpoint, struct source_string *src)
1915 {
1916 char *buf;
1917 int len;
1918
1919 len = strlen (src->str);
1920 buf = alloca (len * 2 + 1);
1921 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
1922
1923 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
1924 tpoint->number, paddress (tpoint->address),
1925 src->type, 0, len, buf);
1926 }
1927
1928 /* Return the first piece of tracepoint definition, and initialize the
1929 state machine that will iterate through all the tracepoint
1930 bits. */
1931
1932 static void
1933 cmd_qtfp (char *packet)
1934 {
1935 trace_debug ("Returning first tracepoint definition piece");
1936
1937 cur_tpoint = tracepoints;
1938 cur_action = cur_step_action = -1;
1939 cur_source_string = NULL;
1940
1941 if (cur_tpoint)
1942 response_tracepoint (packet, cur_tpoint);
1943 else
1944 strcpy (packet, "l");
1945 }
1946
1947 /* Return additional pieces of tracepoint definition. Each action and
1948 stepping action must go into its own packet, because of packet size
1949 limits, and so we use state variables to deliver one piece at a
1950 time. */
1951
1952 static void
1953 cmd_qtsp (char *packet)
1954 {
1955 trace_debug ("Returning subsequent tracepoint definition piece");
1956
1957 if (!cur_tpoint)
1958 {
1959 /* This case would normally never occur, but be prepared for
1960 GDB misbehavior. */
1961 strcpy (packet, "l");
1962 }
1963 else if (cur_action < cur_tpoint->numactions - 1)
1964 {
1965 ++cur_action;
1966 response_action (packet, cur_tpoint,
1967 cur_tpoint->actions_str[cur_action], 0);
1968 }
1969 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
1970 {
1971 ++cur_step_action;
1972 response_action (packet, cur_tpoint,
1973 cur_tpoint->step_actions_str[cur_step_action], 1);
1974 }
1975 else if ((cur_source_string
1976 ? cur_source_string->next
1977 : cur_tpoint->source_strings))
1978 {
1979 if (cur_source_string)
1980 cur_source_string = cur_source_string->next;
1981 else
1982 cur_source_string = cur_tpoint->source_strings;
1983 response_source (packet, cur_tpoint, cur_source_string);
1984 }
1985 else
1986 {
1987 cur_tpoint = cur_tpoint->next;
1988 cur_action = cur_step_action = -1;
1989 cur_source_string = NULL;
1990 if (cur_tpoint)
1991 response_tracepoint (packet, cur_tpoint);
1992 else
1993 strcpy (packet, "l");
1994 }
1995 }
1996
1997 /* Compose a response that is an imitation of the syntax by which the
1998 trace state variable was originally downloaded. */
1999
2000 static void
2001 response_tsv (char *packet, struct trace_state_variable *tsv)
2002 {
2003 char *buf = (char *) "";
2004 int namelen;
2005
2006 if (tsv->name)
2007 {
2008 namelen = strlen (tsv->name);
2009 buf = alloca (namelen * 2 + 1);
2010 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
2011 }
2012
2013 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
2014 tsv->getter ? 1 : 0, buf);
2015 }
2016
2017 /* Return the first trace state variable definition, and initialize
2018 the state machine that will iterate through all the tsv bits. */
2019
2020 static void
2021 cmd_qtfv (char *packet)
2022 {
2023 trace_debug ("Returning first trace state variable definition");
2024
2025 cur_tsv = trace_state_variables;
2026
2027 if (cur_tsv)
2028 response_tsv (packet, cur_tsv);
2029 else
2030 strcpy (packet, "l");
2031 }
2032
2033 /* Return additional trace state variable definitions. */
2034
2035 static void
2036 cmd_qtsv (char *packet)
2037 {
2038 trace_debug ("Returning first trace state variable definition");
2039
2040 if (!cur_tpoint)
2041 {
2042 /* This case would normally never occur, but be prepared for
2043 GDB misbehavior. */
2044 strcpy (packet, "l");
2045 }
2046 else if (cur_tsv)
2047 {
2048 cur_tsv = cur_tsv->next;
2049 if (cur_tsv)
2050 response_tsv (packet, cur_tsv);
2051 else
2052 strcpy (packet, "l");
2053 }
2054 else
2055 strcpy (packet, "l");
2056 }
2057
2058 /* Respond to qTBuffer packet with a block of raw data from the trace
2059 buffer. GDB may ask for a lot, but we are allowed to reply with
2060 only as much as will fit within packet limits or whatever. */
2061
2062 static void
2063 cmd_qtbuffer (char *own_buf)
2064 {
2065 ULONGEST offset, num, tot;
2066 unsigned char *tbp;
2067 char *packet = own_buf;
2068
2069 packet += strlen ("qTBuffer:");
2070
2071 packet = unpack_varlen_hex (packet, &offset);
2072 ++packet; /* skip a comma */
2073 packet = unpack_varlen_hex (packet, &num);
2074
2075 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
2076 (int) num, pulongest (offset));
2077
2078 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
2079
2080 /* If we're right at the end, reply specially that we're done. */
2081 if (offset == tot)
2082 {
2083 strcpy (own_buf, "l");
2084 return;
2085 }
2086
2087 /* Object to any other out-of-bounds request. */
2088 if (offset > tot)
2089 {
2090 write_enn (own_buf);
2091 return;
2092 }
2093
2094 /* Compute the pointer corresponding to the given offset, accounting
2095 for wraparound. */
2096 tbp = trace_buffer_start + offset;
2097 if (tbp >= trace_buffer_wrap)
2098 tbp -= (trace_buffer_wrap - trace_buffer_lo);
2099
2100 /* Trim to the remaining bytes if we're close to the end. */
2101 if (num > tot - offset)
2102 num = tot - offset;
2103
2104 /* Trim to available packet size. */
2105 if (num >= (PBUFSIZ - 16) / 2 )
2106 num = (PBUFSIZ - 16) / 2;
2107
2108 convert_int_to_ascii (tbp, own_buf, num);
2109 own_buf[num] = '\0';
2110 }
2111
2112 static void
2113 cmd_bigqtbuffer (char *own_buf)
2114 {
2115 ULONGEST val;
2116 char *packet = own_buf;
2117
2118 packet += strlen ("QTBuffer:");
2119
2120 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
2121 {
2122 packet += strlen ("circular:");
2123 packet = unpack_varlen_hex (packet, &val);
2124 circular_trace_buffer = val;
2125 trace_debug ("Trace buffer is now %s",
2126 circular_trace_buffer ? "circular" : "linear");
2127 write_ok (own_buf);
2128 }
2129 else
2130 write_enn (own_buf);
2131 }
2132
2133 int
2134 handle_tracepoint_general_set (char *packet)
2135 {
2136 if (strcmp ("QTinit", packet) == 0)
2137 {
2138 cmd_qtinit (packet);
2139 return 1;
2140 }
2141 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
2142 {
2143 cmd_qtdp (packet);
2144 return 1;
2145 }
2146 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
2147 {
2148 cmd_qtdpsrc (packet);
2149 return 1;
2150 }
2151 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
2152 {
2153 cmd_qtdv (packet);
2154 return 1;
2155 }
2156 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
2157 {
2158 cmd_qtro (packet);
2159 return 1;
2160 }
2161 else if (strcmp ("QTStart", packet) == 0)
2162 {
2163 cmd_qtstart (packet);
2164 return 1;
2165 }
2166 else if (strcmp ("QTStop", packet) == 0)
2167 {
2168 cmd_qtstop (packet);
2169 return 1;
2170 }
2171 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
2172 {
2173 cmd_qtframe (packet);
2174 return 1;
2175 }
2176 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
2177 {
2178 cmd_bigqtbuffer (packet);
2179 return 1;
2180 }
2181
2182 return 0;
2183 }
2184
2185 int
2186 handle_tracepoint_query (char *packet)
2187 {
2188 if (strcmp ("qTStatus", packet) == 0)
2189 {
2190 cmd_qtstatus (packet);
2191 return 1;
2192 }
2193 else if (strcmp ("qTfP", packet) == 0)
2194 {
2195 cmd_qtfp (packet);
2196 return 1;
2197 }
2198 else if (strcmp ("qTsP", packet) == 0)
2199 {
2200 cmd_qtsp (packet);
2201 return 1;
2202 }
2203 else if (strcmp ("qTfV", packet) == 0)
2204 {
2205 cmd_qtfv (packet);
2206 return 1;
2207 }
2208 else if (strcmp ("qTsV", packet) == 0)
2209 {
2210 cmd_qtsv (packet);
2211 return 1;
2212 }
2213 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
2214 {
2215 cmd_qtv (packet);
2216 return 1;
2217 }
2218 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
2219 {
2220 cmd_qtbuffer (packet);
2221 return 1;
2222 }
2223
2224 return 0;
2225 }
2226
2227 /* Call this when thread TINFO has hit the tracepoint defined by
2228 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
2229 action. This adds a while-stepping collecting state item to the
2230 threads' collecting state list, so that we can keep track of
2231 multiple simultaneous while-stepping actions being collected by the
2232 same thread. This can happen in cases like:
2233
2234 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
2235 ff0002 INSN2
2236 ff0003 INSN3 <-- TP2, collect $regs
2237 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
2238 ff0005 INSN5
2239
2240 Notice that when instruction INSN5 is reached, the while-stepping
2241 actions of both TP1 and TP3 are still being collected, and that TP2
2242 had been collected meanwhile. The whole range of ff0001-ff0005
2243 should be single-stepped, due to at least TP1's while-stepping
2244 action covering the whole range. */
2245
2246 static void
2247 add_while_stepping_state (struct thread_info *tinfo,
2248 int tp_number, CORE_ADDR tp_address)
2249 {
2250 struct wstep_state *wstep;
2251
2252 wstep = xmalloc (sizeof (*wstep));
2253 wstep->next = tinfo->while_stepping;
2254
2255 wstep->tp_number = tp_number;
2256 wstep->tp_address = tp_address;
2257 wstep->current_step = 0;
2258
2259 tinfo->while_stepping = wstep;
2260 }
2261
2262 /* Release the while-stepping collecting state WSTEP. */
2263
2264 static void
2265 release_while_stepping_state (struct wstep_state *wstep)
2266 {
2267 free (wstep);
2268 }
2269
2270 /* Release all while-stepping collecting states currently associated
2271 with thread TINFO. */
2272
2273 void
2274 release_while_stepping_state_list (struct thread_info *tinfo)
2275 {
2276 struct wstep_state *head;
2277
2278 while (tinfo->while_stepping)
2279 {
2280 head = tinfo->while_stepping;
2281 tinfo->while_stepping = head->next;
2282 release_while_stepping_state (head);
2283 }
2284 }
2285
2286 /* If TINFO was handling a 'while-stepping' action, the step has
2287 finished, so collect any step data needed, and check if any more
2288 steps are required. Return true if the thread was indeed
2289 collecting tracepoint data, false otherwise. */
2290
2291 int
2292 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
2293 {
2294 struct tracepoint *tpoint;
2295 struct wstep_state *wstep;
2296 struct wstep_state **wstep_link;
2297 struct trap_tracepoint_ctx ctx;
2298
2299 /* Check if we were indeed collecting data for one of more
2300 tracepoints with a 'while-stepping' count. */
2301 if (tinfo->while_stepping == NULL)
2302 return 0;
2303
2304 if (!tracing)
2305 {
2306 /* We're not even tracing anymore. Stop this thread from
2307 collecting. */
2308 release_while_stepping_state_list (tinfo);
2309
2310 /* The thread had stopped due to a single-step request indeed
2311 explained by a tracepoint. */
2312 return 1;
2313 }
2314
2315 wstep = tinfo->while_stepping;
2316 wstep_link = &tinfo->while_stepping;
2317
2318 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
2319 target_pid_to_str (tinfo->entry.id),
2320 wstep->tp_number, paddress (wstep->tp_address));
2321
2322 ctx.regcache = get_thread_regcache (tinfo, 1);
2323
2324 while (wstep != NULL)
2325 {
2326 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
2327 if (tpoint == NULL)
2328 {
2329 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
2330 wstep->tp_number, paddress (wstep->tp_address),
2331 target_pid_to_str (tinfo->entry.id));
2332
2333 /* Unlink. */
2334 *wstep_link = wstep->next;
2335 release_while_stepping_state (wstep);
2336 continue;
2337 }
2338
2339 /* We've just finished one step. */
2340 ++wstep->current_step;
2341
2342 /* Collect data. */
2343 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
2344 stop_pc, tpoint, wstep->current_step);
2345
2346 if (wstep->current_step >= tpoint->step_count)
2347 {
2348 /* The requested numbers of steps have occurred. */
2349 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
2350 target_pid_to_str (tinfo->entry.id),
2351 wstep->tp_number, paddress (wstep->tp_address));
2352
2353 /* Unlink the wstep. */
2354 *wstep_link = wstep->next;
2355 release_while_stepping_state (wstep);
2356 wstep = *wstep_link;
2357
2358 /* Only check the hit count now, which ensure that we do all
2359 our stepping before stopping the run. */
2360 if (tpoint->pass_count > 0
2361 && tpoint->hit_count >= tpoint->pass_count
2362 && stopping_tracepoint == NULL)
2363 stopping_tracepoint = tpoint;
2364 }
2365 else
2366 {
2367 /* Keep single-stepping until the requested numbers of steps
2368 have occurred. */
2369 wstep_link = &wstep->next;
2370 wstep = *wstep_link;
2371 }
2372
2373 if (stopping_tracepoint
2374 || trace_buffer_is_full
2375 || expr_eval_result != expr_eval_no_error)
2376 {
2377 stop_tracing ();
2378 break;
2379 }
2380 }
2381
2382 return 1;
2383 }
2384
2385 /* Return true if TINFO just hit a tracepoint. Collect data if
2386 so. */
2387
2388 int
2389 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
2390 {
2391 struct tracepoint *tpoint;
2392 int ret = 0;
2393 struct trap_tracepoint_ctx ctx;
2394
2395 /* Not tracing, don't handle. */
2396 if (!tracing)
2397 return 0;
2398
2399 ctx.regcache = get_thread_regcache (tinfo, 1);
2400
2401 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2402 {
2403 if (tpoint->enabled && stop_pc == tpoint->address)
2404 {
2405 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
2406 target_pid_to_str (tinfo->entry.id),
2407 tpoint->number, paddress (tpoint->address));
2408
2409 /* Test the condition if present, and collect if true. */
2410 if (!tpoint->cond
2411 || (condition_true_at_tracepoint
2412 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
2413 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
2414 stop_pc, tpoint);
2415
2416 if (stopping_tracepoint
2417 || trace_buffer_is_full
2418 || expr_eval_result != expr_eval_no_error)
2419 {
2420 stop_tracing ();
2421 }
2422 /* If the tracepoint had a 'while-stepping' action, then set
2423 the thread to collect this tracepoint on the following
2424 single-steps. */
2425 else if (tpoint->step_count > 0)
2426 {
2427 add_while_stepping_state (tinfo,
2428 tpoint->number, tpoint->address);
2429 }
2430
2431 ret = 1;
2432 }
2433 }
2434
2435 return ret;
2436 }
2437
2438 /* Create a trace frame for the hit of the given tracepoint in the
2439 given thread. */
2440
2441 static void
2442 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
2443 struct tracepoint *tpoint)
2444 {
2445 struct traceframe *tframe;
2446 int acti;
2447
2448 /* Only count it as a hit when we actually collect data. */
2449 tpoint->hit_count++;
2450
2451 /* If we've exceeded a defined pass count, record the event for
2452 later, and finish the collection for this hit. This test is only
2453 for nonstepping tracepoints, stepping tracepoints test at the end
2454 of their while-stepping loop. */
2455 if (tpoint->pass_count > 0
2456 && tpoint->hit_count >= tpoint->pass_count
2457 && tpoint->step_count == 0
2458 && stopping_tracepoint == NULL)
2459 stopping_tracepoint = tpoint;
2460
2461 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
2462 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
2463
2464 tframe = add_traceframe (tpoint);
2465
2466 if (tframe)
2467 {
2468 for (acti = 0; acti < tpoint->numactions; ++acti)
2469 {
2470 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
2471 tpoint->number, paddress (tpoint->address),
2472 tpoint->actions_str[acti]);
2473
2474 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
2475 tpoint->actions[acti]);
2476 }
2477
2478 finish_traceframe (tframe);
2479 }
2480
2481 if (tframe == NULL && tracing)
2482 trace_buffer_is_full = 1;
2483 }
2484
2485 static void
2486 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
2487 CORE_ADDR stop_pc,
2488 struct tracepoint *tpoint, int current_step)
2489 {
2490 struct traceframe *tframe;
2491 int acti;
2492
2493 trace_debug ("Making new step traceframe for "
2494 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
2495 tpoint->number, paddress (tpoint->address),
2496 current_step, tpoint->step_count,
2497 tpoint->hit_count);
2498
2499 tframe = add_traceframe (tpoint);
2500
2501 if (tframe)
2502 {
2503 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
2504 {
2505 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
2506 tpoint->number, paddress (tpoint->address),
2507 tpoint->step_actions_str[acti]);
2508
2509 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
2510 tpoint->step_actions[acti]);
2511 }
2512
2513 finish_traceframe (tframe);
2514 }
2515
2516 if (tframe == NULL && tracing)
2517 trace_buffer_is_full = 1;
2518 }
2519
2520 static struct regcache *
2521 get_context_regcache (struct tracepoint_hit_ctx *ctx)
2522 {
2523 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
2524 struct regcache *regcache = tctx->regcache;
2525
2526 gdb_assert (regcache != NULL);
2527
2528 return regcache;
2529 }
2530
2531 static void
2532 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
2533 CORE_ADDR stop_pc,
2534 struct tracepoint *tpoint,
2535 struct traceframe *tframe,
2536 struct tracepoint_action *taction)
2537 {
2538 enum eval_result_type err;
2539
2540 switch (taction->type)
2541 {
2542 case 'M':
2543 {
2544 struct collect_memory_action *maction;
2545
2546 maction = (struct collect_memory_action *) taction;
2547
2548 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
2549 pulongest (maction->len),
2550 paddress (maction->addr), maction->basereg);
2551 /* (should use basereg) */
2552 agent_mem_read (tframe, NULL,
2553 (CORE_ADDR) maction->addr, maction->len);
2554 break;
2555 }
2556 case 'R':
2557 {
2558 struct collect_registers_action *raction;
2559
2560 unsigned char *regspace;
2561 struct regcache tregcache;
2562 struct regcache *context_regcache;
2563
2564 raction = (struct collect_registers_action *) taction;
2565
2566 trace_debug ("Want to collect registers");
2567
2568 /* Collect all registers for now. */
2569 regspace = add_traceframe_block (tframe,
2570 1 + register_cache_size ());
2571 if (regspace == NULL)
2572 {
2573 trace_debug ("Trace buffer block allocation failed, skipping");
2574 break;
2575 }
2576 /* Identify a register block. */
2577 *regspace = 'R';
2578
2579 context_regcache = get_context_regcache (ctx);
2580
2581 /* Wrap the regblock in a register cache (in the stack, we
2582 don't want to malloc here). */
2583 init_register_cache (&tregcache, regspace + 1);
2584
2585 /* Copy the register data to the regblock. */
2586 regcache_cpy (&tregcache, context_regcache);
2587
2588 /* On some platforms, trap-based tracepoints will have the PC
2589 pointing to the next instruction after the trap, but we
2590 don't want the user or GDB trying to guess whether the
2591 saved PC needs adjusting; so always record the adjusted
2592 stop_pc. Note that we can't use tpoint->address instead,
2593 since it will be wrong for while-stepping actions. */
2594 trace_debug ("Storing stop pc (0x%s) in regblock",
2595 paddress (tpoint->address));
2596
2597 /* This changes the regblock, not the thread's
2598 regcache. */
2599 regcache_write_pc (&tregcache, stop_pc);
2600 }
2601 break;
2602 case 'X':
2603 {
2604 struct eval_expr_action *eaction;
2605
2606 eaction = (struct eval_expr_action *) taction;
2607
2608 trace_debug ("Want to evaluate expression");
2609
2610 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
2611
2612 if (err != expr_eval_no_error)
2613 {
2614 record_tracepoint_error (tpoint, "action expression", err);
2615 return;
2616 }
2617 }
2618 break;
2619 default:
2620 trace_debug ("unknown trace action '%c', ignoring", taction->type);
2621 break;
2622 }
2623 }
2624
2625 static int
2626 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
2627 struct tracepoint *tpoint)
2628 {
2629 ULONGEST value = 0;
2630 enum eval_result_type err;
2631
2632 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
2633
2634 if (err != expr_eval_no_error)
2635 {
2636 record_tracepoint_error (tpoint, "condition", err);
2637 /* The error case must return false. */
2638 return 0;
2639 }
2640
2641 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
2642 tpoint->number, paddress (tpoint->address),
2643 pulongest (value));
2644 return (value ? 1 : 0);
2645 }
2646
2647 /* The packet form of an agent expression consists of an 'X', number
2648 of bytes in expression, a comma, and then the bytes. */
2649
2650 static struct agent_expr *
2651 parse_agent_expr (char **actparm)
2652 {
2653 char *act = *actparm;
2654 ULONGEST xlen;
2655 struct agent_expr *aexpr;
2656
2657 ++act; /* skip the X */
2658 act = unpack_varlen_hex (act, &xlen);
2659 ++act; /* skip a comma */
2660 aexpr = xmalloc (sizeof (struct agent_expr));
2661 aexpr->length = xlen;
2662 aexpr->bytes = xmalloc (xlen);
2663 convert_ascii_to_int (act, aexpr->bytes, xlen);
2664 *actparm = act + (xlen * 2);
2665 return aexpr;
2666 }
2667
2668 /* Convert the bytes of an agent expression back into hex digits, so
2669 they can be printed or uploaded. This allocates the buffer,
2670 callers should free when they are done with it. */
2671
2672 static char *
2673 unparse_agent_expr (struct agent_expr *aexpr)
2674 {
2675 char *rslt;
2676
2677 rslt = xmalloc (2 * aexpr->length + 1);
2678 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
2679 return rslt;
2680 }
2681
2682 /* The agent expression evaluator, as specified by the GDB docs. It
2683 returns 0 if everything went OK, and a nonzero error code
2684 otherwise. */
2685
2686 static enum eval_result_type
2687 eval_agent_expr (struct tracepoint_hit_ctx *ctx,
2688 struct traceframe *tframe,
2689 struct agent_expr *aexpr,
2690 ULONGEST *rslt)
2691 {
2692 int pc = 0;
2693 #define STACK_MAX 100
2694 ULONGEST stack[STACK_MAX], top;
2695 int sp = 0;
2696 unsigned char op;
2697 int arg;
2698
2699 /* This union is a convenient way to convert representations. For
2700 now, assume a standard architecture where the hardware integer
2701 types have 8, 16, 32, 64 bit types. A more robust solution would
2702 be to import stdint.h from gnulib. */
2703 union
2704 {
2705 union
2706 {
2707 unsigned char bytes[1];
2708 unsigned char val;
2709 } u8;
2710 union
2711 {
2712 unsigned char bytes[2];
2713 unsigned short val;
2714 } u16;
2715 union
2716 {
2717 unsigned char bytes[4];
2718 unsigned int val;
2719 } u32;
2720 union
2721 {
2722 unsigned char bytes[8];
2723 ULONGEST val;
2724 } u64;
2725 } cnv;
2726
2727 if (aexpr->length == 0)
2728 {
2729 trace_debug ("empty agent expression");
2730 return expr_eval_empty_expression;
2731 }
2732
2733 /* Cache the stack top in its own variable. Much of the time we can
2734 operate on this variable, rather than dinking with the stack. It
2735 needs to be copied to the stack when sp changes. */
2736 top = 0;
2737
2738 while (1)
2739 {
2740 op = aexpr->bytes[pc++];
2741
2742 trace_debug ("About to interpret byte 0x%x", op);
2743
2744 switch (op)
2745 {
2746 case gdb_agent_op_add:
2747 top += stack[--sp];
2748 break;
2749
2750 case gdb_agent_op_sub:
2751 top = stack[--sp] - top;
2752 break;
2753
2754 case gdb_agent_op_mul:
2755 top *= stack[--sp];
2756 break;
2757
2758 case gdb_agent_op_div_signed:
2759 if (top == 0)
2760 {
2761 trace_debug ("Attempted to divide by zero");
2762 return expr_eval_divide_by_zero;
2763 }
2764 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
2765 break;
2766
2767 case gdb_agent_op_div_unsigned:
2768 if (top == 0)
2769 {
2770 trace_debug ("Attempted to divide by zero");
2771 return expr_eval_divide_by_zero;
2772 }
2773 top = stack[--sp] / top;
2774 break;
2775
2776 case gdb_agent_op_rem_signed:
2777 if (top == 0)
2778 {
2779 trace_debug ("Attempted to divide by zero");
2780 return expr_eval_divide_by_zero;
2781 }
2782 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
2783 break;
2784
2785 case gdb_agent_op_rem_unsigned:
2786 if (top == 0)
2787 {
2788 trace_debug ("Attempted to divide by zero");
2789 return expr_eval_divide_by_zero;
2790 }
2791 top = stack[--sp] % top;
2792 break;
2793
2794 case gdb_agent_op_lsh:
2795 top = stack[--sp] << top;
2796 break;
2797
2798 case gdb_agent_op_rsh_signed:
2799 top = ((LONGEST) stack[--sp]) >> top;
2800 break;
2801
2802 case gdb_agent_op_rsh_unsigned:
2803 top = stack[--sp] >> top;
2804 break;
2805
2806 case gdb_agent_op_trace:
2807 agent_mem_read (tframe,
2808 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
2809 if (--sp >= 0)
2810 top = stack[sp];
2811 break;
2812
2813 case gdb_agent_op_trace_quick:
2814 arg = aexpr->bytes[pc++];
2815 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
2816 break;
2817
2818 case gdb_agent_op_log_not:
2819 top = !top;
2820 break;
2821
2822 case gdb_agent_op_bit_and:
2823 top &= stack[--sp];
2824 break;
2825
2826 case gdb_agent_op_bit_or:
2827 top |= stack[--sp];
2828 break;
2829
2830 case gdb_agent_op_bit_xor:
2831 top ^= stack[--sp];
2832 break;
2833
2834 case gdb_agent_op_bit_not:
2835 top = ~top;
2836 break;
2837
2838 case gdb_agent_op_equal:
2839 top = (stack[--sp] == top);
2840 break;
2841
2842 case gdb_agent_op_less_signed:
2843 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
2844 break;
2845
2846 case gdb_agent_op_less_unsigned:
2847 top = (stack[--sp] < top);
2848 break;
2849
2850 case gdb_agent_op_ext:
2851 arg = aexpr->bytes[pc++];
2852 if (arg < (sizeof (LONGEST) * 8))
2853 {
2854 LONGEST mask = 1 << (arg - 1);
2855 top &= ((LONGEST) 1 << arg) - 1;
2856 top = (top ^ mask) - mask;
2857 }
2858 break;
2859
2860 case gdb_agent_op_ref8:
2861 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
2862 top = cnv.u8.val;
2863 break;
2864
2865 case gdb_agent_op_ref16:
2866 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
2867 top = cnv.u16.val;
2868 break;
2869
2870 case gdb_agent_op_ref32:
2871 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
2872 top = cnv.u32.val;
2873 break;
2874
2875 case gdb_agent_op_ref64:
2876 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
2877 top = cnv.u64.val;
2878 break;
2879
2880 case gdb_agent_op_if_goto:
2881 if (top)
2882 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
2883 else
2884 pc += 2;
2885 if (--sp >= 0)
2886 top = stack[sp];
2887 break;
2888
2889 case gdb_agent_op_goto:
2890 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
2891 break;
2892
2893 case gdb_agent_op_const8:
2894 /* Flush the cached stack top. */
2895 stack[sp++] = top;
2896 top = aexpr->bytes[pc++];
2897 break;
2898
2899 case gdb_agent_op_const16:
2900 /* Flush the cached stack top. */
2901 stack[sp++] = top;
2902 top = aexpr->bytes[pc++];
2903 top = (top << 8) + aexpr->bytes[pc++];
2904 break;
2905
2906 case gdb_agent_op_const32:
2907 /* Flush the cached stack top. */
2908 stack[sp++] = top;
2909 top = aexpr->bytes[pc++];
2910 top = (top << 8) + aexpr->bytes[pc++];
2911 top = (top << 8) + aexpr->bytes[pc++];
2912 top = (top << 8) + aexpr->bytes[pc++];
2913 break;
2914
2915 case gdb_agent_op_const64:
2916 /* Flush the cached stack top. */
2917 stack[sp++] = top;
2918 top = aexpr->bytes[pc++];
2919 top = (top << 8) + aexpr->bytes[pc++];
2920 top = (top << 8) + aexpr->bytes[pc++];
2921 top = (top << 8) + aexpr->bytes[pc++];
2922 top = (top << 8) + aexpr->bytes[pc++];
2923 top = (top << 8) + aexpr->bytes[pc++];
2924 top = (top << 8) + aexpr->bytes[pc++];
2925 top = (top << 8) + aexpr->bytes[pc++];
2926 break;
2927
2928 case gdb_agent_op_reg:
2929 /* Flush the cached stack top. */
2930 stack[sp++] = top;
2931 arg = aexpr->bytes[pc++];
2932 arg = (arg << 8) + aexpr->bytes[pc++];
2933 {
2934 int regnum = arg;
2935 struct regcache *regcache;
2936
2937 regcache = get_context_regcache (ctx);
2938
2939 switch (register_size (regnum))
2940 {
2941 case 8:
2942 collect_register (regcache, regnum, cnv.u64.bytes);
2943 top = cnv.u64.val;
2944 break;
2945 case 4:
2946 collect_register (regcache, regnum, cnv.u32.bytes);
2947 top = cnv.u32.val;
2948 break;
2949 case 2:
2950 collect_register (regcache, regnum, cnv.u16.bytes);
2951 top = cnv.u16.val;
2952 break;
2953 case 1:
2954 collect_register (regcache, regnum, cnv.u8.bytes);
2955 top = cnv.u8.val;
2956 break;
2957 default:
2958 internal_error (__FILE__, __LINE__,
2959 "unhandled register size");
2960 }
2961 }
2962 break;
2963
2964 case gdb_agent_op_end:
2965 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
2966 sp, pulongest (top));
2967 if (rslt)
2968 {
2969 if (sp <= 0)
2970 {
2971 /* This should be an error */
2972 trace_debug ("Stack is empty, nothing to return");
2973 return expr_eval_empty_stack;
2974 }
2975 *rslt = top;
2976 }
2977 return expr_eval_no_error;
2978
2979 case gdb_agent_op_dup:
2980 stack[sp++] = top;
2981 break;
2982
2983 case gdb_agent_op_pop:
2984 if (--sp >= 0)
2985 top = stack[sp];
2986 break;
2987
2988 case gdb_agent_op_zero_ext:
2989 arg = aexpr->bytes[pc++];
2990 if (arg < (sizeof (LONGEST) * 8))
2991 top &= ((LONGEST) 1 << arg) - 1;
2992 break;
2993
2994 case gdb_agent_op_swap:
2995 /* Interchange top two stack elements, making sure top gets
2996 copied back onto stack. */
2997 stack[sp] = top;
2998 top = stack[sp - 1];
2999 stack[sp - 1] = stack[sp];
3000 break;
3001
3002 case gdb_agent_op_getv:
3003 /* Flush the cached stack top. */
3004 stack[sp++] = top;
3005 arg = aexpr->bytes[pc++];
3006 arg = (arg << 8) + aexpr->bytes[pc++];
3007 top = get_trace_state_variable_value (arg);
3008 break;
3009
3010 case gdb_agent_op_setv:
3011 arg = aexpr->bytes[pc++];
3012 arg = (arg << 8) + aexpr->bytes[pc++];
3013 set_trace_state_variable_value (arg, top);
3014 /* Note that we leave the value on the stack, for the
3015 benefit of later/enclosing expressions. */
3016 break;
3017
3018 case gdb_agent_op_tracev:
3019 arg = aexpr->bytes[pc++];
3020 arg = (arg << 8) + aexpr->bytes[pc++];
3021 agent_tsv_read (tframe, arg);
3022 break;
3023
3024 /* GDB never (currently) generates any of these ops. */
3025 case gdb_agent_op_float:
3026 case gdb_agent_op_ref_float:
3027 case gdb_agent_op_ref_double:
3028 case gdb_agent_op_ref_long_double:
3029 case gdb_agent_op_l_to_d:
3030 case gdb_agent_op_d_to_l:
3031 case gdb_agent_op_trace16:
3032 trace_debug ("Agent expression op 0x%x valid, but not handled",
3033 op);
3034 /* If ever GDB generates any of these, we don't have the
3035 option of ignoring. */
3036 return 1;
3037
3038 default:
3039 trace_debug ("Agent expression op 0x%x not recognized", op);
3040 /* Don't struggle on, things will just get worse. */
3041 return expr_eval_unrecognized_opcode;
3042 }
3043
3044 /* Check for stack badness. */
3045 if (sp >= (STACK_MAX - 1))
3046 {
3047 trace_debug ("Expression stack overflow");
3048 return expr_eval_stack_overflow;
3049 }
3050
3051 if (sp < 0)
3052 {
3053 trace_debug ("Expression stack underflow");
3054 return expr_eval_stack_underflow;
3055 }
3056
3057 trace_debug ("Op %s -> sp=%d, top=0x%s",
3058 gdb_agent_op_names[op], sp, pulongest (top));
3059 }
3060 }
3061
3062 /* Do memory copies for bytecodes. */
3063 /* Do the recording of memory blocks for actions and bytecodes. */
3064
3065 static int
3066 agent_mem_read (struct traceframe *tframe,
3067 unsigned char *to, CORE_ADDR from, ULONGEST len)
3068 {
3069 unsigned char *mspace;
3070 ULONGEST remaining = len;
3071 unsigned short blocklen;
3072
3073 /* If a 'to' buffer is specified, use it. */
3074 if (to != NULL)
3075 {
3076 read_inferior_memory (from, to, len);
3077 return 0;
3078 }
3079
3080 /* Otherwise, create a new memory block in the trace buffer. */
3081 while (remaining > 0)
3082 {
3083 size_t sp;
3084
3085 blocklen = (remaining > 65535 ? 65535 : remaining);
3086 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
3087 mspace = add_traceframe_block (tframe, sp);
3088 if (mspace == NULL)
3089 return 1;
3090 /* Identify block as a memory block. */
3091 *mspace = 'M';
3092 ++mspace;
3093 /* Record address and size. */
3094 memcpy (mspace, &from, sizeof (from));
3095 mspace += sizeof (from);
3096 memcpy (mspace, &blocklen, sizeof (blocklen));
3097 mspace += sizeof (blocklen);
3098 /* Record the memory block proper. */
3099 read_inferior_memory (from, mspace, blocklen);
3100 trace_debug ("%d bytes recorded", blocklen);
3101 remaining -= blocklen;
3102 from += blocklen;
3103 }
3104 return 0;
3105 }
3106
3107 /* Record the value of a trace state variable. */
3108
3109 static int
3110 agent_tsv_read (struct traceframe *tframe, int n)
3111 {
3112 unsigned char *vspace;
3113 LONGEST val;
3114
3115 vspace = add_traceframe_block (tframe,
3116 1 + sizeof (n) + sizeof (LONGEST));
3117 if (vspace == NULL)
3118 return 1;
3119 /* Identify block as a variable. */
3120 *vspace = 'V';
3121 /* Record variable's number and value. */
3122 memcpy (vspace + 1, &n, sizeof (n));
3123 val = get_trace_state_variable_value (n);
3124 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
3125 trace_debug ("Variable %d recorded", n);
3126 return 0;
3127 }
3128
3129 static unsigned char *
3130 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
3131 int tfnum, char type_wanted)
3132 {
3133 unsigned char *dataptr;
3134
3135 if (datasize == 0)
3136 {
3137 trace_debug ("traceframe %d has no data", tfnum);
3138 return NULL;
3139 }
3140
3141 /* Iterate through a traceframe's blocks, looking for a block of the
3142 requested type. */
3143 for (dataptr = database;
3144 dataptr < database + datasize;
3145 /* nothing */)
3146 {
3147 char blocktype;
3148 unsigned short mlen;
3149
3150 if (dataptr == trace_buffer_wrap)
3151 {
3152 /* Adjust to reflect wrapping part of the frame around to
3153 the beginning. */
3154 datasize = dataptr - database;
3155 dataptr = database = trace_buffer_lo;
3156 }
3157 blocktype = *dataptr++;
3158
3159 if (type_wanted == blocktype)
3160 return dataptr;
3161
3162 switch (blocktype)
3163 {
3164 case 'R':
3165 /* Skip over the registers block. */
3166 dataptr += register_cache_size ();
3167 break;
3168 case 'M':
3169 /* Skip over the memory block. */
3170 dataptr += sizeof (CORE_ADDR);
3171 memcpy (&mlen, dataptr, sizeof (mlen));
3172 dataptr += (sizeof (mlen) + mlen);
3173 break;
3174 case 'S':
3175 /* Skip over the static trace data block. */
3176 memcpy (&mlen, dataptr, sizeof (mlen));
3177 dataptr += (sizeof (mlen) + mlen);
3178 break;
3179 case 'V':
3180 /* Skip over the TSV block. */
3181 dataptr += (sizeof (int) + sizeof (LONGEST));
3182 break;
3183 default:
3184 trace_debug ("traceframe %d has unknown block type 0x%x",
3185 tfnum, blocktype);
3186 return NULL;
3187 }
3188 }
3189
3190 return NULL;
3191 }
3192
3193 static unsigned char *
3194 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
3195 {
3196 unsigned char *regblock;
3197
3198 regblock = traceframe_find_block_type (tframe->data,
3199 tframe->data_size,
3200 tfnum, 'R');
3201
3202 if (regblock == NULL)
3203 trace_debug ("traceframe %d has no register data", tfnum);
3204
3205 return regblock;
3206 }
3207
3208 /* Get registers from a traceframe. */
3209
3210 int
3211 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
3212 {
3213 unsigned char *dataptr;
3214 struct tracepoint *tpoint;
3215 struct traceframe *tframe;
3216
3217 tframe = find_traceframe (tfnum);
3218
3219 if (tframe == NULL)
3220 {
3221 trace_debug ("traceframe %d not found", tfnum);
3222 return 1;
3223 }
3224
3225 dataptr = traceframe_find_regblock (tframe, tfnum);
3226 if (dataptr == NULL)
3227 {
3228 /* We don't like making up numbers, but GDB has all manner of
3229 troubles when the target says there are no registers. */
3230 supply_regblock (regcache, NULL);
3231
3232 /* We can generally guess at a PC, although this will be
3233 misleading for while-stepping frames and multi-location
3234 tracepoints. */
3235 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
3236 if (tpoint != NULL)
3237 regcache_write_pc (regcache, tpoint->address);
3238 }
3239 else
3240 supply_regblock (regcache, dataptr);
3241
3242 return 0;
3243 }
3244
3245 static CORE_ADDR
3246 traceframe_get_pc (struct traceframe *tframe)
3247 {
3248 struct regcache regcache;
3249 unsigned char *dataptr;
3250
3251 dataptr = traceframe_find_regblock (tframe, -1);
3252 if (dataptr == NULL)
3253 return 0;
3254
3255 init_register_cache (&regcache, dataptr);
3256 return regcache_read_pc (&regcache);
3257 }
3258
3259 /* Read a requested block of memory from a trace frame. */
3260
3261 int
3262 traceframe_read_mem (int tfnum, CORE_ADDR addr,
3263 unsigned char *buf, ULONGEST length,
3264 ULONGEST *nbytes)
3265 {
3266 struct traceframe *tframe;
3267 unsigned char *database, *dataptr;
3268 unsigned int datasize;
3269 CORE_ADDR maddr;
3270 unsigned short mlen;
3271
3272 trace_debug ("traceframe_read_mem");
3273
3274 tframe = find_traceframe (tfnum);
3275
3276 if (!tframe)
3277 {
3278 trace_debug ("traceframe %d not found", tfnum);
3279 return 1;
3280 }
3281
3282 datasize = tframe->data_size;
3283 database = dataptr = &tframe->data[0];
3284
3285 /* Iterate through a traceframe's blocks, looking for memory. */
3286 while ((dataptr = traceframe_find_block_type (dataptr,
3287 datasize - (dataptr - database),
3288 tfnum, 'M')) != NULL)
3289 {
3290 memcpy (&maddr, dataptr, sizeof (maddr));
3291 dataptr += sizeof (maddr);
3292 memcpy (&mlen, dataptr, sizeof (mlen));
3293 dataptr += sizeof (mlen);
3294 trace_debug ("traceframe %d has %d bytes at %s",
3295 tfnum, mlen, paddress (maddr));
3296
3297 /* Check that requested data is in bounds. */
3298 if (maddr <= addr && (addr + length) <= (maddr + mlen))
3299 {
3300 /* Block includes the requested range, copy it out. */
3301 memcpy (buf, dataptr + (addr - maddr), length);
3302 *nbytes = length;
3303 return 0;
3304 }
3305
3306 /* Skip over this block. */
3307 dataptr += mlen;
3308 }
3309
3310 trace_debug ("traceframe %d has no memory data for the desired region",
3311 tfnum);
3312
3313 *nbytes = 0;
3314 return 0;
3315 }
3316
3317 static int
3318 traceframe_read_tsv (int tsvnum, LONGEST *val)
3319 {
3320 int tfnum;
3321 struct traceframe *tframe;
3322 unsigned char *database, *dataptr;
3323 unsigned int datasize;
3324 int vnum;
3325
3326 trace_debug ("traceframe_read_tsv");
3327
3328 tfnum = current_traceframe;
3329
3330 if (tfnum < 0)
3331 {
3332 trace_debug ("no current traceframe");
3333 return 1;
3334 }
3335
3336 tframe = find_traceframe (tfnum);
3337
3338 if (tframe == NULL)
3339 {
3340 trace_debug ("traceframe %d not found", tfnum);
3341 return 1;
3342 }
3343
3344 datasize = tframe->data_size;
3345 database = dataptr = &tframe->data[0];
3346
3347 /* Iterate through a traceframe's blocks, looking for the tsv. */
3348 while ((dataptr = traceframe_find_block_type (dataptr,
3349 datasize - (dataptr - database),
3350 tfnum, 'V')) != NULL)
3351 {
3352 memcpy (&vnum, dataptr, sizeof (vnum));
3353 dataptr += sizeof (vnum);
3354
3355 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
3356
3357 /* Check that this is the variable we want. */
3358 if (tsvnum == vnum)
3359 {
3360 memcpy (val, dataptr, sizeof (*val));
3361 return 0;
3362 }
3363
3364 /* Skip over this block. */
3365 dataptr += sizeof (LONGEST);
3366 }
3367
3368 trace_debug ("traceframe %d has no data for variable %d",
3369 tfnum, tsvnum);
3370 return 1;
3371 }
3372
3373 static LONGEST
3374 tsv_get_timestamp (void)
3375 {
3376 struct timeval tv;
3377
3378 if (gettimeofday (&tv, 0) != 0)
3379 return -1;
3380 else
3381 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
3382 }
3383
3384 void
3385 initialize_tracepoint (void)
3386 {
3387 /* There currently no way to change the buffer size. */
3388 const int sizeOfBuffer = 5 * 1024 * 1024;
3389 unsigned char *buf = xmalloc (sizeOfBuffer);
3390 init_trace_buffer (buf, sizeOfBuffer);
3391
3392 /* Wire trace state variable 1 to be the timestamp. This will be
3393 uploaded to GDB upon connection and become one of its trace state
3394 variables. (In case you're wondering, if GDB already has a trace
3395 variable numbered 1, it will be renumbered.) */
3396 create_trace_state_variable (1);
3397 set_trace_state_variable_name (1, "trace_timestamp");
3398 set_trace_state_variable_getter (1, tsv_get_timestamp);
3399 }