Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 1 | /* 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 | |
| 36 | /* we don't want to add this to a public header file; this is simply |
| 37 | * exported by libosmocore and used by libmsomvty but not for public |
| 38 | * consumption. */ |
| 39 | extern struct llist_head osmo_g_fsms; |
| 40 | |
| 41 | /*! \brief Print information about a FSM [class] to the given VTY |
| 42 | * \param vty The VTY to which to print |
| 43 | * \param[in] fsm The FSM class to print |
| 44 | */ |
| 45 | void vty_out_fsm(struct vty *vty, struct osmo_fsm *fsm) |
| 46 | { |
| 47 | unsigned int i; |
| 48 | const struct value_string *evt_name; |
| 49 | |
| 50 | vty_out(vty, "FSM Name: '%s', Log Subsys: '%s'%s", fsm->name, |
| 51 | log_category_name(fsm->log_subsys), VTY_NEWLINE); |
| 52 | /* list the events */ |
| 53 | for (evt_name = fsm->event_names; evt_name->str != NULL; evt_name++) { |
| 54 | vty_out(vty, " Event %02u (0x%08x): '%s'%s", evt_name->value, |
| 55 | (1 << evt_name->value), evt_name->str, VTY_NEWLINE); |
| 56 | } |
| 57 | /* list the states */ |
| 58 | vty_out(vty, " Number of States: %u%s", fsm->num_states, VTY_NEWLINE); |
| 59 | for (i = 0; i < fsm->num_states; i++) { |
| 60 | const struct osmo_fsm_state *state = &fsm->states[i]; |
| 61 | vty_out(vty, " State %-20s InEvtMask: 0x%08x, OutStateMask: 0x%08x%s", |
| 62 | state->name, state->in_event_mask, state->out_state_mask, |
| 63 | VTY_NEWLINE); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /*! \brief Print a FSM instance to the given VTY |
| 68 | * \param vty The VTY to which to print |
| 69 | * \param[in] fsmi The FSM instance to print |
| 70 | */ |
| 71 | void vty_out_fsm_inst(struct vty *vty, struct osmo_fsm_inst *fsmi) |
| 72 | { |
| 73 | struct osmo_fsm_inst *child; |
| 74 | |
| 75 | vty_out(vty, "FSM Instance Name: '%s', ID: '%s'%s", |
| 76 | fsmi->name, fsmi->id, VTY_NEWLINE); |
| 77 | vty_out(vty, " Log-Level: '%s', State: '%s'%s", |
| 78 | log_level_str(fsmi->log_level), |
| 79 | osmo_fsm_state_name(fsmi->fsm, fsmi->state), |
| 80 | VTY_NEWLINE); |
| 81 | if (fsmi->T) |
| 82 | vty_out(vty, " Timer: %u%s", fsmi->T, VTY_NEWLINE); |
| 83 | if (fsmi->proc.parent) { |
| 84 | vty_out(vty, " Parent: '%s', Term-Event: '%s'%s", |
| 85 | fsmi->proc.parent->name, |
| 86 | osmo_fsm_event_name(fsmi->proc.parent->fsm, |
| 87 | fsmi->proc.parent_term_event), |
| 88 | VTY_NEWLINE); |
| 89 | } |
| 90 | llist_for_each_entry(child, &fsmi->proc.children, list) { |
| 91 | vty_out(vty, " Child: '%s'%s", child->name, VTY_NEWLINE); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #define SH_FSM_STR SHOW_STR "Show information about finite state machines\n" |
| 96 | #define SH_FSMI_STR SHOW_STR "Show information about finite state machine instances\n" |
| 97 | |
| 98 | DEFUN(show_fsms, show_fsms_cmd, |
| 99 | "show fsm all", |
| 100 | SH_FSM_STR |
| 101 | "Display a list of all registered finite state machines\n") |
| 102 | { |
| 103 | struct osmo_fsm *fsm; |
| 104 | |
| 105 | llist_for_each_entry(fsm, &osmo_g_fsms, list) |
| 106 | vty_out_fsm(vty, fsm); |
| 107 | |
| 108 | return CMD_SUCCESS; |
| 109 | } |
| 110 | |
| 111 | DEFUN(show_fsm, show_fsm_cmd, |
| 112 | "show fsm NAME", |
| 113 | SH_FSM_STR |
| 114 | "Display information about a single named finite state machine\n") |
| 115 | { |
| 116 | struct osmo_fsm *fsm; |
| 117 | |
| 118 | fsm = osmo_fsm_find_by_name(argv[0]); |
| 119 | if (!fsm) { |
| 120 | vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s", |
| 121 | argv[0], VTY_NEWLINE); |
| 122 | return CMD_WARNING; |
| 123 | } |
| 124 | |
| 125 | vty_out_fsm(vty, fsm); |
| 126 | |
| 127 | return CMD_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | DEFUN(show_fsm_insts, show_fsm_insts_cmd, |
| 131 | "show fsm-instances all", |
| 132 | SH_FSMI_STR |
| 133 | "Display a list of all FSM instances of all finite state machine") |
| 134 | { |
| 135 | struct osmo_fsm *fsm; |
| 136 | |
| 137 | llist_for_each_entry(fsm, &osmo_g_fsms, list) { |
| 138 | struct osmo_fsm_inst *fsmi; |
| 139 | llist_for_each_entry(fsmi, &fsm->instances, list) |
| 140 | vty_out_fsm_inst(vty, fsmi); |
| 141 | } |
| 142 | |
| 143 | return CMD_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | DEFUN(show_fsm_inst, show_fsm_inst_cmd, |
| 147 | "show fsm-instances NAME", |
| 148 | SH_FSMI_STR |
| 149 | "Display a list of all FSM instances of the named finite state machine") |
| 150 | { |
| 151 | struct osmo_fsm *fsm; |
| 152 | struct osmo_fsm_inst *fsmi; |
| 153 | |
| 154 | fsm = osmo_fsm_find_by_name(argv[0]); |
| 155 | if (!fsm) { |
| 156 | vty_out(vty, "Error: FSM with name '%s' doesn't exist!%s", |
| 157 | argv[0], VTY_NEWLINE); |
| 158 | return CMD_WARNING; |
| 159 | } |
| 160 | |
| 161 | llist_for_each_entry(fsmi, &fsm->instances, list) |
| 162 | vty_out_fsm_inst(vty, fsmi); |
| 163 | |
| 164 | return CMD_SUCCESS; |
| 165 | } |
| 166 | |
| 167 | /*! \brief Install VTY commands for FSM introspection |
| 168 | * This installs a couple of VTY commands for introspection of FSM |
| 169 | * classes as well as FSM instances. Call this once from your |
| 170 | * application if you want to support those commands. */ |
| 171 | void osmo_fsm_vty_add_cmds(void) |
| 172 | { |
| 173 | install_element_ve(&show_fsm_cmd); |
| 174 | install_element_ve(&show_fsms_cmd); |
| 175 | install_element_ve(&show_fsm_inst_cmd); |
| 176 | install_element_ve(&show_fsm_insts_cmd); |
| 177 | } |