|
| 1 | +// systemtap interactive mode |
| 2 | +// Copyright (C) 2015 Red Hat Inc. |
| 3 | +// |
| 4 | +// This file is part of systemtap, and is free software. You can |
| 5 | +// redistribute it and/or modify it under the terms of the GNU General |
| 6 | +// Public License (GPL); either version 2, or (at your option) any |
| 7 | +// later version. |
| 8 | + |
| 9 | +#include "config.h" |
| 10 | +#include "interactive.h" |
| 11 | +#include "session.h" |
| 12 | +#include "util.h" |
| 13 | + |
| 14 | +#include "stap-probe.h" |
| 15 | + |
| 16 | +#include <cstdlib> |
| 17 | + |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +extern "C" { |
| 21 | +#include <unistd.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <readline/readline.h> |
| 24 | +#include <readline/history.h> |
| 25 | +} |
| 26 | + |
| 27 | +// FIXME: these declarations don't really belong here. |
| 28 | +extern int |
| 29 | +passes_0_4 (systemtap_session &s); |
| 30 | +extern int |
| 31 | +pass_5 (systemtap_session &s, vector<remote*> targets); |
| 32 | + |
| 33 | +static void |
| 34 | +interactive_cmds () |
| 35 | +{ |
| 36 | + cout << endl |
| 37 | + << "List of commands:\n\n" |
| 38 | + << "!help -- Print this command list\n" |
| 39 | + << "!set OPTION VALUE -- Set option value. Supported options are\n" |
| 40 | + << " 'keep_tmpdir', 'last_pass' and 'verbose'.\n" |
| 41 | + << "!show OPTION -- Show option value.\n" |
| 42 | + << "!quit -- Exit systemtap\n"; |
| 43 | +} |
| 44 | + |
| 45 | +// FIXME: this isn't very elegant, and will quickly become longer and |
| 46 | +// longer with more supported options. However, for now... |
| 47 | +static void |
| 48 | +handle_setshow_cmd (systemtap_session &s, vector<string> &tokens) |
| 49 | +{ |
| 50 | + bool set = (tokens[0] == "!set"); |
| 51 | + |
| 52 | + if ((set && tokens.size() != 3) || (!set && tokens.size() != 2)) |
| 53 | + { |
| 54 | + cout << endl |
| 55 | + << "Invalid command\n"; |
| 56 | + interactive_cmds(); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (tokens[1] == "keep_tmpdir") |
| 61 | + { |
| 62 | + if (set) |
| 63 | + s.keep_tmpdir = (tokens[2] != "0"); |
| 64 | + else |
| 65 | + cout << "keep_tmpdir: " << s.keep_tmpdir << endl; |
| 66 | + } |
| 67 | + else if (tokens[1] == "last_pass") |
| 68 | + { |
| 69 | + if (set) |
| 70 | + { |
| 71 | + char *end; |
| 72 | + long val; |
| 73 | + |
| 74 | + errno = 0; |
| 75 | + val = strtol (tokens[2].c_str(), &end, 10); |
| 76 | + if (errno != 0 || *end != '\0' || val < 1 || val > 5) |
| 77 | + cout << endl |
| 78 | + << "Invalid option value (should be 1-5)\n"; |
| 79 | + else |
| 80 | + s.last_pass = val; |
| 81 | + } |
| 82 | + else |
| 83 | + cout << "last_pass: " << s.last_pass << endl; |
| 84 | + } |
| 85 | + else if (tokens[1] == "verbose") |
| 86 | + { |
| 87 | + if (set) |
| 88 | + { |
| 89 | + char *end; |
| 90 | + long val; |
| 91 | + |
| 92 | + errno = 0; |
| 93 | + val = strtol (tokens[2].c_str(), &end, 10); |
| 94 | + if (errno != 0 || *end != '\0' || val < 0) |
| 95 | + cout << endl |
| 96 | + << "Invalid option value (should be greater than 0)\n"; |
| 97 | + else |
| 98 | + { |
| 99 | + s.verbose = val; |
| 100 | + for (unsigned i=0; i<5; i++) |
| 101 | + s.perpass_verbose[i] = val; |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + cout << "verbose: " << s.verbose << endl; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + cout << endl |
| 110 | + << "Invalid option name\n"; |
| 111 | + interactive_cmds(); |
| 112 | + } |
| 113 | + return; |
| 114 | +} |
| 115 | + |
| 116 | +// Interactive mode, passes 0 through 5 and back again. |
| 117 | +int |
| 118 | +interactive_mode (systemtap_session &s, vector<remote*> targets) |
| 119 | +{ |
| 120 | + int rc; |
| 121 | + string delimiters = " \t"; |
| 122 | + bool input_handled; |
| 123 | + |
| 124 | + while (1) |
| 125 | + { |
| 126 | + char *line_tmp = readline("stap> "); |
| 127 | + if (line_tmp && *line_tmp) |
| 128 | + add_history(line_tmp); |
| 129 | + else |
| 130 | + continue; |
| 131 | + |
| 132 | + string line = string(line_tmp); |
| 133 | + free(line_tmp); |
| 134 | + |
| 135 | + vector<string> tokens; |
| 136 | + tokenize(line, tokens, delimiters); |
| 137 | + |
| 138 | + input_handled = false; |
| 139 | + if (tokens.size()) |
| 140 | + { |
| 141 | + if (tokens[0] == "!quit") |
| 142 | + { |
| 143 | + input_handled = true; |
| 144 | + break; |
| 145 | + } |
| 146 | + else if (tokens[0] == "!help") |
| 147 | + { |
| 148 | + input_handled = true; |
| 149 | + interactive_cmds(); |
| 150 | + } |
| 151 | + else if (tokens[0] == "!set" || tokens[0] == "!show") |
| 152 | + { |
| 153 | + input_handled = true; |
| 154 | + handle_setshow_cmd(s, tokens); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // If it isn't a command, we assume it is a script to run. |
| 159 | + // |
| 160 | + // FIXME: Later this could be a line from a script that we have |
| 161 | + // to keep track of. |
| 162 | + if (!input_handled) |
| 163 | + { |
| 164 | + // Try creating a new systemtap session object so that we |
| 165 | + // don't get leftovers from the last script we compiled. |
| 166 | + systemtap_session* ss = s.clone(s.architecture, s.kernel_release); |
| 167 | + clog << "orig tmpdir: " << s.tmpdir |
| 168 | + << ", new tmpdir: " << ss->tmpdir << endl; |
| 169 | + |
| 170 | + ss->clear_script_data(); |
| 171 | + ss->cmdline_script = line; |
| 172 | + ss->have_script = true; |
| 173 | + rc = passes_0_4(*ss); |
| 174 | + |
| 175 | + // Run pass 5, if passes 0-4 worked. |
| 176 | + if (rc == 0 && ss->last_pass >= 5 && !pending_interrupts) |
| 177 | + rc = pass_5 (*ss, targets); |
| 178 | + ss->reset_tmp_dir(); |
| 179 | + } |
| 180 | + } |
| 181 | + return 0; |
| 182 | +} |
0 commit comments