Commit | Line | Data |
---|---|---|
00bf0b85 SS |
1 | /* This program does two things; it generates valid trace files, and |
2 | it can also be traced so as to test trace file creation from | |
3 | GDB. */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <string.h> | |
7 | #include <fcntl.h> | |
8 | #include <sys/stat.h> | |
9 | ||
10 | char spbuf[200]; | |
11 | ||
12 | char trbuf[1000]; | |
13 | char *trptr; | |
14 | char *tfsizeptr; | |
15 | ||
16 | int testglob = 31415; | |
17 | ||
18 | int | |
19 | start_trace_file (char *filename) | |
20 | { | |
21 | int fd; | |
22 | ||
23 | fd = open (filename, O_WRONLY|O_CREAT|O_APPEND, | |
24 | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); | |
25 | ||
26 | if (fd < 0) | |
27 | return fd; | |
28 | ||
29 | /* Write a file header, with a high-bit-set char to indicate a | |
30 | binary file, plus a hint as what this file is, and a version | |
31 | number in case of future needs. */ | |
32 | write (fd, "\x7fTRACE0\n", 8); | |
33 | ||
34 | return fd; | |
35 | } | |
36 | ||
37 | void | |
38 | finish_trace_file (int fd) | |
39 | { | |
40 | close (fd); | |
41 | } | |
42 | ||
43 | void | |
6c28cbf2 | 44 | write_basic_trace_file (void) |
00bf0b85 | 45 | { |
6c28cbf2 SS |
46 | int fd, int_x; |
47 | short short_x; | |
48 | long long ll_x; | |
00bf0b85 SS |
49 | |
50 | fd = start_trace_file ("basic.tf"); | |
51 | ||
52 | /* The next part of the file consists of newline-separated lines | |
53 | defining status, tracepoints, etc. The section is terminated by | |
54 | an empty line. */ | |
55 | ||
56 | /* Dump the size of the R (register) blocks in traceframes. */ | |
57 | snprintf (spbuf, sizeof spbuf, "R %x\n", 500 /* FIXME get from arch */); | |
58 | write (fd, spbuf, strlen (spbuf)); | |
59 | ||
60 | /* Dump trace status, in the general form of the qTstatus reply. */ | |
61 | snprintf (spbuf, sizeof spbuf, "status 0;tstop:0;tframes:1;tcreated:1;tfree:100;tsize:1000\n"); | |
62 | write (fd, spbuf, strlen (spbuf)); | |
63 | ||
64 | /* Dump tracepoint definitions, in syntax similar to that used | |
65 | for reconnection uploads. */ | |
6c28cbf2 | 66 | /* FIXME need a portable way to print function address in hex */ |
00bf0b85 SS |
67 | snprintf (spbuf, sizeof spbuf, "tp T1:%lx:E:0:0\n", |
68 | (long) &write_basic_trace_file); | |
69 | write (fd, spbuf, strlen (spbuf)); | |
70 | /* (Note that we would only need actions defined if we wanted to | |
71 | test tdump.) */ | |
72 | ||
73 | /* Empty line marks the end of the definition section. */ | |
74 | write (fd, "\n", 1); | |
75 | ||
76 | /* Make up a simulated trace buffer. */ | |
6c28cbf2 SS |
77 | /* (Encapsulate better if we're going to do lots of this; note that |
78 | buffer endianness is the target program's enddianness.) */ | |
00bf0b85 | 79 | trptr = trbuf; |
6c28cbf2 SS |
80 | short_x = 1; |
81 | memcpy (trptr, &short_x, 2); | |
82 | trptr += 2; | |
00bf0b85 | 83 | tfsizeptr = trptr; |
6c28cbf2 | 84 | trptr += 4; |
00bf0b85 SS |
85 | *((char *) trptr) = 'M'; |
86 | trptr += 1; | |
6c28cbf2 SS |
87 | ll_x = (long) &testglob; |
88 | memcpy (trptr, &ll_x, sizeof (long long)); | |
00bf0b85 | 89 | trptr += sizeof (long long); |
6c28cbf2 SS |
90 | short_x = sizeof (testglob); |
91 | memcpy (trptr, &short_x, 2); | |
92 | trptr += 2; | |
93 | memcpy (trptr, &testglob, sizeof (testglob)); | |
00bf0b85 SS |
94 | trptr += sizeof (testglob); |
95 | /* Go back and patch in the frame size. */ | |
6c28cbf2 SS |
96 | int_x = trptr - tfsizeptr - sizeof (int); |
97 | memcpy (tfsizeptr, &int_x, 4); | |
98 | ||
99 | /* Write end of tracebuffer marker. */ | |
100 | memset (trptr, 0, 6); | |
101 | trptr += 6; | |
102 | ||
103 | write (fd, trbuf, trptr - trbuf); | |
104 | ||
105 | finish_trace_file (fd); | |
106 | } | |
107 | ||
610197fd PA |
108 | /* Convert number NIB to a hex digit. */ |
109 | ||
110 | static int | |
111 | tohex (int nib) | |
112 | { | |
113 | if (nib < 10) | |
114 | return '0' + nib; | |
115 | else | |
116 | return 'a' + nib - 10; | |
117 | } | |
118 | ||
119 | int | |
120 | bin2hex (const char *bin, char *hex, int count) | |
121 | { | |
122 | int i; | |
123 | ||
124 | for (i = 0; i < count; i++) | |
125 | { | |
126 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
127 | *hex++ = tohex (*bin++ & 0xf); | |
128 | } | |
129 | *hex = 0; | |
130 | return i; | |
131 | } | |
132 | ||
6c28cbf2 SS |
133 | void |
134 | write_error_trace_file (void) | |
135 | { | |
136 | int fd; | |
610197fd PA |
137 | const char made_up[] = "made-up error"; |
138 | int len = sizeof (made_up) - 1; | |
139 | char *hex = alloca (len * 2 + 1); | |
6c28cbf2 SS |
140 | |
141 | fd = start_trace_file ("error.tf"); | |
142 | ||
143 | /* The next part of the file consists of newline-separated lines | |
144 | defining status, tracepoints, etc. The section is terminated by | |
145 | an empty line. */ | |
146 | ||
147 | /* Dump the size of the R (register) blocks in traceframes. */ | |
148 | snprintf (spbuf, sizeof spbuf, "R %x\n", 500 /* FIXME get from arch */); | |
149 | write (fd, spbuf, strlen (spbuf)); | |
150 | ||
610197fd PA |
151 | bin2hex (made_up, hex, len); |
152 | ||
6c28cbf2 | 153 | /* Dump trace status, in the general form of the qTstatus reply. */ |
610197fd PA |
154 | snprintf (spbuf, sizeof spbuf, |
155 | "status 0;" | |
156 | "terror:%s:1;" | |
157 | "tframes:0;tcreated:0;tfree:100;tsize:1000\n", | |
158 | hex); | |
6c28cbf2 SS |
159 | write (fd, spbuf, strlen (spbuf)); |
160 | ||
161 | /* Dump tracepoint definitions, in syntax similar to that used | |
162 | for reconnection uploads. */ | |
163 | /* FIXME need a portable way to print function address in hex */ | |
164 | snprintf (spbuf, sizeof spbuf, "tp T1:%lx:E:0:0\n", | |
165 | (long) &write_basic_trace_file); | |
166 | write (fd, spbuf, strlen (spbuf)); | |
167 | /* (Note that we would only need actions defined if we wanted to | |
168 | test tdump.) */ | |
169 | ||
170 | /* Empty line marks the end of the definition section. */ | |
171 | write (fd, "\n", 1); | |
172 | ||
173 | trptr = trbuf; | |
00bf0b85 SS |
174 | |
175 | /* Write end of tracebuffer marker. */ | |
6c28cbf2 SS |
176 | memset (trptr, 0, 6); |
177 | trptr += 6; | |
00bf0b85 SS |
178 | |
179 | write (fd, trbuf, trptr - trbuf); | |
180 | ||
181 | finish_trace_file (fd); | |
182 | } | |
183 | ||
184 | void | |
185 | done_making_trace_files (void) | |
186 | { | |
187 | } | |
188 | ||
189 | int | |
190 | main (int argc, char **argv, char **envp) | |
191 | { | |
192 | write_basic_trace_file (); | |
193 | ||
6c28cbf2 SS |
194 | write_error_trace_file (); |
195 | ||
00bf0b85 SS |
196 | done_making_trace_files (); |
197 | ||
198 | return 0; | |
199 | } | |
200 |