blob: 2dc36dfa0896b28e2ac4a84de7cac8c2fd1e4174 [file] [log] [blame]
Quentin Monnet30da46b2018-12-05 10:28:24 +00001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (c) 2015-2017 Daniel Borkmann */
3/* Copyright (c) 2018 Netronome Systems, Inc. */
4
5#include <errno.h>
6#include <limits.h>
7#include <signal.h>
8#include <stdio.h>
9#include <string.h>
10#include <unistd.h>
11#include <linux/magic.h>
12#include <sys/fcntl.h>
13#include <sys/vfs.h>
14
15#include "main.h"
16
17#ifndef TRACEFS_MAGIC
18# define TRACEFS_MAGIC 0x74726163
19#endif
20
21#define _textify(x) #x
22#define textify(x) _textify(x)
23
24FILE *trace_pipe_fd;
25char *buff;
26
27static int validate_tracefs_mnt(const char *mnt, unsigned long magic)
28{
29 struct statfs st_fs;
30
31 if (statfs(mnt, &st_fs) < 0)
32 return -ENOENT;
33 if ((unsigned long)st_fs.f_type != magic)
34 return -ENOENT;
35
36 return 0;
37}
38
39static bool
40find_tracefs_mnt_single(unsigned long magic, char *mnt, const char *mntpt)
41{
42 size_t src_len;
43
44 if (validate_tracefs_mnt(mntpt, magic))
45 return false;
46
47 src_len = strlen(mntpt);
48 if (src_len + 1 >= PATH_MAX) {
49 p_err("tracefs mount point name too long");
50 return false;
51 }
52
53 strcpy(mnt, mntpt);
54 return true;
55}
56
Quentin Monnetbe3245e2018-12-18 10:13:18 +000057static bool get_tracefs_pipe(char *mnt)
Quentin Monnet30da46b2018-12-05 10:28:24 +000058{
59 static const char * const known_mnts[] = {
60 "/sys/kernel/debug/tracing",
61 "/sys/kernel/tracing",
62 "/tracing",
63 "/trace",
64 };
65 const char *pipe_name = "/trace_pipe";
66 const char *fstype = "tracefs";
67 char type[100], format[32];
68 const char * const *ptr;
69 bool found = false;
70 FILE *fp;
71
72 for (ptr = known_mnts; ptr < known_mnts + ARRAY_SIZE(known_mnts); ptr++)
73 if (find_tracefs_mnt_single(TRACEFS_MAGIC, mnt, *ptr))
74 goto exit_found;
75
76 fp = fopen("/proc/mounts", "r");
77 if (!fp)
78 return false;
79
80 /* Allow room for NULL terminating byte and pipe file name */
81 snprintf(format, sizeof(format), "%%*s %%%zds %%99s %%*s %%*d %%*d\\n",
82 PATH_MAX - strlen(pipe_name) - 1);
83 while (fscanf(fp, format, mnt, type) == 2)
84 if (strcmp(type, fstype) == 0) {
85 found = true;
86 break;
87 }
88 fclose(fp);
89
90 /* The string from fscanf() might be truncated, check mnt is valid */
Quentin Monnetbe3245e2018-12-18 10:13:18 +000091 if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
92 goto exit_found;
93
94 p_info("could not find tracefs, attempting to mount it now");
95 /* Most of the time, tracefs is automatically mounted by debugfs at
96 * /sys/kernel/debug/tracing when we try to access it. If we could not
97 * find it, it is likely that debugfs is not mounted. Let's give one
98 * attempt at mounting just tracefs at /sys/kernel/tracing.
99 */
100 strcpy(mnt, known_mnts[1]);
101 if (mount_tracefs(mnt))
Quentin Monnet30da46b2018-12-05 10:28:24 +0000102 return false;
103
104exit_found:
105 strcat(mnt, pipe_name);
106 return true;
107}
108
109static void exit_tracelog(int signum)
110{
111 fclose(trace_pipe_fd);
112 free(buff);
113
114 if (json_output) {
115 jsonw_end_array(json_wtr);
116 jsonw_destroy(&json_wtr);
117 }
118
119 exit(0);
120}
121
122int do_tracelog(int argc, char **argv)
123{
124 const struct sigaction act = {
125 .sa_handler = exit_tracelog
126 };
127 char trace_pipe[PATH_MAX];
Quentin Monnet30da46b2018-12-05 10:28:24 +0000128 size_t buff_len = 0;
129
130 if (json_output)
131 jsonw_start_array(json_wtr);
132
Quentin Monnetbe3245e2018-12-18 10:13:18 +0000133 if (!get_tracefs_pipe(trace_pipe))
Quentin Monnet30da46b2018-12-05 10:28:24 +0000134 return -1;
Quentin Monnet30da46b2018-12-05 10:28:24 +0000135
136 trace_pipe_fd = fopen(trace_pipe, "r");
137 if (!trace_pipe_fd) {
138 p_err("could not open trace pipe: %s", strerror(errno));
139 return -1;
140 }
141
142 sigaction(SIGHUP, &act, NULL);
143 sigaction(SIGINT, &act, NULL);
144 sigaction(SIGTERM, &act, NULL);
145 while (1) {
146 ssize_t ret;
147
148 ret = getline(&buff, &buff_len, trace_pipe_fd);
149 if (ret <= 0) {
150 p_err("failed to read content from trace pipe: %s",
151 strerror(errno));
152 break;
153 }
154 if (json_output)
155 jsonw_string(json_wtr, buff);
156 else
157 printf("%s", buff);
158 }
159
160 fclose(trace_pipe_fd);
161 free(buff);
162 return -1;
163}