Debian Codegraph @

src/lib/rt-utils.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // SPDX-License-Identifier: GPL-2.0-or-latercxx . . $ `<file>/src/lib/rt-utils.c`/
2 /*
3 * Copyright (C) 2009 Carsten Emde <[email protected]>
4 * Copyright (C) 2010 Clark Williams <[email protected]>
5 * Copyright (C) 2015 John Kacur <[email protected]>
6 *
7 * based on functions from cyclictest that has
8 * (C) 2008-2009 Clark Williams <[email protected]>
9 * (C) 2005-2007 Thomas Gleixner <[email protected]>
10 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sched.h>
15 #include <stdarg.h>
16 #include <errno.h>
17 #include <ctype.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <sys/syscall.h> /* For SYS_gettid definitions */
23 #include <sys/utsname.h>
24 #include <time.h>
25 #include <sys/time.h>
26
27 #include "rt-utils.h"cxx . . $ `<file>/src/include/rt-utils.h`/
28 #include "rt-sched.h"cxx . . $ `<file>/src/include/rt-sched.h`/
29 #include "rt-error.h"cxx . . $ `<file>/src/include/rt-error.h`/
30
31 #define TRACEBUFSIZcxx . . $ `src/lib/rt-utils.c:31:10`! 1024
32 #define MAX_COMMAND_LINEcxx . . $ `src/lib/rt-utils.c:32:10`! 4096
33 #define MAX_TS_SIZEcxx . . $ `src/lib/rt-utils.c:33:10`! 64
34
35 static char tracefs_prefixcxx . . $ tracefs_prefix.[MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!];
36 static int trace_fdcxx . . $ trace_fd. = -1;
37 static int tracemark_fdcxx . . $ tracemark_fd. = -1;
38 static __thread char tracebufcxx . . $ tracebuf.[TRACEBUFSIZcxx . . $ `src/lib/rt-utils.c:31:10`!];
39 static char test_cmdlinecxx . . $ test_cmdline.[MAX_COMMAND_LINEcxx . . $ `src/lib/rt-utils.c:32:10`!];
40 static char ts_startcxx . . $ ts_start.[MAX_TS_SIZEcxx . . $ `src/lib/rt-utils.c:33:10`!];
41
42 /*
43 * Finds the tracing directory
44 */
45 char *get_tracefs_prefixcxx . . $ get_tracefs_prefix(41208ce6ca65dd58).(void)
46 {
47 char type[100];
48 FILEcxx . . $ FILE# *fp;
49 int found = 0;
50 struct statcxx . . $ stat# s;
51
52 if (tracefs_prefixcxx . . $ tracefs_prefix.[0] != '\0')
53 goto out;
54
55 /* look in the "standard" mount point first */
56 if ((statcxx . . $ stat(a9cd1e23e96f423c).("/sys/kernel/tracing", &s) == 0) && S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(s.st_mode)) {
57 strcpycxx . . $ strcpy(b7b3a83273496486).(tracefs_prefixcxx . . $ tracefs_prefix., "/sys/kernel/tracing/");
58 goto out;
59 }
60
61 /* oh well, parse /proc/mounts and see if it's there */
62 if ((fp = fopencxx . . $ fopen(82ec0787cd37e0a3).("/proc/mounts", "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
63 goto out;
64
65 while (fscanfcxx . . $ fscanf(bc216f8e5b1ebeac).(fp, "%*s %"
66 STRcxx . . $ `src/include/rt-utils.h:8:9`!(MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!)
67 "s %99s %*s %*d %*d\n",
68 tracefs_prefixcxx . . $ tracefs_prefix., type) == 2) {
69 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(type, "tracefs") == 0) {
70 found = 1;
71 break;
72 }
73 }
74 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
75
76 if (!found) {
77 tracefs_prefixcxx . . $ tracefs_prefix.[0] = '\0';
78 goto out;
79 }
80 out:
81 return tracefs_prefixcxx . . $ tracefs_prefix.;
82 }
83
84 static char **tracer_listcxx . . $ tracer_list.;
85 static char *tracer_buffercxx . . $ tracer_buffer.;
86 static int num_tracerscxx . . $ num_tracers.;
87 #define CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`! 1024
88
89 /*
90 * return a list of the tracers configured into the running kernel
91 */
92
93 int get_tracerscxx . . $ get_tracers(c293f0ab5a5bee95).(char ***list)
94 {
95 int ret;
96 FILEcxx . . $ FILE# *fp;
97 char buffer[CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`!];
98 char *prefix = get_tracefs_prefixcxx . . $ get_tracefs_prefix(41208ce6ca65dd58).();
99 char *tmpbuf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
100 char *ptr;
101 int tmpsz = 0;
102
103 /* if we've already parse it, return what we have */
104 if (tracer_listcxx . . $ tracer_list.) {
105 *list = tracer_listcxx . . $ tracer_list.;
106 return num_tracerscxx . . $ num_tracers.;
107 }
108
109 /* open the tracing file available_tracers */
110 sprintfcxx . . $ sprintf(26c8642d26409d6d).(buffer, "%savailable_tracers", prefix);
111 if ((fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(buffer, "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
112 fatalcxx . . $ fatal(5313ecf19fd40d4d).("Can't open %s for reading\n", buffer);
113
114 /* allocate initial buffer */
115 ptr = tmpbuf = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`!);
116 if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
117 fatalcxx . . $ fatal(5313ecf19fd40d4d).("error allocating initial space for tracer list\n");
118
119 /* read in the list of available tracers */
120 while ((ret = freadcxx . . $ fread(738fc8ac1d711e58).(buffer, sizeof(char), CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`!, fp))) {
121 if ((ptr+ret+1) > (tmpbuf+tmpsz)) {
122 tmpbuf = realloccxx . . $ realloc(417ab160f6baadc6).(tmpbuf, tmpsz + CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`!);
123 if (tmpbuf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
124 fatalcxx . . $ fatal(5313ecf19fd40d4d).("error allocating space for list of valid tracers\n");
125 tmpsz += CHUNKSZcxx . . $ `src/lib/rt-utils.c:87:9`!;
126 }
127 strncpycxx . . $ strncpy(255158a415b673b0).(ptr, buffer, ret);
128 ptr += ret;
129 }
130 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
131 if (tmpsz == 0)
132 fatalcxx . . $ fatal(5313ecf19fd40d4d).("error reading available tracers\n");
133
134 tracer_buffercxx . . $ tracer_buffer. = tmpbuf;
135
136 /* get a buffer for the pointers to tracers */
137 if (!(tracer_listcxx . . $ tracer_list. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(char *))))
138 fatalcxx . . $ fatal(5313ecf19fd40d4d).("error allocatinging tracer list buffer\n");
139
140 /* parse the buffer */
141 ptr = strtokcxx . . $ strtok(b7b3a83273496486).(tmpbuf, " \t\n\r");
142 do {
143 tracer_listcxx . . $ tracer_list.[num_tracerscxx . . $ num_tracers.++] = ptr;
144 tracer_listcxx . . $ tracer_list. = realloccxx . . $ realloc(417ab160f6baadc6).(tracer_listcxx . . $ tracer_list., sizeof(char*)*(num_tracerscxx . . $ num_tracers.+1));
145 tracer_listcxx . . $ tracer_list.[num_tracerscxx . . $ num_tracers.] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
146 } while ((ptr = strtokcxx . . $ strtok(b7b3a83273496486).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, " \t\n\r")) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
147
148 /* return the list and number of tracers */
149 *list = tracer_listcxx . . $ tracer_list.;
150 return num_tracerscxx . . $ num_tracers.;
151 }
152
153
154 /*
155 * return zero if tracername is not a valid tracer, non-zero if it is
156 */
157
158 int valid_tracercxx . . $ valid_tracer(1ef8e6ec083aabb9).(char *tracername)
159 {
160 char **list;
161 int ntracers;
162 int i;
163
164 ntracers = get_tracerscxx . . $ get_tracers(c293f0ab5a5bee95).(&list);
165 if (ntracers == 0 || tracername == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
166 return 0;
167 for (i = 0; i < ntracers; i++)
168 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(list[i], tracername, strlencxx . . $ strlen(751346f8406fd082).(list[i])) == 0)
169 return 1;
170 return 0;
171 }
172
173 /*
174 * enable event tracepoint
175 */
176 int seteventcxx . . $ setevent(e99f026884c896d5).(char *event, char *val)
177 {
178 char *prefix = get_tracefs_prefixcxx . . $ get_tracefs_prefix(41208ce6ca65dd58).();
179 char buffer[MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!];
180 int fd;
181 int ret;
182
183 sprintfcxx . . $ sprintf(26c8642d26409d6d).(buffer, "%s%s", prefix, event);
184 if ((fd = opencxx . . $ open(e156e3aa1e5c4ad1).(buffer, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!)) < 0) {
185 warncxx . . $ warn(d5d9ff45db7b5d36).("unable to open %s\n", buffer);
186 return -1;
187 }
188 if ((ret = writecxx . . $ write(41184a59d38b4f65).(fd, val, strlencxx . . $ strlen(751346f8406fd082).(val))) < 0) {
189 warncxx . . $ warn(d5d9ff45db7b5d36).("unable to write %s to %s\n", val, buffer);
190 closecxx . . $ close(7864480464b09eea).(fd);
191 return -1;
192 }
193 closecxx . . $ close(7864480464b09eea).(fd);
194 return 0;
195 }
196
197 int event_enable_allcxx . . $ event_enable_all(b126dc7c1de90089).(void)
198 {
199 return seteventcxx . . $ setevent(e99f026884c896d5).("events/enable", "1");
200 }
201
202 int event_disable_allcxx . . $ event_disable_all(b126dc7c1de90089).(void)
203 {
204 return seteventcxx . . $ setevent(e99f026884c896d5).("events/enable", "0");
205 }
206
207 int event_enablecxx . . $ event_enable(1ef8e6ec083aabb9).(char *event)
208 {
209 char path[MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!];
210
211 sprintfcxx . . $ sprintf(26c8642d26409d6d).(path, "events/%s/enable", event);
212 return seteventcxx . . $ setevent(e99f026884c896d5).(path, "1");
213 }
214
215 int event_disablecxx . . $ event_disable(1ef8e6ec083aabb9).(char *event)
216 {
217 char path[MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!];
218
219 sprintfcxx . . $ sprintf(26c8642d26409d6d).(path, "events/%s/enable", event);
220 return seteventcxx . . $ setevent(e99f026884c896d5).(path, "0");
221 }
222
223 int check_privscxx . . $ check_privs(b126dc7c1de90089).(void)
224 {
225 int policy = sched_getschedulercxx . . $ sched_getscheduler(7864480464b09eea).(0);
226 struct sched_paramcxx . . $ sched_param# param, old_param;
227
228 /* if we're already running a realtime scheduler
229 * then we *should* be able to change things later
230 */
231 if (policy == SCHED_FIFOcxx . . $ `<external>/4872883757279652994/sched.h:29:9`! || policy == SCHED_RRcxx . . $ `<external>/4872883757279652994/sched.h:30:9`!)
232 return 0;
233
234 /* first get the current parameters */
235 if (sched_getparamcxx . . $ sched_getparam(434f53043d3e31ba).(0, &old_param)) {
236 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "unable to get scheduler parameters\n");
237 return 1;
238 }
239 param = old_param;
240
241 /* try to change to SCHED_FIFO */
242 param.sched_prioritycxx . . $ `<external>/17522079545615609418/sched.h:47:9`! = 1;
243 if (sched_setschedulercxx . . $ sched_setscheduler(b99e0841b006542f).(0, SCHED_FIFOcxx . . $ `<external>/4872883757279652994/sched.h:29:9`!, &param)) {
244 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Unable to change scheduling policy!\n");
•245 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Probably missing capabilities, either run as "
246 "root or increase RLIMIT_RTPRIO limits.\n");
247 return 1;
248 }
249
250 /* we're good; change back and return success */
251 return sched_setschedulercxx . . $ sched_setscheduler(b99e0841b006542f).(0, policy, &old_param);
252 }
253
254 const char *policy_to_stringcxx . . $ policy_to_string(f7ae7e56280f18ae).(int policy)
255 {
256 switch (policy) {
257 case SCHED_OTHERcxx . . $ `<external>/4872883757279652994/sched.h:28:9`!:
258 return "SCHED_OTHER";
259 case SCHED_FIFOcxx . . $ `<external>/4872883757279652994/sched.h:29:9`!:
260 return "SCHED_FIFO";
261 case SCHED_RRcxx . . $ `<external>/4872883757279652994/sched.h:30:9`!:
262 return "SCHED_RR";
263 case SCHED_BATCHcxx . . $ `<external>/4872883757279652994/sched.h:33:10`!:
264 return "SCHED_BATCH";
265 case SCHED_IDLEcxx . . $ `<external>/4872883757279652994/sched.h:35:10`!:
266 return "SCHED_IDLE";
267 case SCHED_DEADLINEcxx . . $ `<external>/4872883757279652994/sched.h:36:10`!:
268 return "SCHED_DEADLINE";
269 }
270
271 return "unknown";
272 }
273
274 uint32_tcxx . . $ uint32_t# string_to_policycxx . . $ string_to_policy(c61c63047ce273de).(const char *str)
275 {
276 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "other"))
277 return SCHED_OTHERcxx . . $ `<external>/4872883757279652994/sched.h:28:9`!;
278 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "fifo"))
279 return SCHED_FIFOcxx . . $ `<external>/4872883757279652994/sched.h:29:9`!;
280 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "rr"))
281 return SCHED_RRcxx . . $ `<external>/4872883757279652994/sched.h:30:9`!;
282 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "batch"))
283 return SCHED_BATCHcxx . . $ `<external>/4872883757279652994/sched.h:33:10`!;
284 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "idle"))
285 return SCHED_IDLEcxx . . $ `<external>/4872883757279652994/sched.h:35:10`!;
286 else if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(str, "deadline"))
287 return SCHED_DEADLINEcxx . . $ `<external>/4872883757279652994/sched.h:36:10`!;
288
289 return 0;
290 }
291
292 pid_tcxx . . $ pid_t# gettidcxx . . $ gettid(b126dc7c1de90089).(void)
293 {
294 return syscallcxx . . $ syscall(523f12be2d96faea).(SYS_gettidcxx . . $ `<external>/16530160117609833761/syscall.h:683:10`!);
295 }
296
297 /*
298 * parse an input value as a base10 value followed by an optional
299 * suffix. The input value is presumed to be in seconds, unless
300 * followed by a modifier suffix: m=minutes, h=hours, d=days
301 *
302 * the return value is a value in seconds
303 */
304 int parse_time_stringcxx . . $ parse_time_string(1ef8e6ec083aabb9).(char *val)
305 {
306 char *end;
307 int t = strtolcxx . . $ strtol(44bbb87609b212fc).(val, &end, 10);
308 if (end) {
309 switch (*end) {
310 case 'm':
311 case 'M':
312 t *= 60;
313 break;
314
315 case 'h':
316 case 'H':
317 t *= 60*60;
318 break;
319
320 case 'd':
321 case 'D':
322 t *= 24*60*60;
323 break;
324
325 }
326 }
327 return t;
328 }
329
330 int parse_mem_stringcxx . . $ parse_mem_string(bd93ee2296053586).(char *str, uint64_tcxx . . $ uint64_t# *val)
331 {
332 char *endptr;
333 int v = strtolcxx . . $ strtol(44bbb87609b212fc).(str, &endptr, 10);
334
335 if (!*endptr)
336 return v;
337
338 switch (*endptr) {
339 case 'g':
340 case 'G':
341 v *= 1024;
342 __attribute__ ((fallthrough));
343 case 'm':
344 case 'M':
345 v *= 1024;
346 __attribute__ ((fallthrough));
347 case 'k':
348 case 'K':
349 v *= 1024;
350 __attribute__ ((fallthrough));
351 case 'b':
352 case 'B':
353 break;
354 default:
355 return -1;
356 }
357
358 *val = v;
359
360 return 0;
361 }
362
363 static void open_tracemark_fdcxx . . $ open_tracemark_fd(49f6e7a06ebc5aa8).(void)
364 {
365 char path[MAX_PATHcxx . . $ `src/include/rt-utils.h:9:9`!];
366 int n;
367
368 /*
369 * open the tracemark file if it's not already open
370 */
371 if (tracemark_fdcxx . . $ tracemark_fd. < 0) {
372 n = snprintfcxx . . $ snprintf(ea502d73e74c754c).(path, sizeof(path), "%s/%s", tracefs_prefixcxx . . $ tracefs_prefix., "trace_marker");
373 if (n >= sizeof(path)) {
374 warncxx . . $ warn(d5d9ff45db7b5d36).("tracefs path too long\n");
375 return;
376 }
377 tracemark_fdcxx . . $ tracemark_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(path, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!);
378 if (tracemark_fdcxx . . $ tracemark_fd. < 0) {
379 warncxx . . $ warn(d5d9ff45db7b5d36).("unable to open trace_marker file: %s\n", path);
380 return;
381 }
382 }
383
384 /*
385 * if we're not tracing and the tracing_on fd is not open,
386 * open the tracing_on file so that we can stop the trace
387 * if we hit a breaktrace threshold
388 */
389 if (trace_fdcxx . . $ trace_fd. < 0) {
390 n = snprintfcxx . . $ snprintf(ea502d73e74c754c).(path, sizeof(path), "%s/%s", tracefs_prefixcxx . . $ tracefs_prefix., "tracing_on");
391 if (n >= sizeof(path)) {
392 warncxx . . $ warn(d5d9ff45db7b5d36).("tracefs path too long\n");
393 return;
394 }
395 if ((trace_fdcxx . . $ trace_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(path, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!)) < 0)
396 warncxx . . $ warn(d5d9ff45db7b5d36).("unable to open tracing_on file: %s\n", path);
397 }
398 }
399
400 static void close_tracemark_fdcxx . . $ close_tracemark_fd(49f6e7a06ebc5aa8).(void)
401 {
402 if (tracemark_fdcxx . . $ tracemark_fd. > 0)
403 closecxx . . $ close(7864480464b09eea).(tracemark_fdcxx . . $ tracemark_fd.);
404
405 if (trace_fdcxx . . $ trace_fd. > 0)
406 closecxx . . $ close(7864480464b09eea).(trace_fdcxx . . $ trace_fd.);
407 }
408
409 static int trace_file_existscxx . . $ trace_file_exists(1ef8e6ec083aabb9).(char *name)
410 {
411 struct statcxx . . $ stat# sbuf;
412 return statcxx . . $ stat(a9cd1e23e96f423c).(tracefs_prefixcxx . . $ tracefs_prefix., &sbuf) ? 0 : 1;
413 }
414
415 static void tracefs_preparecxx . . $ tracefs_prepare(49f6e7a06ebc5aa8).(void)
416 {
417 get_tracefs_prefixcxx . . $ get_tracefs_prefix(41208ce6ca65dd58).();
418 if (!trace_file_existscxx . . $ trace_file_exists(1ef8e6ec083aabb9).("tracing_enabled") &&
419 !trace_file_existscxx . . $ trace_file_exists(1ef8e6ec083aabb9).("tracing_on"))
420 warncxx . . $ warn(d5d9ff45db7b5d36).("tracing_enabled or tracing_on not found\n"
421 "tracefs not found");
422 }
423
424 void tracemarkcxx . . $ tracemark(d5d9ff45db7b5d36).(char *fmt, ...)
425 {
426 va_listcxx . . $ va_list# ap;
427 int len;
428 int ret;
429
430 /* bail out if we're not tracing */
431 /* or if the kernel doesn't support trace_mark */
432 if (tracemark_fdcxx . . $ tracemark_fd. < 0)
433 return;
434
435 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(ap, fmt);
436 len = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(tracebufcxx . . $ tracebuf., TRACEBUFSIZcxx . . $ `src/lib/rt-utils.c:31:10`!, fmt, ap);
437 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(ap);
438
439 /* write the tracemark message */
440 ret = writecxx . . $ write(41184a59d38b4f65).(tracemark_fdcxx . . $ tracemark_fd., tracebufcxx . . $ tracebuf., len);
441 if (ret != len)
442 warncxx . . $ warn(d5d9ff45db7b5d36).("trace mark write failed\n");
443 }
444
445 void tracing_stopcxx . . $ tracing_stop(49f6e7a06ebc5aa8).(void)
446 {
447 int ret;
448
449 if (trace_fdcxx . . $ trace_fd. < 0)
450 return;
451 ret = writecxx . . $ write(41184a59d38b4f65).(trace_fdcxx . . $ trace_fd., "0\n", 2);
452 if (ret != 2)
453 warncxx . . $ warn(d5d9ff45db7b5d36).("trace stop write failed\n");
454 }
455
456 void enable_trace_markcxx . . $ enable_trace_mark(49f6e7a06ebc5aa8).(void)
457 {
458 tracefs_preparecxx . . $ tracefs_prepare(49f6e7a06ebc5aa8).();
459 open_tracemark_fdcxx . . $ open_tracemark_fd(49f6e7a06ebc5aa8).();
460 }
461
462 void disable_trace_markcxx . . $ disable_trace_mark(49f6e7a06ebc5aa8).(void)
463 {
464 close_tracemark_fdcxx . . $ close_tracemark_fd(49f6e7a06ebc5aa8).();
465 }
466
467 static void get_timestampcxx . . $ get_timestamp(fda57c8427c60377).(char *tsbuf)
468 {
469 struct timevalcxx . . $ timeval# tv;
470 struct tmcxx . . $ tm# *tm;
471 time_tcxx . . $ time_t# t;
472
473 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
474 t = tv.tv_seccxx . . $ timeval#tv_sec.;
475 tm = localtimecxx . . $ localtime(9477a1348abbbc5).(&t);
476 /* RFC 2822-compliant date format */
477 strftimecxx . . $ strftime(98e75925167c4f1b).(tsbuf, MAX_TS_SIZEcxx . . $ `src/lib/rt-utils.c:33:10`!, "%a, %d %b %Y %T %z", tm);
478 }
479
480 void rt_initcxx . . $ rt_init(c759356ac01a31e7).(int argc, char *argv[])
481 {
482 int offset = 0;
483 int len, i;
484
485 test_cmdlinecxx . . $ test_cmdline.[0] = '\0';
486
487 /*
488 * getopt_long() permutes the contents of argv as it scans, so
489 * that eventually all the nonoptions are at the end. Make a
490 * copy before calling getopt_long().
491 */
492 for (i = 0; i < argc;) {
493 len = strlencxx . . $ strlen(751346f8406fd082).(argv[i]);
494 if (offset + len + 1 >= MAX_COMMAND_LINEcxx . . $ `src/lib/rt-utils.c:32:10`!)
495 break;
496
497 strcatcxx . . $ strcat(b7b3a83273496486).(test_cmdlinecxx . . $ test_cmdline., argv[i]);
498 i++;
499 if (i < argc)
500 strcatcxx . . $ strcat(b7b3a83273496486).(test_cmdlinecxx . . $ test_cmdline., " ");
501
502 offset += len + 1;
503 }
504
505 get_timestampcxx . . $ get_timestamp(fda57c8427c60377).(ts_startcxx . . $ ts_start.);
506 }
507
508 void rt_write_jsoncxx . . $ rt_write_json(969c696bbc9a09ad).(const char *filename, int return_code,
509 void (*cb)(FILEcxx . . $ FILE# *, void *),
510 void *data)
511 {
512 unsigned char buf[1];
513 struct utsnamecxx . . $ utsname# uts;
514 char ts_end[MAX_TS_SIZEcxx . . $ `src/lib/rt-utils.c:33:10`!];
515 FILEcxx . . $ FILE# *f, *s;
516 size_tcxx . . $ size_t# n;
517 int rt = 0;
518
519 if (!filename || !strcmpcxx . . $ strcmp(5ae29d726ae57f88).("-", filename)) {
520 f = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!;
521 } else {
522 f = fopencxx . . $ fopen(82ec0787cd37e0a3).(filename, "w");
523 if (!f)
524 err_exitcxx . . $ err_exit(7cbaebca1c50800).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, "Failed to open '%s'\n", filename);
525 }
526
527 get_timestampcxx . . $ get_timestamp(fda57c8427c60377).(ts_end);
528
529 s = fopencxx . . $ fopen(82ec0787cd37e0a3).("/sys/kernel/realtime", "r");
530 if (s) {
531 n = freadcxx . . $ fread(738fc8ac1d711e58).(buf, 1, 1, s);
532 if (n == 1 && buf[0] == '1')
533 rt = 1;
534 fclosecxx . . $ fclose(89bc1efcdac0ca22).(s);
535 }
536
537 if (unamecxx . . $ uname(c69a16225c55e184).(&uts))
538 err_exitcxx . . $ err_exit(7cbaebca1c50800).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, "Could not retrieve system information");
539
540 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "{\n");
541 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"file_version\": 1,\n");
542 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"cmdline:\": \"%s\",\n", test_cmdlinecxx . . $ test_cmdline.);
543 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"rt_test_version:\": \"%1.2f\",\n", VERSIONcxx . . $ `<built-in>:412:9`!);
544 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"start_time\": \"%s\",\n", ts_startcxx . . $ ts_start.);
545 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"end_time\": \"%s\",\n", ts_end);
546 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"return_code\": %d,\n", return_code);
547 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"sysinfo\": {\n");
548 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"sysname\": \"%s\",\n", uts.sysnamecxx . . $ utsname#sysname.);
549 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"nodename\": \"%s\",\n", uts.nodenamecxx . . $ utsname#nodename.);
550 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"release\": \"%s\",\n", uts.releasecxx . . $ utsname#release.);
551 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"version\": \"%s\",\n", uts.versioncxx . . $ utsname#version.);
552 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"machine\": \"%s\",\n", uts.machinecxx . . $ utsname#machine.);
553 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " \"realtime\": %d\n", rt);
554
555 if (cb) {
556 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " },\n");
557 (cb)(f, data);
558 } else {
559 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " }\n");
560 }
561
562 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "}\n");
563
564 if (!filename || strcmpcxx . . $ strcmp(5ae29d726ae57f88).("-", filename))
565 fclosecxx . . $ fclose(89bc1efcdac0ca22).(f);
566 }