barectf-tracepoint: add QEMU ARM target example
[barectf.git] / doc / examples / barectf-tracepoint / barectf-platform-qemu-arm-uart.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <barectf.h>
4
5 #include "barectf-platform-qemu-arm-uart.h"
6
7 #define BUF_SIZE 4096
8 #define TIMER_CTRL_32BIT (1 << 1)
9 #define TIMER_CTRL_ENABLE (1 << 7)
10
11 volatile uint32_t * const uart0 = (uint32_t *) 0x101f1000;
12 volatile uint32_t * const uart1 = (uint32_t *) 0x101f2000;
13 volatile uint32_t * const timer0_ctrl = (uint32_t *) 0x101e2008;
14 volatile uint32_t * const timer0_value = (uint32_t *) 0x101e2004;
15
16 static struct barectf_default_ctx barectf_ctx;
17
18 static uint8_t buf[BUF_SIZE];
19
20 static uint64_t get_clock(void* data)
21 {
22 return (uint64_t) -*timer0_value;
23 }
24
25 static void flush_packet(void)
26 {
27 size_t i;
28
29 /* flush packet to UART 1 */
30 for (i = 0; i < BUF_SIZE; ++i) {
31 *uart1 = (uint32_t) buf[i];
32 }
33 }
34
35 static int is_backend_full(void *data)
36 {
37 return 0;
38 }
39
40 static void open_packet(void *data)
41 {
42 barectf_default_open_packet(&barectf_ctx);
43 }
44
45 static void close_packet(void *data)
46 {
47 /* close packet now */
48 barectf_default_close_packet(&barectf_ctx);
49
50 /* flush current packet */
51 flush_packet();
52 }
53
54 void barectf_platform_qemu_arm_uart_init(void)
55 {
56 struct barectf_platform_callbacks cbs = {
57 .default_clock_get_value = get_clock,
58 .is_backend_full = is_backend_full,
59 .open_packet = open_packet,
60 .close_packet = close_packet,
61 };
62
63 /* enable/start timer (clock source) */
64 *timer0_ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE;
65
66 /* initialize barectf context */
67 barectf_init(&barectf_ctx, buf, BUF_SIZE, cbs, NULL);
68
69 /* open first packet */
70 open_packet(NULL);
71
72 /* indicate that tracing is starting */
73 puts("tracing: starting");
74 }
75
76 void barectf_platform_qemu_arm_uart_fini(void)
77 {
78 /* close last packet if it contains at least one event */
79 if (barectf_packet_is_open(&barectf_ctx) &&
80 !barectf_packet_is_empty(&barectf_ctx)) {
81 close_packet(NULL);
82 }
83
84 /* indicate that tracing is done */
85 puts("tracing: done");
86 }
87
88 struct barectf_default_ctx *barectf_platform_qemu_arm_uart_get_barectf_ctx()
89 {
90 return &barectf_ctx;
91 }
92
93 #define STDOUT 1
94 #define STDERR 2
95
96 /* custom write "syscall" for newlib's stdio: write to UART 0 */
97 int _write(int file, char *ptr, int len) {
98 if (file == STDOUT || file == STDERR) {
99 int i;
100
101 for (i = 0; i < len; i++) {
102 *uart0 = (uint32_t) ptr[i];
103 }
104 }
105
106 return 0;
107 }
This page took 0.048761 seconds and 4 git commands to generate.