Motr  M0
protocol.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_XCODE
31 
32 #include <stdio.h>
33 #include <string.h> /* strcmp, basename */
34 #include <stdlib.h> /* qsort */
35 #include <err.h>
36 #include <sys/stat.h>
37 #include <sysexits.h>
38 
39 #include "lib/uuid.h" /* m0_node_uuid_string_set */
40 #include "lib/misc.h" /* ARRAY_SIZE */
41 #include "lib/user_space/types.h" /* bool */
42 #include "motr/init.h"
43 #include "motr/version.h" /* m0_build_info_print */
44 #include "module/instance.h"
45 #include "sm/sm.h" /* m0_sm_conf_print */
46 #include "lib/user_space/trace.h" /* m0_trace_set_mmapped_buffer */
47 #include "xcode/xcode.h"
48 #include "xcode/init.h" /* m0_xcode_init */
49 
50 #undef __MOTR_XCODE_XLIST_H__
51 
52 #define _TI(x)
53 #define _EN(x)
54 #define _FI(x)
55 #define _FF(x)
56 
57 #define _XT(x) M0_EXTERN struct m0_xcode_type *x;
58 #include "xcode/xlist.h"
59 #undef _XT
60 
61 /* Let it be included second time. */
62 #undef __MOTR_XCODE_XLIST_H__
63 
64 static struct m0_xcode_type **xt[] = {
65 #define _XT(x) &x,
66 #include "xcode/xlist.h"
67 #undef _XT
68 };
69 
70 #undef _TI
71 #undef _EN
72 #undef _FI
73 #undef _FF
74 
75 static void field_print(const struct m0_xcode_field *f, int i);
76 static void type_print(const struct m0_xcode_type *xt);
77 static void protocol_print(int dom);
78 static int cmp(const void *p0, const void *p1);
79 
80 static void type_print(const struct m0_xcode_type *xt)
81 {
82  int i;
83 
84  printf("\n%-30s %8.8s %6zi",
85  xt->xct_name,
86  xt->xct_aggr == M0_XA_ATOM ?
89  xt->xct_sizeof);
90  for (i = 0; i < xt->xct_nr; ++i)
91  field_print(&xt->xct_child[i], i);
92  printf("\nend %s", xt->xct_name);
93 }
94 
95 static const char *xcode_domain_name = "all";
96 
97 static void protocol_print(int dom)
98 {
99  const struct m0_xcode_type *xtype;
100  size_t total_structs = ARRAY_SIZE(xt);
101  int i;
102 
103  qsort(xt, ARRAY_SIZE(xt), sizeof xt[0], &cmp);
104  if (dom != 0)
105  for (i = 0, total_structs = 0; i < ARRAY_SIZE(xt); ++i) {
106  xtype = *(xt[i]);
107  if (xtype->xct_flags & dom)
108  total_structs++;
109  }
110 
111  printf("Motr binary protocol\n");
112  printf("Domains: %s\n", xcode_domain_name);
113  printf("Total structures: %zu\n", total_structs);
114 
115  for (i = 0; i < ARRAY_SIZE(xt); ++i) {
116  xtype = *(xt[i]);
117  if (dom == 0 /* any domain */ || xtype->xct_flags & dom)
118  type_print(xtype);
119  }
120  printf("\n");
121 }
122 
123 static int cmp(const void *p0, const void *p1)
124 {
125  const struct m0_xcode_type *xt0 = **(void ***)p0;
126  const struct m0_xcode_type *xt1 = **(void ***)p1;
127 
128  return strcmp(xt0->xct_name, xt1->xct_name);
129 }
130 
131 static void field_print(const struct m0_xcode_field *f, int i)
132 {
133  printf("\n %4i %-20s %-20s %4"PRIi32" [%#" PRIx64 "]",
134  i, f->xf_name, f->xf_type->xct_name, f->xf_offset, f->xf_tag);
135 }
136 
137 void (*m0_sm__conf_init)(const struct m0_sm_conf *conf);
138 
139 void usage_print(const char* progname) {
140  fprintf(stderr,
141  "Usage: %s [options]\n"
142  " -p|--only-proto print only protocol, skip"
143  " any other useful info\n"
144  " -d|--domain DOM print only protocol for a particular"
145  " xcode domain, available domains are:"
146  " 'be', 'conf' and 'rpc'\n"
147  "\n"
148  " -v|--version print version information\n"
149  " -h|--help print usage information\n",
150  progname);
151 }
152 
153 int main(int argc, char **argv)
154 {
155  struct m0 instance = {};
156  bool only_protocol = false;
157  int xcode_domain = 0; /* any domain */
158  int i;
159  int result;
160 
161  /* prevent creation of trace file for ourselves */
163 
164  /*
165  * break dependency on m0tr.ko module and make ADDB happy,
166  * as we don't need a real node uuid for normal operation of this utility
167  */
169 
170  /*
171  * using "manual" option processing because M0_GETOPTS expects m0_init()
172  * to be already done (due to m0_alloc() which uses fault injection
173  * which in turn uses m0_mutex)
174  */
175  for (i = 1; i < argc; ++i) {
176  if (strcmp(argv[i], "-p") == 0 ||
177  strcmp(argv[i], "--only-proto") == 0)
178  {
179  only_protocol = true;
180  continue;
181  }
182  if (strcmp(argv[i], "-d") == 0 ||
183  strcmp(argv[i], "--domain") == 0)
184  {
185  if (i + 1 >= argc) {
186  warnx("Error: missing argument for"
187  " '-d|--domain' option\n");
188  usage_print(basename(argv[0]));
189  exit(EX_USAGE);
190  }
191 
192  if (strcmp(argv[i + 1], "be") == 0) {
193  xcode_domain = M0_XCODE_TYPE_FLAG_DOM_BE;
194  } else if (strcmp(argv[i + 1], "conf") == 0) {
195  xcode_domain = M0_XCODE_TYPE_FLAG_DOM_CONF;
196  } else if (strcmp(argv[i + 1], "rpc") == 0) {
197  xcode_domain = M0_XCODE_TYPE_FLAG_DOM_RPC;
198  } else {
199  warnx("Error: invaid domain '%s' specified for"
200  " '-d|--domain' option\n", argv[i + 1]);
201  usage_print(basename(argv[0]));
202  exit(EX_USAGE);
203  }
204 
205  xcode_domain_name = argv[++i];
206  only_protocol = true;
207  continue;
208  }
209  if (strcmp(argv[i], "-v") == 0 ||
210  strcmp(argv[i], "--version") == 0)
211  {
213  exit(EXIT_SUCCESS);
214  }
215 
216  /* show usage if requested or for an unknown option */
217  if (strcmp(argv[i], "-h") == 0 ||
218  strcmp(argv[i], "--help") == 0)
219  {
220  usage_print(basename(argv[0]));
221  exit(EXIT_SUCCESS);
222  } else {
223  warnx("Error: unknown option '%s'\n", argv[i]);
224  usage_print(basename(argv[0]));
225  exit(EX_USAGE);
226  }
227  }
228 
229  if (only_protocol) {
230  result = m0_xcode_init();
231  } else {
233  result = m0_init(&instance);
234  }
235 
236  if (result != 0)
237  err(EX_CONFIG, "Cannot initialise motr: %d", result);
238  protocol_print(xcode_domain);
239 
240  if (only_protocol)
241  m0_xcode_fini();
242  else
243  m0_fini();
244  return EX_OK;
245 }
246 
247 #undef M0_TRACE_SUBSYSTEM
248 
251 /*
252  * Local variables:
253  * c-indentation-style: "K&R"
254  * c-basic-offset: 8
255  * tab-width: 8
256  * fill-column: 80
257  * scroll-step: 1
258  * End:
259  */
260 /*
261  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
262  */
#define NULL
Definition: misc.h:38
void m0_fini(void)
Definition: init.c:318
Definition: sm.h:350
static FILE * f
Definition: adieu.c:79
size_t xct_nr
Definition: xcode.h:343
struct m0_xcode_field xct_child[0]
Definition: xcode.h:345
int m0_init(struct m0 *instance)
Definition: init.c:310
static struct m0_xcode_type ** xt[]
Definition: protocol.c:64
#define PRIx64
Definition: types.h:61
static const char * xcode_domain_name
Definition: protocol.c:95
int i
Definition: dir.c:1033
void m0_build_info_print(void)
Definition: version.c:66
void m0_xcode_fini(void)
Definition: init.c:60
int m0_xcode_init(void)
Definition: init.c:43
struct crate_conf * conf
enum m0_xode_atom_type xct_atype
Definition: xcode.h:326
void m0_node_uuid_string_set(const char *uuid)
Definition: uuuid.c:64
static struct m0_stob_domain * dom
Definition: storage.c:38
Definition: instance.h:80
static const char * progname
Definition: traced.c:65
#define PRIi32
Definition: types.h:67
const char * m0_xcode_atom_type_name[M0_XAT_NR]
Definition: xcode.c:965
const char * m0_xcode_aggr_name[M0_XA_NR]
Definition: xcode.c:955
static void type_print(const struct m0_xcode_type *xt)
Definition: protocol.c:80
static void field_print(const struct m0_xcode_field *f, int i)
Definition: protocol.c:131
static int cmp(const void *p0, const void *p1)
Definition: protocol.c:123
enum m0_xcode_aggr xct_aggr
Definition: xcode.h:316
void(* m0_sm__conf_init)(const struct m0_sm_conf *conf)
Definition: protocol.c:137
static void protocol_print(int dom)
Definition: protocol.c:97
int main(int argc, char **argv)
Definition: protocol.c:153
void usage_print(const char *progname)
Definition: protocol.c:139
M0_INTERNAL void m0_sm_conf_print(const struct m0_sm_conf *conf)
Definition: sm.c:993
static struct m0 instance
Definition: main.c:78
size_t xct_sizeof
Definition: xcode.h:341
M0_INTERNAL void m0_trace_set_mmapped_buffer(bool val)
Definition: utrace.c:156
#define ARRAY_SIZE(a)
Definition: misc.h:45
uint32_t xct_flags
Definition: xcode.h:331
const char * xct_name
Definition: xcode.h:318