1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* Trace file TFILE format support in GDB.
Copyright (C) 1997-2014 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "tracefile.h"
#include "readline/tilde.h"
#include "filestuff.h"
#include "rsp-low.h" /* bin2hex */
/* TFILE trace writer. */
struct tfile_trace_file_writer
{
struct trace_file_writer base;
/* File pointer to tfile trace file. */
FILE *fp;
/* Path name of the tfile trace file. */
char *pathname;
};
/* This is the implementation of trace_file_write_ops method
target_save. We just call the generic target
target_save_trace_data to do target-side saving. */
static int
tfile_target_save (struct trace_file_writer *self,
const char *filename)
{
int err = target_save_trace_data (filename);
return (err >= 0);
}
/* This is the implementation of trace_file_write_ops method
dtor. */
static void
tfile_dtor (struct trace_file_writer *self)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
xfree (writer->pathname);
if (writer->fp != NULL)
fclose (writer->fp);
}
/* This is the implementation of trace_file_write_ops method
start. It creates the trace file FILENAME and registers some
cleanups. */
static void
tfile_start (struct trace_file_writer *self, const char *filename)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
writer->pathname = tilde_expand (filename);
writer->fp = gdb_fopen_cloexec (writer->pathname, "wb");
if (writer->fp == NULL)
error (_("Unable to open file '%s' for saving trace data (%s)"),
writer->pathname, safe_strerror (errno));
}
/* This is the implementation of trace_file_write_ops method
write_header. Write the TFILE header. */
static void
tfile_write_header (struct trace_file_writer *self)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
int written;
/* Write a file header, with a high-bit-set char to indicate a
binary file, plus a hint as what this file is, and a version
number in case of future needs. */
written = fwrite ("\x7fTRACE0\n", 8, 1, writer->fp);
if (written < 1)
perror_with_name (writer->pathname);
}
/* This is the implementation of trace_file_write_ops method
write_regblock_type. Write the size of register block. */
static void
tfile_write_regblock_type (struct trace_file_writer *self, int size)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
fprintf (writer->fp, "R %x\n", size);
}
/* This is the implementation of trace_file_write_ops method
write_status. */
static void
tfile_write_status (struct trace_file_writer *self,
struct trace_status *ts)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
fprintf (writer->fp, "status %c;%s",
(ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
if (ts->stop_reason == tracepoint_error
|| ts->stop_reason == tstop_command)
{
char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
bin2hex ((gdb_byte *) ts->stop_desc, buf, strlen (ts->stop_desc));
fprintf (writer->fp, ":%s", buf);
}
fprintf (writer->fp, ":%x", ts->stopping_tracepoint);
if (ts->traceframe_count >= 0)
fprintf (writer->fp, ";tframes:%x", ts->traceframe_count);
if (ts->traceframes_created >= 0)
fprintf (writer->fp, ";tcreated:%x", ts->traceframes_created);
if (ts->buffer_free >= 0)
fprintf (writer->fp, ";tfree:%x", ts->buffer_free);
if (ts->buffer_size >= 0)
fprintf (writer->fp, ";tsize:%x", ts->buffer_size);
if (ts->disconnected_tracing)
fprintf (writer->fp, ";disconn:%x", ts->disconnected_tracing);
if (ts->circular_buffer)
fprintf (writer->fp, ";circular:%x", ts->circular_buffer);
if (ts->start_time)
{
fprintf (writer->fp, ";starttime:%s",
phex_nz (ts->start_time, sizeof (ts->start_time)));
}
if (ts->stop_time)
{
fprintf (writer->fp, ";stoptime:%s",
phex_nz (ts->stop_time, sizeof (ts->stop_time)));
}
if (ts->notes != NULL)
{
char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
bin2hex ((gdb_byte *) ts->notes, buf, strlen (ts->notes));
fprintf (writer->fp, ";notes:%s", buf);
}
if (ts->user_name != NULL)
{
char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
bin2hex ((gdb_byte *) ts->user_name, buf, strlen (ts->user_name));
fprintf (writer->fp, ";username:%s", buf);
}
fprintf (writer->fp, "\n");
}
/* This is the implementation of trace_file_write_ops method
write_uploaded_tsv. */
static void
tfile_write_uploaded_tsv (struct trace_file_writer *self,
struct uploaded_tsv *utsv)
{
char *buf = "";
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
if (utsv->name)
{
buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
bin2hex ((gdb_byte *) (utsv->name), buf, strlen (utsv->name));
}
fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
utsv->number, phex_nz (utsv->initial_value, 8),
utsv->builtin, buf);
if (utsv->name)
xfree (buf);
}
#define MAX_TRACE_UPLOAD 2000
/* This is the implementation of trace_file_write_ops method
write_uploaded_tp. */
static void
tfile_write_uploaded_tp (struct trace_file_writer *self,
struct uploaded_tp *utp)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
int a;
char *act;
char buf[MAX_TRACE_UPLOAD];
fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
(utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
if (utp->type == bp_fast_tracepoint)
fprintf (writer->fp, ":F%x", utp->orig_size);
if (utp->cond)
fprintf (writer->fp,
":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
utp->cond);
fprintf (writer->fp, "\n");
for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
fprintf (writer->fp, "tp A%x:%s:%s\n",
utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
fprintf (writer->fp, "tp S%x:%s:%s\n",
utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
if (utp->at_string)
{
encode_source_string (utp->number, utp->addr,
"at", utp->at_string, buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}
if (utp->cond_string)
{
encode_source_string (utp->number, utp->addr,
"cond", utp->cond_string,
buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}
for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
{
encode_source_string (utp->number, utp->addr, "cmd", act,
buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}
fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
utp->hit_count,
phex_nz (utp->traceframe_usage,
sizeof (utp->traceframe_usage)));
}
/* This is the implementation of trace_file_write_ops method
write_definition_end. */
static void
tfile_write_definition_end (struct trace_file_writer *self)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
fprintf (writer->fp, "\n");
}
/* This is the implementation of trace_file_write_ops method
write_raw_data. */
static void
tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
LONGEST len)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
if (fwrite (buf, len, 1, writer->fp) < 1)
perror_with_name (writer->pathname);
}
/* This is the implementation of trace_file_write_ops method
end. */
static void
tfile_end (struct trace_file_writer *self)
{
struct tfile_trace_file_writer *writer
= (struct tfile_trace_file_writer *) self;
uint32_t gotten = 0;
/* Mark the end of trace data. */
if (fwrite (&gotten, 4, 1, writer->fp) < 1)
perror_with_name (writer->pathname);
}
/* Operations to write trace buffers into TFILE format. */
static const struct trace_file_write_ops tfile_write_ops =
{
tfile_dtor,
tfile_target_save,
tfile_start,
tfile_write_header,
tfile_write_regblock_type,
tfile_write_status,
tfile_write_uploaded_tsv,
tfile_write_uploaded_tp,
tfile_write_definition_end,
tfile_write_raw_data,
NULL,
tfile_end,
};
/* Return a trace writer for TFILE format. */
struct trace_file_writer *
tfile_trace_file_writer_new (void)
{
struct tfile_trace_file_writer *writer
= xmalloc (sizeof (struct tfile_trace_file_writer));
writer->base.ops = &tfile_write_ops;
writer->fp = NULL;
writer->pathname = NULL;
return (struct trace_file_writer *) writer;
}
|