FD.io VPP  v21.10.1-2-g0a485f517
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: pvh - Return vector header of vector
42  *
43  * @param *p - void - address of vector
44  *
45  * @return vh - vec_header_t, the vector header
46  *
47  */
49 pvh (void *p)
50 {
51  return _vec_find (p);
52 }
53 
54 
55 /**
56  * @brief GDB callable function: pe - call pool_elts - number of elements in a pool
57  *
58  * @param *v - void - address of pool
59  *
60  * @return number - uword
61  *
62  */
63 uword
64 pe (void *v)
65 {
66  return (pool_elts (v));
67 }
68 
69 /**
70  * @brief GDB callable function: ph - call pool_header - get pool header.
71  *
72  * @param *p - void - address of pool
73  *
74  * @return pool_header_t
75  *
76  */
78 ph (void *p)
79 {
80  return pool_header (p);
81 }
82 
83 /**
84  * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
85  *
86  * @param *p - void - address of pool
87  * @param *index - u32
88  *
89  * @return 0|1 - int
90  *
91  */
92 int
93 pifi (void *p, u32 index)
94 {
95  return pool_is_free_index (p, index);
96 }
97 
98 /**
99  * @brief GDB callable function: debug_hex_bytes - return formatted hex string
100  *
101  * @param *s - u8
102  * @param n - u32 - number of bytes to format
103  *
104  */
105 void
107 {
108  fformat (stderr, "%U\n", format_hex_bytes, s, n);
109 }
110 
111 /**
112  * @brief GDB callable function: vlib_dump_frame_ownership
113  *
114  */
115 void
117 {
120  vlib_node_runtime_t *this_node_runtime;
121  vlib_next_frame_t *nf;
122  u32 first_nf_index;
123  u32 index;
124 
125  vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
126  {
127  first_nf_index = this_node_runtime->next_frame_index;
128 
129  for (index = first_nf_index; index < first_nf_index +
130  this_node_runtime->n_next_nodes; index++)
131  {
132  vlib_node_runtime_t *owned_runtime;
134  if (nf->flags & VLIB_FRAME_OWNER)
135  {
136  owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
137  nf->node_runtime_index);
138  fformat (stderr,
139  "%s next index %d owns enqueue rights to %s\n",
140  nm->nodes[this_node_runtime->node_index]->name,
141  index - first_nf_index,
142  nm->nodes[owned_runtime->node_index]->name);
143  fformat (stderr, " nf index %d nf->frame %p\n",
144  nf - vm->node_main.next_frames, nf->frame);
145  }
146  }
147  }
148 }
149 
150 /**
151  * @brief GDB callable function: vlib_runtime_index_to_node_name
152  *
153  * Takes node index and will return the node name.
154  *
155  * @param index - u32
156  */
157 void
159 {
162 
163  if (index >= vec_len (nm->nodes))
164  {
165  fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
166  return;
167  }
168 
169  fformat (stderr, "node runtime index %d name %s\n", index,
170  nm->nodes[index]->name);
171 }
172 
173 void
174 gdb_show_errors (int verbose)
175 {
177  unformat_input_t input;
179 
180  if (verbose == 0)
181  unformat_init_string (&input, "verbose 0", 9);
182  else if (verbose == 1)
183  unformat_init_string (&input, "verbose 1", 9);
184  else
185  {
186  fformat (stderr, "verbose not 0 or 1\n");
187  return;
188  }
189 
190  vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
191  unformat_free (&input);
192 }
193 
194 void
195 gdb_show_session (int verbose)
196 {
198  unformat_input_t input;
200 
201  if (verbose == 0)
202  unformat_init_string (&input, "verbose 0", 9);
203  else if (verbose == 1)
204  unformat_init_string (&input, "verbose 1", 9);
205  else if (verbose == 2)
206  unformat_init_string (&input, "verbose 2", 9);
207  else
208  {
209  fformat (stderr, "verbose not 0 - 2\n");
210  return;
211  }
212 
213  vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
214  unformat_free (&input);
215 }
216 
217 static int
218 trace_cmp (void *a1, void *a2)
219 {
220  vlib_trace_header_t **t1 = a1;
221  vlib_trace_header_t **t2 = a2;
222  i64 dt = t1[0]->time - t2[0]->time;
223  return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
224 }
225 
226 void
228 {
229  vlib_trace_main_t *tm;
230  vlib_trace_header_t **h, **traces;
231  u32 i, index = 0;
232  char *fmt;
233  u8 *s = 0;
234  u32 max;
235 
236  /* By default display only this many traces. */
237  max = 50;
238 
239  /* Get active traces from pool. */
240 
242  {
243  fmt = "------------------- Start of thread %d %s -------------------\n";
245 
246  tm = &this_vlib_main->trace_main;
247 
248  trace_apply_filter (this_vlib_main);
249 
250  traces = 0;
252  {
253  vec_add1 (traces, h[0]);
254  }
255 
256  if (vec_len (traces) == 0)
257  {
258  s = format (s, "No packets in trace buffer\n");
259  goto done;
260  }
261 
262  /* Sort them by increasing time. */
264 
265  for (i = 0; i < vec_len (traces); i++)
266  {
267  if (i == max)
268  {
269  fformat (stderr,
270  "Limiting display to %d packets."
271  " To display more specify max.",
272  max);
273  goto done;
274  }
275 
276  s = format (s, "Packet %d\n%U\n\n", i + 1, format_vlib_trace,
277  vlib_get_first_main (), traces[i]);
278  }
279 
280  done:
281  vec_free (traces);
282 
283  index++;
284  }
285 
286  fformat (stderr, "%v", s);
287  vec_free (s);
288 }
289 
290 /**
291  * @brief GDB callable function: show_gdb_command_fn - show gdb
292  *
293  * Shows list of functions for VPP available in GDB
294  *
295  * @return error - clib_error_t
296  */
297 static clib_error_t *
299  unformat_input_t * input, vlib_cli_command_t * cmd)
300 {
301  vlib_cli_output (vm, "vl(p) returns vec_len(p)");
302  vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]");
303  vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]");
304  vlib_cli_output (vm, "vbi(b) returns b index");
306  "vgb(bi) returns vlib_get_buffer(vlib_get_main(), bi)");
307  vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
308  vlib_cli_output (vm, "ph(p) returns pool_header(p)");
309  vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
310  vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters");
311  vlib_cli_output (vm, "gdb_show_session dumps session counters");
312  vlib_cli_output (vm, "gdb_show_traces() dumps buffer traces");
313  vlib_cli_output (vm, "gdb_validate_buffer(b) check vlib_buffer b sanity");
314  vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
315  vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
316  vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
317 
318  return 0;
319 }
320 
321 /* *INDENT-OFF* */
323  .path = "show gdb",
324  .short_help = "Describe functions which can be called from gdb",
325  .function = show_gdb_command_fn,
326 };
327 /* *INDENT-ON* */
328 
330 vgb (u32 bi)
331 {
332  return vlib_get_buffer (vlib_get_main (), bi);
333 }
334 
336 vb (void *vb_arg)
337 {
338  vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
340 
341  rv = vnet_buffer (b);
342 
343  return rv;
344 }
345 
347 vb2 (void *vb_arg)
348 {
349  vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
351 
352  rv = vnet_buffer2 (b);
353 
354  return rv;
355 }
356 
357 u32
359 {
362  u32 bi = pointer_to_uword (b) - bm->buffer_mem_start;
364  return bi;
365 }
366 
367 int
369 {
371  u32 bi = vbi (b);
372  u8 *s =
374  if (s)
375  {
376  fformat (stderr, "gdb_validate_buffer(): %v", s);
377  return -1;
378  }
379  fformat (stderr, "gdb_validate_buffer(): no error found\n");
380  return 0;
381 }
382 
383 /**
384  * Dump a trajectory trace, reasonably easy to call from gdb
385  */
386 void
388 {
389 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
391  vlib_node_main_t *vnm = &vm->node_main;
392  vlib_buffer_t *b;
393  u16 *trace;
394  u8 i;
395 
396  b = vlib_get_buffer (vm, bi);
397 
398  trace = b->trajectory_trace;
399 
400  fformat (stderr, "Context trace for bi %d b 0x%llx, visited %d\n", bi, b,
401  b->trajectory_nb);
402 
403  for (i = 0; i < b->trajectory_nb; i++)
404  {
405  u32 node_index;
406 
407  node_index = trace[i];
408 
409  if (node_index >= vec_len (vnm->nodes))
410  {
411  fformat (stderr, "Skip bogus node index %d\n", node_index);
412  continue;
413  }
414 
415  fformat (stderr, "%v (%d)\n", vnm->nodes[node_index]->name, node_index);
416  }
417 #else
418  fformat (stderr, "in vlib/buffers.h, "
419  "#define VLIB_BUFFER_TRACE_TRAJECTORY 1\n");
420 
421 #endif
422 }
423 
424 void
426 {
427  fformat (stderr, "%U\n", format_vnet_buffer, b);
428 }
429 
430 /* Cafeteria plan, maybe you don't want these functions */
431 clib_error_t *
433 {
434  return 0;
435 }
436 
438 
439 /*
440  * fd.io coding-style-patch-verification: ON
441  *
442  * Local Variables:
443  * eval: (c-set-style "gnu")
444  * End:
445  */
vlib_buffer_main_t
Definition: buffer.h:479
vlib.h
pifi
int pifi(void *p, u32 index)
GDB callable function: pifi - call pool_is_free_index - is passed index free?
Definition: gdb_funcs.c:93
trace
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:870
trace_apply_filter
void trace_apply_filter(vlib_main_t *vm)
Definition: trace.c:240
format_vlib_trace
format_function_t format_vlib_trace
Definition: trace.h:114
vnet_buffer_opaque2_t
Definition: buffer.h:444
vbi
u32 vbi(vlib_buffer_t *b)
Definition: gdb_funcs.c:358
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
pointer_to_uword
static uword pointer_to_uword(const void *p)
Definition: types.h:131
name
string name[64]
Definition: fib.api:25
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
format_hex_bytes
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
threads.h
vb2
vnet_buffer_opaque2_t * vb2(void *vb_arg)
Definition: gdb_funcs.c:347
gdb_dump_trajectory_trace
void gdb_dump_trajectory_trace(u32 bi)
Dump a trajectory trace, reasonably easy to call from gdb.
Definition: gdb_funcs.c:387
vlib_cli_command_t::path
char * path
Definition: cli.h:96
show_gdb_command_fn
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:298
u16
unsigned short u16
Definition: types.h:57
vlib_main_t::node_main
vlib_node_main_t node_main
Definition: main.h:173
vlib_cli_show_session_command
vlib_cli_command_t vlib_cli_show_session_command
(constructor) VLIB_CLI_COMMAND (vlib_cli_show_session_command)
Definition: session_cli.c:670
vlib_node_runtime_t::next_frame_index
u32 next_frame_index
Start of next frames for this node.
Definition: node.h:475
vlib_main_t::buffer_main
vlib_buffer_main_t * buffer_main
Definition: main.h:167
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
node_index
node node_index
Definition: interface_output.c:440
gdb_show_session
void gdb_show_session(int verbose)
Definition: gdb_funcs.c:195
vnet_buffer2
#define vnet_buffer2(b)
Definition: buffer.h:505
unformat_input_t
struct _unformat_input_t unformat_input_t
gdb_show_traces
void gdb_show_traces()
Definition: gdb_funcs.c:227
vlib_worker_threads
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
h
h
Definition: flowhash_template.h:372
i64
signed long i64
Definition: types.h:78
ph
pool_header_t * ph(void *p)
GDB callable function: ph - call pool_header - get pool header.
Definition: gdb_funcs.c:78
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
vlib_node_main_t::next_frames
vlib_next_frame_t * next_frames
Definition: node.h:691
foreach_vlib_main
#define foreach_vlib_main()
Definition: threads.h:216
vlib_node_runtime_t::n_next_nodes
u16 n_next_nodes
Definition: node.h:495
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
vlib_validate_buffers
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
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
gdb_show_errors
void gdb_show_errors(int verbose)
Definition: gdb_funcs.c:174
vlib_next_frame_t::flags
u32 flags
Definition: node.h:402
trace_cmp
static int trace_cmp(void *a1, void *a2)
Definition: gdb_funcs.c:218
uword
u64 uword
Definition: types.h:112
format.h
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
VLIB_FRAME_OWNER
#define VLIB_FRAME_OWNER
Definition: node.h:413
vgb
vlib_buffer_t * vgb(u32 bi)
Definition: gdb_funcs.c:330
pe
uword pe(void *v)
GDB callable function: pe - call pool_elts - number of elements in a pool.
Definition: gdb_funcs.c:64
fmt
int cJSON_bool fmt
Definition: cJSON.h:160
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vlib_buffer_main_t::buffer_mem_start
uword buffer_mem_start
Definition: buffer.h:484
vlib_runtime_index_to_node_name
void vlib_runtime_index_to_node_name(u32 index)
GDB callable function: vlib_runtime_index_to_node_name.
Definition: gdb_funcs.c:158
vb
vnet_buffer_opaque_t * vb(void *vb_arg)
Definition: gdb_funcs.c:336
gdb_func_init
clib_error_t * gdb_func_init(vlib_main_t *vm)
Definition: gdb_funcs.c:432
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
index
u32 index
Definition: flow_types.api:221
vlib_dump_frame_ownership
void vlib_dump_frame_ownership(void)
GDB callable function: vlib_dump_frame_ownership.
Definition: gdb_funcs.c:116
vlib_node_runtime_t::node_index
u32 node_index
Node index.
Definition: node.h:478
vnet_buffer_opaque_t
Definition: buffer.h:153
unformat_init_string
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
vlib_trace_main_t::trace_buffer_pool
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:86
format
description fragment has unexpected format
Definition: map.api:433
pvh
vec_header_t * pvh(void *p)
GDB callable function: pvh - Return vector header of vector.
Definition: gdb_funcs.c:49
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vlib_next_frame_t::node_runtime_index
u32 node_runtime_index
Definition: node.h:399
vlib_get_first_main
static vlib_main_t * vlib_get_first_main(void)
Definition: global_funcs.h:44
CLIB_LOG2_CACHE_LINE_BYTES
#define CLIB_LOG2_CACHE_LINE_BYTES
Definition: cache.h:45
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
vl
u32 vl(void *p)
GDB callable function: vl - Return vector length of vector.
Definition: gdb_funcs.c:35
show_gdb_funcs_command
static vlib_cli_command_t show_gdb_funcs_command
(constructor) VLIB_CLI_COMMAND (show_gdb_funcs_command)
Definition: gdb_funcs.c:322
gdb_validate_buffer
int gdb_validate_buffer(vlib_buffer_t *b)
Definition: gdb_funcs.c:368
nm
nat44_ei_main_t * nm
Definition: nat44_ei_hairpinning.c:413
fformat
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:466
pool_header_t
Definition: pool.h:51
vec_header_t
vector header structure
Definition: vec_bootstrap.h:55
vec_sort_with_function
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1097
vlib_cli_command_t::function
vlib_cli_command_function_t * function
Definition: cli.h:103
vlib_main_t
Definition: main.h:102
pool_header
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:76
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
debug_hex_bytes
void debug_hex_bytes(u8 *s, u32 n)
GDB callable function: debug_hex_bytes - return formatted hex string.
Definition: gdb_funcs.c:106
vlib_trace_header_t
Definition: trace.h:45
i
int i
Definition: flowhash_template.h:376
vlib_trace_header_t::time
u64 time
Definition: trace.h:48
rv
int __clib_unused rv
Definition: application.c:491
VLIB_BUFFER_KNOWN_ALLOCATED
@ VLIB_BUFFER_KNOWN_ALLOCATED
Definition: buffer_funcs.h:520
vlib_cli_show_errors
vlib_cli_command_t vlib_cli_show_errors
(constructor) VLIB_CLI_COMMAND (vlib_cli_show_errors)
Definition: error.c:344
vlib_node_main_t
Definition: node.h:665
vnet.h
vlib_node_runtime_t
Definition: node.h:454
vlib_next_frame_t
Definition: node.h:393
vlib_node_main_t::nodes
vlib_node_t ** nodes
Definition: node.h:668
vlib_cli_command_t
Definition: cli.h:92
vlib_trace_main_t
Definition: trace.h:83
vlib_node_t::name
u8 * name
Definition: node.h:253
vlib_next_frame_t::frame
vlib_frame_t * frame
Definition: node.h:396
format_vnet_buffer
format_function_t format_vnet_buffer
Definition: buffer.h:521
gdb_dump_buffer
void gdb_dump_buffer(vlib_buffer_t *b)
Definition: gdb_funcs.c:425
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111