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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief Host utility functions
19 #include <vppinfra/format.h>
20 #include <vlib/vlib.h>
22 #include <vlib/threads.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/format.h>
27 * @brief GDB callable function: vl - Return vector length of vector
29 * @param *p - void - address of vector
31 * @return length - u32
41 * @brief GDB callable function: pvh - Return vector header of vector
43 * @param *p - void - address of vector
45 * @return vh - vec_header_t, the vector header
56 * @brief GDB callable function: pe - call pool_elts - number of elements in a pool
58 * @param *v - void - address of pool
60 * @return number - uword
66 return (pool_elts (v));
70 * @brief GDB callable function: ph - call pool_header - get pool header.
72 * @param *p - void - address of pool
74 * @return pool_header_t
80 return pool_header (p);
84 * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
86 * @param *p - void - address of pool
93 pifi (void *p, u32 index)
95 return pool_is_free_index (p, index);
99 * @brief GDB callable function: debug_hex_bytes - return formatted hex string
102 * @param n - u32 - number of bytes to format
106 debug_hex_bytes (u8 * s, u32 n)
108 fformat (stderr, "%U\n", format_hex_bytes, s, n);
112 * @brief GDB callable function: vlib_dump_frame_ownership
116 vlib_dump_frame_ownership (void)
118 vlib_main_t *vm = vlib_get_main ();
119 vlib_node_main_t *nm = &vm->node_main;
120 vlib_node_runtime_t *this_node_runtime;
121 vlib_next_frame_t *nf;
125 vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
127 first_nf_index = this_node_runtime->next_frame_index;
129 for (index = first_nf_index; index < first_nf_index +
130 this_node_runtime->n_next_nodes; index++)
132 vlib_node_runtime_t *owned_runtime;
133 nf = vec_elt_at_index (vm->node_main.next_frames, index);
134 if (nf->flags & VLIB_FRAME_OWNER)
136 owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
137 nf->node_runtime_index);
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);
151 * @brief GDB callable function: vlib_runtime_index_to_node_name
153 * Takes node index and will return the node name.
158 vlib_runtime_index_to_node_name (u32 index)
160 vlib_main_t *vm = vlib_get_main ();
161 vlib_node_main_t *nm = &vm->node_main;
163 if (index >= vec_len (nm->nodes))
165 fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
169 fformat (stderr, "node runtime index %d name %s\n", index,
170 nm->nodes[index]->name);
174 gdb_show_errors (int verbose)
176 extern vlib_cli_command_t vlib_cli_show_errors;
177 unformat_input_t input;
178 vlib_main_t *vm = vlib_get_main ();
181 unformat_init_string (&input, "verbose 0", 9);
182 else if (verbose == 1)
183 unformat_init_string (&input, "verbose 1", 9);
186 fformat (stderr, "verbose not 0 or 1\n");
190 vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
191 unformat_free (&input);
195 gdb_show_session (int verbose)
197 extern vlib_cli_command_t vlib_cli_show_session_command;
198 unformat_input_t input;
199 vlib_main_t *vm = vlib_get_main ();
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);
209 fformat (stderr, "verbose not 0 - 2\n");
213 vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
214 unformat_free (&input);
218 trace_cmp (void *a1, void *a2)
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);
229 vlib_trace_main_t *tm;
230 vlib_trace_header_t **h, **traces;
236 /* By default display only this many traces. */
239 /* Get active traces from pool. */
243 fmt = "------------------- Start of thread %d %s -------------------\n";
244 s = format (s, fmt, index, vlib_worker_threads[index].name);
246 tm = &this_vlib_main->trace_main;
248 trace_apply_filter (this_vlib_main);
251 pool_foreach (h, tm->trace_buffer_pool)
253 vec_add1 (traces, h[0]);
256 if (vec_len (traces) == 0)
258 s = format (s, "No packets in trace buffer\n");
262 /* Sort them by increasing time. */
263 vec_sort_with_function (traces, trace_cmp);
265 for (i = 0; i < vec_len (traces); i++)
270 "Limiting display to %d packets."
271 " To display more specify max.",
276 s = format (s, "Packet %d\n%U\n\n", i + 1, format_vlib_trace,
277 vlib_get_first_main (), traces[i]);
286 fformat (stderr, "%v", s);
291 * @brief GDB callable function: show_gdb_command_fn - show gdb
293 * Shows list of functions for VPP available in GDB
295 * @return error - clib_error_t
297 static clib_error_t *
298 show_gdb_command_fn (vlib_main_t * vm,
299 unformat_input_t * input, vlib_cli_command_t * cmd)
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");
322 VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
324 .short_help = "Describe functions which can be called from gdb",
325 .function = show_gdb_command_fn,
332 return vlib_get_buffer (vlib_get_main (), bi);
335 vnet_buffer_opaque_t *
338 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
339 vnet_buffer_opaque_t *rv;
341 rv = vnet_buffer (b);
346 vnet_buffer_opaque2_t *
349 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
350 vnet_buffer_opaque2_t *rv;
352 rv = vnet_buffer2 (b);
358 vbi (vlib_buffer_t * b)
360 vlib_main_t *vm = vlib_get_main ();
361 vlib_buffer_main_t *bm = vm->buffer_main;
362 u32 bi = pointer_to_uword (b) - bm->buffer_mem_start;
363 bi >>= CLIB_LOG2_CACHE_LINE_BYTES;
368 gdb_validate_buffer (vlib_buffer_t * b)
370 vlib_main_t *vm = vlib_get_main ();
373 vlib_validate_buffers (vm, &bi, 0, 1, VLIB_BUFFER_KNOWN_ALLOCATED, 1);
376 fformat (stderr, "gdb_validate_buffer(): %v", s);
379 fformat (stderr, "gdb_validate_buffer(): no error found\n");
384 * Dump a trajectory trace, reasonably easy to call from gdb
387 gdb_dump_trajectory_trace (u32 bi)
389 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
390 vlib_main_t *vm = vlib_get_main ();
391 vlib_node_main_t *vnm = &vm->node_main;
396 b = vlib_get_buffer (vm, bi);
398 trace = b->trajectory_trace;
400 fformat (stderr, "Context trace for bi %d b 0x%llx, visited %d\n", bi, b,
403 for (i = 0; i < b->trajectory_nb; i++)
407 node_index = trace[i];
409 if (node_index >= vec_len (vnm->nodes))
411 fformat (stderr, "Skip bogus node index %d\n", node_index);
415 fformat (stderr, "%v (%d)\n", vnm->nodes[node_index]->name, node_index);
418 fformat (stderr, "in vlib/buffers.h, "
419 "#define VLIB_BUFFER_TRACE_TRAJECTORY 1\n");
424 /* Cafeteria plan, maybe you don't want these functions */
426 gdb_func_init (vlib_main_t * vm)
431 VLIB_INIT_FUNCTION (gdb_func_init);
434 * fd.io coding-style-patch-verification: ON
437 * eval: (c-set-style "gnu")