Initial commit of vpp code.
[vpp.git] / vnet / vnet / unix / gdb_funcs.c
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 #include <vppinfra/format.h>
16 #include <vlib/vlib.h>
17
18 #include <vlib/threads.h>
19
20 /* Functions to call from gdb */
21
22 u32 vl(void *p)
23 {
24   return vec_len (p);
25 }
26
27 uword pe (void *v)
28 {
29   return (pool_elts(v));
30 }
31
32 int pifi (void *p, u32 index)
33 {
34   return pool_is_free_index (p, index);
35 }
36
37 void debug_hex_bytes (u8 *s, u32 n)
38 {
39   fformat (stderr, "%U\n", format_hex_bytes, s, n);
40 }
41
42 void vlib_dump_frame_ownership (void)
43 {
44   vlib_main_t * vm = vlib_get_main();
45   vlib_node_main_t * nm = &vm->node_main;
46   vlib_node_runtime_t * this_node_runtime;
47   vlib_next_frame_t * nf;
48   u32 first_nf_index;
49   u32 index;
50     
51   vec_foreach(this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
52     {
53       first_nf_index = this_node_runtime->next_frame_index;
54
55       for (index = first_nf_index; index < first_nf_index + 
56              this_node_runtime->n_next_nodes; index++) 
57         {
58           vlib_node_runtime_t * owned_runtime;
59           nf = vec_elt_at_index (vm->node_main.next_frames, index);
60           if (nf->flags & VLIB_FRAME_OWNER) 
61             {
62               owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
63                                                 nf->node_runtime_index);
64               fformat(stderr, 
65                       "%s next index %d owns enqueue rights to %s\n",
66                       nm->nodes[this_node_runtime->node_index]->name, 
67                       index - first_nf_index, 
68                       nm->nodes[owned_runtime->node_index]->name);
69               fformat (stderr, "  nf index %d nf->frame_index %d\n",
70                        nf - vm->node_main.next_frames, 
71                        nf->frame_index);
72             }
73         }
74     }
75 }
76
77 void vlib_runtime_index_to_node_name (u32 index)
78 {
79   vlib_main_t * vm = vlib_get_main();
80   vlib_node_main_t * nm = &vm->node_main;
81     
82   if (index > vec_len (nm->nodes))
83     {
84       fformat(stderr, "%d out of range, max %d\n", vec_len(nm->nodes));
85       return;
86     }
87
88   fformat(stderr, "node runtime index %d name %s\n", index, nm->nodes[index]->name);
89 }
90
91
92 static clib_error_t *
93 show_gdb_command_fn (vlib_main_t * vm,
94                      unformat_input_t * input,
95                      vlib_cli_command_t * cmd)
96 {
97   vlib_cli_output (vm, "vl(p) returns vec_len(p)");
98   vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
99   vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
100   vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
101   vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
102   vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
103
104   return 0;
105 }
106
107 VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
108   .path = "show gdb",
109   .short_help = "Describe functions which can be called from gdb",
110   .function = show_gdb_command_fn,
111 };
112
113 /* Cafeteria plan, maybe you don't want these functions */
114 clib_error_t * 
115 gdb_func_init (vlib_main_t * vm) { return 0; } 
116
117 VLIB_INIT_FUNCTION (gdb_func_init);