1 /*
2  * Copyright (c) 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <soc.h>
8 #include <stdio.h>
9 #include <kernel.h>
10 #include <cmdline.h>
11 #include <sys/__assert.h>
12 #include <tracing_backend.h>
13 
14 static void *out_stream;
15 static const char *file_name;
16 
tracing_backend_posix_init(void)17 static void tracing_backend_posix_init(void)
18 {
19 	if (file_name == NULL) {
20 		file_name = "channel0_0";
21 	}
22 
23 	out_stream = (void *)fopen(file_name, "wb");
24 
25 	__ASSERT(out_stream != NULL, "posix backend init failed");
26 }
27 
tracing_backend_posix_output(const struct tracing_backend * backend,uint8_t * data,uint32_t length)28 static void tracing_backend_posix_output(
29 		const struct tracing_backend *backend,
30 		uint8_t *data, uint32_t length)
31 {
32 	fwrite(data, length, 1, (FILE *)out_stream);
33 
34 	if (!k_is_in_isr()) {
35 		fflush((FILE *)out_stream);
36 	}
37 }
38 
39 const struct tracing_backend_api tracing_backend_posix_api = {
40 	.init = tracing_backend_posix_init,
41 	.output  = tracing_backend_posix_output
42 };
43 
44 TRACING_BACKEND_DEFINE(tracing_backend_posix, tracing_backend_posix_api);
45 
tracing_backend_posix_option(void)46 void tracing_backend_posix_option(void)
47 {
48 	static struct args_struct_t tracing_backend_option[] = {
49 		{
50 			.manual = false,
51 			.is_mandatory = false,
52 			.is_switch = false,
53 			.option = "trace-file",
54 			.name = "file_name",
55 			.type = 's',
56 			.dest = (void *)&file_name,
57 			.call_when_found = NULL,
58 			.descript = "File name for tracing output.",
59 		},
60 		ARG_TABLE_ENDMARKER
61 	};
62 
63 	native_add_command_line_opts(tracing_backend_option);
64 }
65 
66 NATIVE_TASK(tracing_backend_posix_option, PRE_BOOT_1, 1);
67