FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
gdb_funcs.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief Host utility functions
18  */
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
21 
22 #include <vlib/threads.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/format.h>
25 
26 /**
27  * @brief GDB callable function: vl - Return vector length of vector
28  *
29  * @param *p - void - address of vector
30  *
31  * @return length - u32
32  *
33  */
34 u32
35 vl (void *p)
36 {
37  return vec_len (p);
38 }
39 
40 /**
41  * @brief GDB callable function: pe - call pool_elts - number of elements in a pool
42  *
43  * @param *v - void - address of pool
44  *
45  * @return number - uword
46  *
47  */
48 uword
49 pe (void *v)
50 {
51  return (pool_elts (v));
52 }
53 
54 /**
55  * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
56  *
57  * @param *p - void - address of pool
58  * @param *index - u32
59  *
60  * @return 0|1 - int
61  *
62  */
63 int
64 pifi (void *p, u32 index)
65 {
66  return pool_is_free_index (p, index);
67 }
68 
69 /**
70  * @brief GDB callable function: debug_hex_bytes - return formatted hex string
71  *
72  * @param *s - u8
73  * @param n - u32 - number of bytes to format
74  *
75  */
76 void
78 {
79  fformat (stderr, "%U\n", format_hex_bytes, s, n);
80 }
81 
82 /**
83  * @brief GDB callable function: vlib_dump_frame_ownership
84  *
85  */
86 void
88 {
90  vlib_node_main_t *nm = &vm->node_main;
91  vlib_node_runtime_t *this_node_runtime;
93  u32 first_nf_index;
94  u32 index;
95 
96  vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
97  {
98  first_nf_index = this_node_runtime->next_frame_index;
99 
100  for (index = first_nf_index; index < first_nf_index +
101  this_node_runtime->n_next_nodes; index++)
102  {
103  vlib_node_runtime_t *owned_runtime;
104  nf = vec_elt_at_index (vm->node_main.next_frames, index);
105  if (nf->flags & VLIB_FRAME_OWNER)
106  {
107  owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
108  nf->node_runtime_index);
109  fformat (stderr,
110  "%s next index %d owns enqueue rights to %s\n",
111  nm->nodes[this_node_runtime->node_index]->name,
112  index - first_nf_index,
113  nm->nodes[owned_runtime->node_index]->name);
114  fformat (stderr, " nf index %d nf->frame %p\n",
115  nf - vm->node_main.next_frames, nf->frame);
116  }
117  }
118  }
119 }
120 
121 /**
122  * @brief GDB callable function: vlib_runtime_index_to_node_name
123  *
124  * Takes node index and will return the node name.
125  *
126  * @param index - u32
127  */
128 void
130 {
132  vlib_node_main_t *nm = &vm->node_main;
133 
134  if (index >= vec_len (nm->nodes))
135  {
136  fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
137  return;
138  }
139 
140  fformat (stderr, "node runtime index %d name %s\n", index,
141  nm->nodes[index]->name);
142 }
143 
144 void
145 gdb_show_errors (int verbose)
146 {
148  unformat_input_t input;
150 
151  if (verbose == 0)
152  unformat_init_string (&input, "verbose 0", 9);
153  else if (verbose == 1)
154  unformat_init_string (&input, "verbose 1", 9);
155  else
156  {
157  fformat (stderr, "verbose not 0 or 1\n");
158  return;
159  }
160 
161  vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
162  unformat_free (&input);
163 }
164 
165 void
166 gdb_show_session (int verbose)
167 {
169  unformat_input_t input;
171 
172  if (verbose == 0)
173  unformat_init_string (&input, "verbose 0", 9);
174  else if (verbose == 1)
175  unformat_init_string (&input, "verbose 1", 9);
176  else if (verbose == 2)
177  unformat_init_string (&input, "verbose 2", 9);
178  else
179  {
180  fformat (stderr, "verbose not 0 - 2\n");
181  return;
182  }
183 
184  vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
185  unformat_free (&input);
186 }
187 
188 static int
189 trace_cmp (void *a1, void *a2)
190 {
191  vlib_trace_header_t **t1 = a1;
192  vlib_trace_header_t **t2 = a2;
193  i64 dt = t1[0]->time - t2[0]->time;
194  return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
195 }
196 
197 void
199 {
200  vlib_trace_main_t *tm;
201  vlib_trace_header_t **h, **traces;
202  u32 i, index = 0;
203  char *fmt;
204  u8 *s = 0;
205  u32 max;
206 
207  /* By default display only this many traces. */
208  max = 50;
209 
210  /* Get active traces from pool. */
211 
212  /* *INDENT-OFF* */
214  ({
215  fmt = "------------------- Start of thread %d %s -------------------\n";
216  s = format (s, fmt, index, vlib_worker_threads[index].name);
217 
218  tm = &this_vlib_main->trace_main;
219 
220  trace_apply_filter(this_vlib_main);
221 
222  traces = 0;
224  ({
225  vec_add1 (traces, h[0]);
226  }));
227 
228  if (vec_len (traces) == 0)
229  {
230  s = format (s, "No packets in trace buffer\n");
231  goto done;
232  }
233 
234  /* Sort them by increasing time. */
236 
237  for (i = 0; i < vec_len (traces); i++)
238  {
239  if (i == max)
240  {
241  fformat (stderr, "Limiting display to %d packets."
242  " To display more specify max.", max);
243  goto done;
244  }
245 
246  s = format (s, "Packet %d\n%U\n\n", i + 1,
247  format_vlib_trace, vlib_mains[0], traces[i]);
248  }
249 
250  done:
251  vec_free (traces);
252 
253  index++;
254  }));
255  /* *INDENT-ON* */
256 
257  fformat (stderr, "%v", s);
258  vec_free (s);
259 }
260 
261 /**
262  * @brief GDB callable function: show_gdb_command_fn - show gdb
263  *
264  * Shows list of functions for VPP available in GDB
265  *
266  * @return error - clib_error_t
267  */
268 static clib_error_t *
270  unformat_input_t * input, vlib_cli_command_t * cmd)
271 {
272  vlib_cli_output (vm, "vl(p) returns vec_len(p)");
273  vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]");
274  vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]");
275  vlib_cli_output (vm, "vbi(b) returns b index");
276  vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
277  vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
278  vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters");
279  vlib_cli_output (vm, "gdb_show_session dumps session counters");
280  vlib_cli_output (vm, "gdb_show_traces() dumps buffer traces");
281  vlib_cli_output (vm, "gdb_validate_buffer(b) check vlib_buffer b sanity");
282  vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
283  vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
284  vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
285 
286  return 0;
287 }
288 
289 /* *INDENT-OFF* */
290 VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
291  .path = "show gdb",
292  .short_help = "Describe functions which can be called from gdb",
293  .function = show_gdb_command_fn,
294 };
295 /* *INDENT-ON* */
296 
298 vb (void *vb_arg)
299 {
300  vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
302 
303  rv = vnet_buffer (b);
304 
305  return rv;
306 }
307 
309 vb2 (void *vb_arg)
310 {
311  vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
313 
314  rv = vnet_buffer2 (b);
315 
316  return rv;
317 }
318 
319 u32
321 {
324  u32 bi = pointer_to_uword (b) - bm->buffer_mem_start;
326  return bi;
327 }
328 
329 int
331 {
333  u32 bi = vbi (b);
334  u8 *s =
336  if (s)
337  {
338  fformat (stderr, "gdb_validate_buffer(): %v", s);
339  return -1;
340  }
341  fformat (stderr, "gdb_validate_buffer(): no error found\n");
342  return 0;
343 }
344 
345 /* Cafeteria plan, maybe you don't want these functions */
346 clib_error_t *
348 {
349  return 0;
350 }
351 
353 
354 /*
355  * fd.io coding-style-patch-verification: ON
356  *
357  * Local Variables:
358  * eval: (c-set-style "gnu")
359  * End:
360  */
void vlib_runtime_index_to_node_name(u32 index)
GDB callable function: vlib_runtime_index_to_node_name.
Definition: gdb_funcs.c:129
u32 next_frame_index
Start of next frames for this node.
Definition: node.h:493
#define vnet_buffer2(b)
Definition: buffer.h:467
#define CLIB_LOG2_CACHE_LINE_BYTES
Definition: cache.h:50
u32 vbi(vlib_buffer_t *b)
Definition: gdb_funcs.c:320
int i
vlib_buffer_main_t * buffer_main
Definition: main.h:152
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
void vlib_dump_frame_ownership(void)
GDB callable function: vlib_dump_frame_ownership.
Definition: gdb_funcs.c:87
void gdb_show_traces()
Definition: gdb_funcs.c:198
vlib_main_t ** vlib_mains
Definition: buffer.c:332
unsigned char u8
Definition: types.h:56
vnet_buffer_opaque_t * vb(void *vb_arg)
Definition: gdb_funcs.c:298
void gdb_show_session(int verbose)
Definition: gdb_funcs.c:166
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:86
void debug_hex_bytes(u8 *s, u32 n)
GDB callable function: debug_hex_bytes - return formatted hex string.
Definition: gdb_funcs.c:77
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vlib_node_t ** nodes
Definition: node.h:699
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
unsigned int u32
Definition: types.h:88
vlib_node_runtime_t * nodes_by_type[VLIB_N_NODE_TYPE]
Definition: node.h:709
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:37
vlib_cli_command_t vlib_cli_show_errors
(constructor) VLIB_CLI_COMMAND (vlib_cli_show_errors)
Definition: error.c:285
vlib_cli_command_function_t * function
Definition: cli.h:102
format_function_t format_vlib_trace
Definition: trace.h:113
vlib_cli_command_t vlib_cli_show_session_command
(constructor) VLIB_CLI_COMMAND (vlib_cli_show_session_command)
Definition: session_cli.c:632
static clib_error_t * show_gdb_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
GDB callable function: show_gdb_command_fn - show gdb.
Definition: gdb_funcs.c:269
struct _unformat_input_t unformat_input_t
signed long i64
Definition: types.h:78
u32 node_index
Node index.
Definition: node.h:496
#define foreach_vlib_main(body)
Definition: threads.h:241
vlib_main_t * vm
Definition: in2out_ed.c:1810
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
u8 * name
Definition: node.h:264
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:284
vnet_buffer_opaque2_t * vb2(void *vb_arg)
Definition: gdb_funcs.c:309
string name[64]
Definition: ip.api:44
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
clib_error_t * gdb_func_init(vlib_main_t *vm)
Definition: gdb_funcs.c:347
int gdb_validate_buffer(vlib_buffer_t *b)
Definition: gdb_funcs.c:330
vlib_frame_t * frame
Definition: node.h:406
int pifi(void *p, u32 index)
GDB callable function: pifi - call pool_is_free_index - is passed index free?
Definition: gdb_funcs.c:64
uword pe(void *v)
GDB callable function: pe - call pool_elts - number of elements in a pool.
Definition: gdb_funcs.c:49
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define VLIB_FRAME_OWNER
Definition: node.h:423
u32 vl(void *p)
GDB callable function: vl - Return vector length of vector.
Definition: gdb_funcs.c:35
static int trace_cmp(void *a1, void *a2)
Definition: gdb_funcs.c:189
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_node_main_t node_main
Definition: main.h:158
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
vlib_next_frame_t * next_frames
Definition: node.h:722
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:983
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
uword buffer_mem_start
Definition: buffer.h:452
u8 * vlib_validate_buffers(vlib_main_t *vm, u32 *buffers, uword next_buffer_stride, uword n_buffers, vlib_buffer_known_state_t known_state, uword follow_buffer_next)
Definition: buffer.c:261
#define vnet_buffer(b)
Definition: buffer.h:408
#define vec_foreach(var, vec)
Vector iterator.
u32 node_runtime_index
Definition: node.h:409
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
void trace_apply_filter(vlib_main_t *vm)
Definition: trace.c:226
void gdb_show_errors(int verbose)
Definition: gdb_funcs.c:145
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128