blob: acc8ca60bd742a41256d4af71283baf5edc6e4f0 [file] [log] [blame]
Harald Welte34193912017-01-07 11:49:55 +01001/* Osmocom FSM introspection via VTY */
2/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "../../config.h"
25
26#include <osmocom/vty/command.h>
27#include <osmocom/vty/buffer.h>
28#include <osmocom/vty/vty.h>
29#include <osmocom/vty/telnet_interface.h>
30#include <osmocom/vty/misc.h>
31
32#include <osmocom/core/fsm.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/linuxlist.h>
35
Harald Welte96e2a002017-06-12 21:44:18 +020036/*! \addtogroup fsm
37 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 * VTY interface for Osmocom FSM
Harald Welte96e2a002017-06-12 21:44:18 +020039 *
40 * This is code implementing generic VTY access to Osmocom FSMs from
41 * libosmocore. This means that any application can expose all state
42 * of all instances of all registered FSM classes by calling a single
43 * command during startup: \ref osmo_fsm_vty_add_cmds
44 */
45
Harald Welte34193912017-01-07 11:49:55 +010046/* we don't want to add this to a public header file; this is simply
47 * exported by libosmocore and used by libmsomvty but not for public
48 * consumption. */
49extern struct llist_head osmo_g_fsms;
50
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! Print information about a FSM [class] to the given VTY
Harald Welte34193912017-01-07 11:49:55 +010052 * \param vty The VTY to which to print
53 * \param[in] fsm The FSM class to print
54 */
55void vty_out_fsm(struct vty *vty, struct osmo_fsm *fsm)
56{
57 unsigned int i;
58 const struct value_string *evt_name;
59
60 vty_out(vty, "FSM Name: '%s', Log Subsys: '%s'%s", fsm->name,
61 log_category_name(fsm->log_subsys), VTY_NEWLINE);
62 /* list the events */
63 for (evt_name = fsm->event_names; evt_name->str != NULL; evt_name++) {
64 vty_out(vty, " Event %02u (0x%08x): '%s'%s", evt_name->value,
65 (1 << evt_name->value), evt_name->str, VTY_NEWLINE);
66 }
67 /* list the states */
68 vty_out(vty, " Number of States: %u%s", fsm->num_states, VTY_NEWLINE);
69 for (i = 0; i < fsm->num_states; i++) {
70 const struct osmo_fsm_state *state = &fsm->states[i];
71 vty_out(vty, " State %-20s InEvtMask: 0x%08x, OutStateMask: 0x%08x%s",
72 state->name, state->in_event_mask, state->out_state_mask,
73 VTY_NEWLINE);
74 }
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! Print a FSM instance to the given VTY
Harald Welte34193912017-01-07 11:49:55 +010078 * \param vty The VTY to which to print
79 * \param[in] fsmi The FSM instance to print
80 */
81void vty_out_fsm_inst(struct vty *vty, struct osmo_fsm_inst *fsmi)
82{
83 struct osmo_fsm_inst *child;
84
85 vty_out(vty, "FSM Instance Name: '%s', ID: '%s'%s",
86 fsmi->name, fsmi->id, VTY_NEWLINE);
87 vty_out(vty, " Log-Level: '%s', State: '%s'%s",
88 log_level_str(fsmi->log_level),
89 osmo_fsm_state_name(fsmi->fsm, fsmi->state),
90 VTY_NEWLINE);
91 if (fsmi->T)
92 vty_out(vty, " Timer: %u%s", fsmi->T, VTY_NEWLINE);
93 if (fsmi->proc.parent) {
94 vty_out(vty, " Parent: '%s', Term-Event: '%s'%s",
95 fsmi->proc.parent->name,
96 osmo_fsm_event_name(fsmi->proc.parent->fsm,
97 fsmi->proc.parent_term_event),
98 VTY_NEWLINE);
99 }
100 llist_for_each_entry(child, &fsmi->proc.children, list) {
101 vty_out(vty, " Child: '%s'%s", child->name, VTY_NEWLINE);
102 }
103}
104
105#define SH_FSM_STR SHOW_STR "Show information about finite state machines\n"
106#define SH_FSMI_STR SHOW_STR "Show information about finite state machine instances\n"
107
108DEFUN(show_fsms, show_fsms_cmd,
109 "show fsm all",
110 SH_FSM_STR
111 "Display a list of all registered finite state machines\n")
112{
113 struct osmo_fsm *fsm;
114
115 llist_for_each_entry(fsm, &osmo_g_fsms, list)
116 vty_out_fsm(vty, fsm);
117
118 return CMD_SUCCESS;
119}
120
121DEFUN(show_fsm, show_fsm_cmd,
122 "show fsm NAME",
123 SH_FSM_STR
124 "Display information about a single named finite state machine\n")
125{
126 struct osmo_fsm *fsm;
127
128 fsm = osmo_fsm_find_by_name(argv[0]);
129 if (!fsm) {
130 vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s",
131 argv[0], VTY_NEWLINE);
132 return CMD_WARNING;
133 }
134
135 vty_out_fsm(vty, fsm);
136
137 return CMD_SUCCESS;
138}
139
140DEFUN(show_fsm_insts, show_fsm_insts_cmd,
141 "show fsm-instances all",
142 SH_FSMI_STR
143 "Display a list of all FSM instances of all finite state machine")
144{
145 struct osmo_fsm *fsm;
146
147 llist_for_each_entry(fsm, &osmo_g_fsms, list) {
148 struct osmo_fsm_inst *fsmi;
149 llist_for_each_entry(fsmi, &fsm->instances, list)
150 vty_out_fsm_inst(vty, fsmi);
151 }
152
153 return CMD_SUCCESS;
154}
155
156DEFUN(show_fsm_inst, show_fsm_inst_cmd,
157 "show fsm-instances NAME",
158 SH_FSMI_STR
159 "Display a list of all FSM instances of the named finite state machine")
160{
161 struct osmo_fsm *fsm;
162 struct osmo_fsm_inst *fsmi;
163
164 fsm = osmo_fsm_find_by_name(argv[0]);
165 if (!fsm) {
166 vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s",
167 argv[0], VTY_NEWLINE);
168 return CMD_WARNING;
169 }
170
171 llist_for_each_entry(fsmi, &fsm->instances, list)
172 vty_out_fsm_inst(vty, fsmi);
173
174 return CMD_SUCCESS;
175}
176
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200177/*! Install VTY commands for FSM introspection
Harald Welte34193912017-01-07 11:49:55 +0100178 * This installs a couple of VTY commands for introspection of FSM
179 * classes as well as FSM instances. Call this once from your
180 * application if you want to support those commands. */
181void osmo_fsm_vty_add_cmds(void)
182{
183 install_element_ve(&show_fsm_cmd);
184 install_element_ve(&show_fsms_cmd);
185 install_element_ve(&show_fsm_inst_cmd);
186 install_element_ve(&show_fsm_insts_cmd);
187}
Harald Welte96e2a002017-06-12 21:44:18 +0200188
189/*! @} */