Motr  M0
trace_dump.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 
24 #include <stdio.h> /* printf */
25 #include <err.h> /* err */
26 #include <errno.h> /* errno */
27 #include <string.h> /* strcpy, basename */
28 #include <sysexits.h> /* EX_* exit codes (EX_OSERR, EX_SOFTWARE) */
29 
30 #include "module/instance.h" /* m0 */
31 #include "motr/init.h" /* m0_init */
32 #include "lib/uuid.h" /* m0_node_uuid_string_set */
33 #include "lib/getopts.h" /* M0_GETOPTS */
34 #include "lib/thread.h" /* LAMBDA */
35 #include "lib/string.h" /* m0_strdup */
36 #include "lib/user_space/types.h" /* bool */
37 #include "lib/user_space/trace.h" /* m0_trace_parse */
38 #include "lib/misc.h" /* ARRAY_SIZE */
39 
40 
41 #define DEFAULT_MOTR_KO_IMG_PATH "/var/log/motr/m0tr_ko.img"
42 
43 int main(int argc, char *argv[])
44 {
45  static struct m0 instance;
46 
47  const char std_inout_file_name[] = "-";
48  const char *input_file_name = std_inout_file_name;
49  const char *output_file_name = std_inout_file_name;
50  const char *m0tr_ko_path = DEFAULT_MOTR_KO_IMG_PATH;
52  FILE *input_file;
53  FILE *output_file;
54  int rc;
55 
56  /* prevent creation of trace file for ourselves */
58 
59  /* we don't need a real node uuid, so we force a default one to be useed
60  * instead */
62 
63  rc = m0_init(&instance);
64  if (rc != 0)
65  return EX_SOFTWARE;
66 
67  /* process CLI options */
68  rc = M0_GETOPTS(basename(argv[0]), argc, argv,
69  M0_HELPARG('h'),
70  M0_STRINGARG('i',
71  "input file name, if none is provided, then STDIN is used by"
72  " default",
73  LAMBDA(void, (const char *str) {
75  })
76  ),
77  M0_STRINGARG('o',
78  "output file name, if none is provided, then STDOUT is used by"
79  " default",
80  LAMBDA(void, (const char *str) {
82  })
83  ),
84  M0_VOIDARG('s',
85  "stream mode, each trace record is formatted as a"
86  " separate YAML document, so they can be fetched from"
87  " YAML stream one by one (this option has no effect as it's"
88  " 'on' by default, it has been kept for backward"
89  " compatibility, it's superseded by '-S' option)",
90  LAMBDA(void, (void) { })
91  ),
92  M0_VOIDARG('S',
93  "disable stream mode (discards action of '-s' option)",
94  LAMBDA(void, (void) {
96  })
97  ),
98  M0_VOIDARG('H',
99  "dump only trace header information",
100  LAMBDA(void, (void) {
102  })
103  ),
104  M0_STRINGARG('k',
105  "path to m0tr.ko modules's core image (only required for"
106  " parsing kernel mode trace files), by default it is '"
108  LAMBDA(void, (const char *str) {
109  m0tr_ko_path = m0_strdup(str);
110  })
111  ),
112  );
113 
114  if (rc != 0)
115  return EX_USAGE;
116 
117  /* open input file */
118  if (strcmp(input_file_name, std_inout_file_name) == 0) {
119  input_file = stdin;
120  } else {
121  input_file = fopen(input_file_name, "r");
122  if (input_file == NULL)
123  err(EX_NOINPUT, "Failed to open input file '%s'",
125  }
126 
127  /* open output file */
128  if (strcmp(output_file_name, std_inout_file_name) == 0) {
129  output_file = stdout;
130  } else {
131  output_file = fopen(output_file_name, "w");
132  if (output_file == NULL)
133  err(EX_CANTCREAT, "Failed to open output file '%s'",
135  }
136 
137  rc = m0_trace_parse(input_file, output_file, m0tr_ko_path, flags,
138  0, 0);
139  if (rc != 0) {
140  warnx("Error occurred while parsing input trace data");
141  rc = EX_SOFTWARE;
142  }
143 
144  m0_fini();
145 
146  fclose(output_file);
147  fclose(input_file);
148 
149  return rc;
150 }
151 
152 /*
153  * Local variables:
154  * c-indentation-style: "K&R"
155  * c-basic-offset: 8
156  * tab-width: 8
157  * fill-column: 80
158  * scroll-step: 1
159  * End:
160  */
161 /*
162  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
163  */
#define M0_GETOPTS(progname, argc, argv,...)
Definition: getopts.h:169
#define m0_strdup(s)
Definition: string.h:43
int const char const void size_t int flags
Definition: dir.c:328
#define NULL
Definition: misc.h:38
void m0_fini(void)
Definition: init.c:318
static const char * output_file_name
Definition: traced.c:67
int m0_init(struct m0 *instance)
Definition: init.c:310
m0_trace_parse_flags
Definition: trace.h:41
#define M0_VOIDARG(ch, desc, func)
Definition: getopts.h:177
#define M0_STRINGARG(ch, desc, func)
Definition: getopts.h:207
int main(int argc, char *argv[])
Definition: trace_dump.c:43
#define LAMBDA(T,...)
Definition: thread.h:153
void m0_node_uuid_string_set(const char *uuid)
Definition: uuuid.c:64
Definition: instance.h:80
#define M0_HELPARG(ch)
Definition: getopts.h:242
static struct m0 instance
Definition: main.c:78
M0_INTERNAL int m0_trace_parse(FILE *trace_file, FILE *output_file, const char *m0tr_ko_path, enum m0_trace_parse_flags flags, const void *magic_symbols[], unsigned int magic_symbols_nr)
Definition: utrace.c:527
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL void m0_trace_set_mmapped_buffer(bool val)
Definition: utrace.c:156
static const char * input_file_name
Definition: traced.c:66
#define DEFAULT_MOTR_KO_IMG_PATH
Definition: trace_dump.c:41