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: pe - call pool_elts - number of elements in a pool
43 * @param *v - void - address of pool
45 * @return number - uword
51 return (pool_elts (v));
55 * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
57 * @param *p - void - address of pool
64 pifi (void *p, u32 index)
66 return pool_is_free_index (p, index);
70 * @brief GDB callable function: debug_hex_bytes - return formatted hex string
73 * @param n - u32 - number of bytes to format
77 debug_hex_bytes (u8 * s, u32 n)
79 fformat (stderr, "%U\n", format_hex_bytes, s, n);
83 * @brief GDB callable function: vlib_dump_frame_ownership
87 vlib_dump_frame_ownership (void)
89 vlib_main_t *vm = vlib_get_main ();
90 vlib_node_main_t *nm = &vm->node_main;
91 vlib_node_runtime_t *this_node_runtime;
92 vlib_next_frame_t *nf;
96 vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
98 first_nf_index = this_node_runtime->next_frame_index;
100 for (index = first_nf_index; index < first_nf_index +
101 this_node_runtime->n_next_nodes; index++)
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)
107 owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
108 nf->node_runtime_index);
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);
122 * @brief GDB callable function: vlib_runtime_index_to_node_name
124 * Takes node index and will return the node name.
129 vlib_runtime_index_to_node_name (u32 index)
131 vlib_main_t *vm = vlib_get_main ();
132 vlib_node_main_t *nm = &vm->node_main;
134 if (index >= vec_len (nm->nodes))
136 fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
140 fformat (stderr, "node runtime index %d name %s\n", index,
141 nm->nodes[index]->name);
145 gdb_show_errors (int verbose)
147 extern vlib_cli_command_t vlib_cli_show_errors;
148 unformat_input_t input;
149 vlib_main_t *vm = vlib_get_main ();
152 unformat_init_string (&input, "verbose 0", 9);
153 else if (verbose == 1)
154 unformat_init_string (&input, "verbose 1", 9);
157 fformat (stderr, "verbose not 0 or 1\n");
161 vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
162 unformat_free (&input);
166 gdb_show_session (int verbose)
168 extern vlib_cli_command_t vlib_cli_show_session_command;
169 unformat_input_t input;
170 vlib_main_t *vm = vlib_get_main ();
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);
180 fformat (stderr, "verbose not 0 - 2\n");
184 vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
185 unformat_free (&input);
189 trace_cmp (void *a1, void *a2)
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);
200 vlib_trace_main_t *tm;
201 vlib_trace_header_t **h, **traces;
207 /* By default display only this many traces. */
210 /* Get active traces from pool. */
215 fmt = "------------------- Start of thread %d %s -------------------\n";
216 s = format (s, fmt, index, vlib_worker_threads[index].name);
218 tm = &this_vlib_main->trace_main;
220 trace_apply_filter(this_vlib_main);
223 pool_foreach (h, tm->trace_buffer_pool,
225 vec_add1 (traces, h[0]);
228 if (vec_len (traces) == 0)
230 s = format (s, "No packets in trace buffer\n");
234 /* Sort them by increasing time. */
235 vec_sort_with_function (traces, trace_cmp);
237 for (i = 0; i < vec_len (traces); i++)
241 fformat (stderr, "Limiting display to %d packets."
242 " To display more specify max.", max);
246 s = format (s, "Packet %d\n%U\n\n", i + 1,
247 format_vlib_trace, vlib_mains[0], traces[i]);
257 fformat (stderr, "%v", s);
262 * @brief GDB callable function: show_gdb_command_fn - show gdb
264 * Shows list of functions for VPP available in GDB
266 * @return error - clib_error_t
268 static clib_error_t *
269 show_gdb_command_fn (vlib_main_t * vm,
270 unformat_input_t * input, vlib_cli_command_t * cmd)
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");
290 VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
292 .short_help = "Describe functions which can be called from gdb",
293 .function = show_gdb_command_fn,
297 vnet_buffer_opaque_t *
300 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
301 vnet_buffer_opaque_t *rv;
303 rv = vnet_buffer (b);
308 vnet_buffer_opaque2_t *
311 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
312 vnet_buffer_opaque2_t *rv;
314 rv = vnet_buffer2 (b);
320 vbi (vlib_buffer_t * b)
322 vlib_main_t *vm = vlib_get_main ();
323 vlib_buffer_main_t *bm = vm->buffer_main;
324 u32 bi = pointer_to_uword (b) - bm->buffer_mem_start;
325 bi >>= CLIB_LOG2_CACHE_LINE_BYTES;
330 gdb_validate_buffer (vlib_buffer_t * b)
332 vlib_main_t *vm = vlib_get_main ();
335 vlib_validate_buffers (vm, &bi, 0, 1, VLIB_BUFFER_KNOWN_ALLOCATED, 1);
338 fformat (stderr, "gdb_validate_buffer(): %v", s);
341 fformat (stderr, "gdb_validate_buffer(): no error found\n");
345 /* Cafeteria plan, maybe you don't want these functions */
347 gdb_func_init (vlib_main_t * vm)
352 VLIB_INIT_FUNCTION (gdb_func_init);
355 * fd.io coding-style-patch-verification: ON
358 * eval: (c-set-style "gnu")