Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1 | //===-- CommandObjectWatchpointCommand.cpp ----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | // C++ Includes |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 12 | #include <vector> |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 13 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 16 | #include "CommandObjectWatchpointCommand.h" |
| 17 | #include "CommandObjectWatchpoint.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/IOHandler.h" |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 20 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 21 | #include "lldb/Target/Target.h" |
| 22 | #include "lldb/Target/Thread.h" |
| 23 | #include "lldb/Breakpoint/Watchpoint.h" |
| 24 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 25 | #include "lldb/Core/State.h" |
| 26 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
| 30 | //------------------------------------------------------------------------- |
| 31 | // CommandObjectWatchpointCommandAdd |
| 32 | //------------------------------------------------------------------------- |
| 33 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 34 | class CommandObjectWatchpointCommandAdd : |
| 35 | public CommandObjectParsed, |
| 36 | public IOHandlerDelegateMultiline |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 37 | { |
| 38 | public: |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 39 | CommandObjectWatchpointCommandAdd(CommandInterpreter &interpreter) |
| 40 | : CommandObjectParsed( |
| 41 | interpreter, "add", |
| 42 | "Add a set of LLDB commands to a watchpoint, to be executed whenever the watchpoint is hit.", nullptr), |
| 43 | IOHandlerDelegateMultiline("DONE", IOHandlerDelegate::Completion::LLDBCommand), |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 44 | m_options() |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 45 | { |
| 46 | SetHelpLong ( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 47 | R"( |
| 48 | General information about entering watchpoint commands |
| 49 | ------------------------------------------------------ |
| 50 | |
| 51 | )" "This command will prompt for commands to be executed when the specified \ |
| 52 | watchpoint is hit. Each command is typed on its own line following the '> ' \ |
| 53 | prompt until 'DONE' is entered." R"( |
| 54 | |
| 55 | )" "Syntactic errors may not be detected when initially entered, and many \ |
| 56 | malformed commands can silently fail when executed. If your watchpoint commands \ |
| 57 | do not appear to be executing, double-check the command syntax." R"( |
| 58 | |
| 59 | )" "Note: You may enter any debugger command exactly as you would at the debugger \ |
| 60 | prompt. There is no limit to the number of commands supplied, but do NOT enter \ |
| 61 | more than one command per line." R"( |
| 62 | |
| 63 | Special information about PYTHON watchpoint commands |
| 64 | ---------------------------------------------------- |
| 65 | |
| 66 | )" "You may enter either one or more lines of Python, including function \ |
| 67 | definitions or calls to functions that will have been imported by the time \ |
| 68 | the code executes. Single line watchpoint commands will be interpreted 'as is' \ |
| 69 | when the watchpoint is hit. Multiple lines of Python will be wrapped in a \ |
| 70 | generated function, and a call to the function will be attached to the watchpoint." R"( |
| 71 | |
| 72 | This auto-generated function is passed in three arguments: |
| 73 | |
| 74 | frame: an lldb.SBFrame object for the frame which hit the watchpoint. |
| 75 | |
| 76 | wp: the watchpoint that was hit. |
| 77 | |
| 78 | )" "When specifying a python function with the --python-function option, you need \ |
| 79 | to supply the function name prepended by the module name:" R"( |
| 80 | |
| 81 | --python-function myutils.watchpoint_callback |
| 82 | |
| 83 | The function itself must have the following prototype: |
| 84 | |
| 85 | def watchpoint_callback(frame, wp): |
| 86 | # Your code goes here |
| 87 | |
| 88 | )" "The arguments are the same as the arguments passed to generated functions as \ |
| 89 | described above. Note that the global variable 'lldb.frame' will NOT be updated when \ |
| 90 | this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ |
| 91 | can get you to the thread via frame.GetThread(), the thread can get you to the \ |
| 92 | process via thread.GetProcess(), and the process can get you back to the target \ |
| 93 | via process.GetTarget()." R"( |
| 94 | |
| 95 | )" "Important Note: As Python code gets collected into functions, access to global \ |
| 96 | variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ |
| 97 | Python syntax, including indentation, when entering Python watchpoint commands." R"( |
| 98 | |
| 99 | Example Python one-line watchpoint command: |
| 100 | |
| 101 | (lldb) watchpoint command add -s python 1 |
| 102 | Enter your Python command(s). Type 'DONE' to end. |
| 103 | > print "Hit this watchpoint!" |
| 104 | > DONE |
| 105 | |
| 106 | As a convenience, this also works for a short Python one-liner: |
| 107 | |
| 108 | (lldb) watchpoint command add -s python 1 -o 'import time; print time.asctime()' |
| 109 | (lldb) run |
| 110 | Launching '.../a.out' (x86_64) |
| 111 | (lldb) Fri Sep 10 12:17:45 2010 |
| 112 | Process 21778 Stopped |
| 113 | * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = watchpoint 1.1, queue = com.apple.main-thread |
| 114 | 36 |
| 115 | 37 int c(int val) |
| 116 | 38 { |
| 117 | 39 -> return val + 3; |
| 118 | 40 } |
| 119 | 41 |
| 120 | 42 int main (int argc, char const *argv[]) |
| 121 | |
| 122 | Example multiple line Python watchpoint command, using function definition: |
| 123 | |
| 124 | (lldb) watchpoint command add -s python 1 |
| 125 | Enter your Python command(s). Type 'DONE' to end. |
| 126 | > def watchpoint_output (wp_no): |
| 127 | > out_string = "Hit watchpoint number " + repr (wp_no) |
| 128 | > print out_string |
| 129 | > return True |
| 130 | > watchpoint_output (1) |
| 131 | > DONE |
| 132 | |
| 133 | Example multiple line Python watchpoint command, using 'loose' Python: |
| 134 | |
| 135 | (lldb) watchpoint command add -s p 1 |
| 136 | Enter your Python command(s). Type 'DONE' to end. |
| 137 | > global wp_count |
| 138 | > wp_count = wp_count + 1 |
| 139 | > print "Hit this watchpoint " + repr(wp_count) + " times!" |
| 140 | > DONE |
| 141 | |
| 142 | )" "In this case, since there is a reference to a global variable, \ |
| 143 | 'wp_count', you will also need to make sure 'wp_count' exists and is \ |
| 144 | initialized:" R"( |
| 145 | |
| 146 | (lldb) script |
| 147 | >>> wp_count = 0 |
| 148 | >>> quit() |
| 149 | |
| 150 | )" "Final Note: A warning that no watchpoint command was generated when there \ |
| 151 | are no syntax errors may indicate that a function was declared but never called." |
| 152 | ); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 153 | |
| 154 | CommandArgumentEntry arg; |
| 155 | CommandArgumentData wp_id_arg; |
| 156 | |
| 157 | // Define the first (and only) variant of this arg. |
| 158 | wp_id_arg.arg_type = eArgTypeWatchpointID; |
| 159 | wp_id_arg.arg_repetition = eArgRepeatPlain; |
| 160 | |
| 161 | // There is only one variant this argument could be; put it into the argument entry. |
| 162 | arg.push_back (wp_id_arg); |
| 163 | |
| 164 | // Push the data for the first argument into the m_arguments vector. |
| 165 | m_arguments.push_back (arg); |
| 166 | } |
| 167 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 168 | ~CommandObjectWatchpointCommandAdd() override = default; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 169 | |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 170 | Options * |
| 171 | GetOptions () override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 172 | { |
| 173 | return &m_options; |
| 174 | } |
| 175 | |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 176 | void |
| 177 | IOHandlerActivated (IOHandler &io_handler) override |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 178 | { |
| 179 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 180 | if (output_sp) |
| 181 | { |
| 182 | output_sp->PutCString("Enter your debugger command(s). Type 'DONE' to end.\n"); |
| 183 | output_sp->Flush(); |
| 184 | } |
| 185 | } |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 186 | |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 187 | void |
| 188 | IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 189 | { |
| 190 | io_handler.SetIsDone(true); |
| 191 | |
| 192 | // The WatchpointOptions object is owned by the watchpoint or watchpoint location |
| 193 | WatchpointOptions *wp_options = (WatchpointOptions *) io_handler.GetUserData(); |
| 194 | if (wp_options) |
| 195 | { |
| 196 | std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 197 | if (data_ap) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 198 | { |
| 199 | data_ap->user_source.SplitIntoLines(line); |
| 200 | BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); |
| 201 | wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 206 | void |
| 207 | CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, |
| 208 | CommandReturnObject &result) |
| 209 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 210 | m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt |
| 211 | *this, // IOHandlerDelegate |
| 212 | true, // Run IOHandler in async mode |
| 213 | wp_options); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /// Set a one-liner as the callback for the watchpoint. |
| 217 | void |
| 218 | SetWatchpointCommandCallback (WatchpointOptions *wp_options, |
| 219 | const char *oneliner) |
| 220 | { |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 221 | std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 222 | |
| 223 | // It's necessary to set both user_source and script_source to the oneliner. |
| 224 | // The former is used to generate callback description (as in watchpoint command list) |
| 225 | // while the latter is used for Python to interpret during the actual callback. |
| 226 | data_ap->user_source.AppendString (oneliner); |
| 227 | data_ap->script_source.assign (oneliner); |
| 228 | data_ap->stop_on_error = m_options.m_stop_on_error; |
| 229 | |
| 230 | BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); |
| 231 | wp_options->SetCallback (WatchpointOptionsCallbackFunction, baton_sp); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 232 | } |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 233 | |
| 234 | static bool |
| 235 | WatchpointOptionsCallbackFunction (void *baton, |
| 236 | StoppointCallbackContext *context, |
| 237 | lldb::user_id_t watch_id) |
| 238 | { |
| 239 | bool ret_value = true; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 240 | if (baton == nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 241 | return true; |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 242 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 243 | WatchpointOptions::CommandData *data = (WatchpointOptions::CommandData *) baton; |
| 244 | StringList &commands = data->user_source; |
| 245 | |
| 246 | if (commands.GetSize() > 0) |
| 247 | { |
| 248 | ExecutionContext exe_ctx (context->exe_ctx_ref); |
| 249 | Target *target = exe_ctx.GetTargetPtr(); |
| 250 | if (target) |
| 251 | { |
| 252 | CommandReturnObject result; |
| 253 | Debugger &debugger = target->GetDebugger(); |
| 254 | // Rig up the results secondary output stream to the debugger's, so the output will come out synchronously |
| 255 | // if the debugger is set up that way. |
| 256 | |
| 257 | StreamSP output_stream (debugger.GetAsyncOutputStream()); |
| 258 | StreamSP error_stream (debugger.GetAsyncErrorStream()); |
| 259 | result.SetImmediateOutputStream (output_stream); |
| 260 | result.SetImmediateErrorStream (error_stream); |
| 261 | |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 262 | CommandInterpreterRunOptions options; |
| 263 | options.SetStopOnContinue (true); |
| 264 | options.SetStopOnError (data->stop_on_error); |
| 265 | options.SetEchoCommands (false); |
| 266 | options.SetPrintResults (true); |
| 267 | options.SetAddToHistory (false); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 268 | |
| 269 | debugger.GetCommandInterpreter().HandleCommands (commands, |
| 270 | &exe_ctx, |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 271 | options, |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 272 | result); |
| 273 | result.GetImmediateOutputStream()->Flush(); |
| 274 | result.GetImmediateErrorStream()->Flush(); |
| 275 | } |
| 276 | } |
| 277 | return ret_value; |
| 278 | } |
| 279 | |
| 280 | class CommandOptions : public Options |
| 281 | { |
| 282 | public: |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 283 | CommandOptions() : |
| 284 | Options(), |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 285 | m_use_commands (false), |
| 286 | m_use_script_language (false), |
| 287 | m_script_language (eScriptLanguageNone), |
| 288 | m_use_one_liner (false), |
| 289 | m_one_liner(), |
| 290 | m_function_name() |
| 291 | { |
| 292 | } |
| 293 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 294 | ~CommandOptions() override = default; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 295 | |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 296 | Error |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 297 | SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 298 | ExecutionContext *execution_context) override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 299 | { |
| 300 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 301 | const int short_option = m_getopt_table[option_idx].val; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 302 | |
| 303 | switch (short_option) |
| 304 | { |
| 305 | case 'o': |
| 306 | m_use_one_liner = true; |
| 307 | m_one_liner = option_arg; |
| 308 | break; |
| 309 | |
| 310 | case 's': |
| 311 | m_script_language = (lldb::ScriptLanguage) Args::StringToOptionEnum (option_arg, |
| 312 | g_option_table[option_idx].enum_values, |
| 313 | eScriptLanguageNone, |
| 314 | error); |
| 315 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 316 | m_use_script_language = |
| 317 | (m_script_language == eScriptLanguagePython || m_script_language == eScriptLanguageDefault); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 318 | break; |
| 319 | |
| 320 | case 'e': |
| 321 | { |
| 322 | bool success = false; |
| 323 | m_stop_on_error = Args::StringToBoolean(option_arg, false, &success); |
| 324 | if (!success) |
| 325 | error.SetErrorStringWithFormat("invalid value for stop-on-error: \"%s\"", option_arg); |
| 326 | } |
| 327 | break; |
| 328 | |
| 329 | case 'F': |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 330 | m_use_one_liner = false; |
| 331 | m_use_script_language = true; |
| 332 | m_function_name.assign(option_arg); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 333 | break; |
| 334 | |
| 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | return error; |
| 339 | } |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 340 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 341 | void |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 342 | OptionParsingStarting(ExecutionContext *execution_context) override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 343 | { |
| 344 | m_use_commands = true; |
| 345 | m_use_script_language = false; |
| 346 | m_script_language = eScriptLanguageNone; |
| 347 | |
| 348 | m_use_one_liner = false; |
| 349 | m_stop_on_error = true; |
| 350 | m_one_liner.clear(); |
| 351 | m_function_name.clear(); |
| 352 | } |
| 353 | |
| 354 | const OptionDefinition* |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 355 | GetDefinitions () override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 356 | { |
| 357 | return g_option_table; |
| 358 | } |
| 359 | |
| 360 | // Options table: Required for subclasses of Options. |
| 361 | |
| 362 | static OptionDefinition g_option_table[]; |
| 363 | |
| 364 | // Instance variables to hold the values for command options. |
| 365 | |
| 366 | bool m_use_commands; |
| 367 | bool m_use_script_language; |
| 368 | lldb::ScriptLanguage m_script_language; |
| 369 | |
| 370 | // Instance variables to hold the values for one_liner options. |
| 371 | bool m_use_one_liner; |
| 372 | std::string m_one_liner; |
| 373 | bool m_stop_on_error; |
| 374 | std::string m_function_name; |
| 375 | }; |
| 376 | |
| 377 | protected: |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 378 | bool |
| 379 | DoExecute (Args& command, CommandReturnObject &result) override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 380 | { |
| 381 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
| 382 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 383 | if (target == nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 384 | { |
| 385 | result.AppendError ("There is not a current executable; there are no watchpoints to which to add commands"); |
| 386 | result.SetStatus (eReturnStatusFailed); |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | const WatchpointList &watchpoints = target->GetWatchpointList(); |
| 391 | size_t num_watchpoints = watchpoints.GetSize(); |
| 392 | |
| 393 | if (num_watchpoints == 0) |
| 394 | { |
| 395 | result.AppendError ("No watchpoints exist to have commands added"); |
| 396 | result.SetStatus (eReturnStatusFailed); |
| 397 | return false; |
| 398 | } |
| 399 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 400 | if (!m_options.m_use_script_language && !m_options.m_function_name.empty()) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 401 | { |
| 402 | result.AppendError ("need to enable scripting to have a function run as a watchpoint command"); |
| 403 | result.SetStatus (eReturnStatusFailed); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | std::vector<uint32_t> valid_wp_ids; |
Jim Ingham | b0b4513 | 2013-07-02 02:09:46 +0000 | [diff] [blame] | 408 | if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 409 | { |
| 410 | result.AppendError("Invalid watchpoints specification."); |
| 411 | result.SetStatus(eReturnStatusFailed); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 416 | const size_t count = valid_wp_ids.size(); |
| 417 | for (size_t i = 0; i < count; ++i) |
| 418 | { |
| 419 | uint32_t cur_wp_id = valid_wp_ids.at (i); |
| 420 | if (cur_wp_id != LLDB_INVALID_WATCH_ID) |
| 421 | { |
| 422 | Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); |
| 423 | // Sanity check wp first. |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 424 | if (wp == nullptr) continue; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 425 | |
| 426 | WatchpointOptions *wp_options = wp->GetOptions(); |
| 427 | // Skip this watchpoint if wp_options is not good. |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 428 | if (wp_options == nullptr) continue; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 429 | |
| 430 | // If we are using script language, get the script interpreter |
| 431 | // in order to set or collect command callback. Otherwise, call |
| 432 | // the methods associated with this object. |
| 433 | if (m_options.m_use_script_language) |
| 434 | { |
| 435 | // Special handling for one-liner specified inline. |
| 436 | if (m_options.m_use_one_liner) |
| 437 | { |
| 438 | m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options, |
| 439 | m_options.m_one_liner.c_str()); |
| 440 | } |
| 441 | // Special handling for using a Python function by name |
| 442 | // instead of extending the watchpoint callback data structures, we just automatize |
| 443 | // what the user would do manually: make their watchpoint command be a function call |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 444 | else if (!m_options.m_function_name.empty()) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 445 | { |
| 446 | std::string oneliner(m_options.m_function_name); |
| 447 | oneliner += "(frame, wp, internal_dict)"; |
| 448 | m_interpreter.GetScriptInterpreter()->SetWatchpointCommandCallback (wp_options, |
| 449 | oneliner.c_str()); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | m_interpreter.GetScriptInterpreter()->CollectDataForWatchpointCommandCallback (wp_options, |
| 454 | result); |
| 455 | } |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | // Special handling for one-liner specified inline. |
| 460 | if (m_options.m_use_one_liner) |
| 461 | SetWatchpointCommandCallback (wp_options, |
| 462 | m_options.m_one_liner.c_str()); |
| 463 | else |
| 464 | CollectDataForWatchpointCommandCallback (wp_options, |
| 465 | result); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return result.Succeeded(); |
| 471 | } |
| 472 | |
| 473 | private: |
| 474 | CommandOptions m_options; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 475 | }; |
| 476 | |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 477 | // FIXME: "script-type" needs to have its contents determined dynamically, so somebody can add a new scripting |
| 478 | // language to lldb and have it pickable here without having to change this enumeration by hand and rebuild lldb proper. |
| 479 | |
| 480 | static OptionEnumValueElement |
| 481 | g_script_option_enumeration[4] = |
| 482 | { |
| 483 | { eScriptLanguageNone, "command", "Commands are in the lldb command interpreter language"}, |
| 484 | { eScriptLanguagePython, "python", "Commands are in the Python language."}, |
| 485 | { eSortOrderByName, "default-script", "Commands are in the default scripting language."}, |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 486 | { 0, nullptr, nullptr } |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | OptionDefinition |
| 490 | CommandObjectWatchpointCommandAdd::CommandOptions::g_option_table[] = |
| 491 | { |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame^] | 492 | // clang-format off |
| 493 | {LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner, "Specify a one-line watchpoint command inline. Be sure to surround it with quotes."}, |
| 494 | {LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Specify whether watchpoint command execution should terminate on error."}, |
| 495 | {LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, g_script_option_enumeration, 0, eArgTypeNone, "Specify the language for the commands - if none is specified, the lldb command interpreter will be used."}, |
| 496 | {LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this watchpoint. Be sure to give a module name if appropriate."}, |
| 497 | {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr } |
| 498 | // clang-format on |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | //------------------------------------------------------------------------- |
| 502 | // CommandObjectWatchpointCommandDelete |
| 503 | //------------------------------------------------------------------------- |
| 504 | |
| 505 | class CommandObjectWatchpointCommandDelete : public CommandObjectParsed |
| 506 | { |
| 507 | public: |
| 508 | CommandObjectWatchpointCommandDelete (CommandInterpreter &interpreter) : |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 509 | CommandObjectParsed(interpreter, |
| 510 | "delete", |
| 511 | "Delete the set of commands from a watchpoint.", |
| 512 | nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 513 | { |
| 514 | CommandArgumentEntry arg; |
| 515 | CommandArgumentData wp_id_arg; |
| 516 | |
| 517 | // Define the first (and only) variant of this arg. |
| 518 | wp_id_arg.arg_type = eArgTypeWatchpointID; |
| 519 | wp_id_arg.arg_repetition = eArgRepeatPlain; |
| 520 | |
| 521 | // There is only one variant this argument could be; put it into the argument entry. |
| 522 | arg.push_back (wp_id_arg); |
| 523 | |
| 524 | // Push the data for the first argument into the m_arguments vector. |
| 525 | m_arguments.push_back (arg); |
| 526 | } |
| 527 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 528 | ~CommandObjectWatchpointCommandDelete() override = default; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 529 | |
| 530 | protected: |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 531 | bool |
| 532 | DoExecute (Args& command, CommandReturnObject &result) override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 533 | { |
| 534 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
| 535 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 536 | if (target == nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 537 | { |
| 538 | result.AppendError ("There is not a current executable; there are no watchpoints from which to delete commands"); |
| 539 | result.SetStatus (eReturnStatusFailed); |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | const WatchpointList &watchpoints = target->GetWatchpointList(); |
| 544 | size_t num_watchpoints = watchpoints.GetSize(); |
| 545 | |
| 546 | if (num_watchpoints == 0) |
| 547 | { |
| 548 | result.AppendError ("No watchpoints exist to have commands deleted"); |
| 549 | result.SetStatus (eReturnStatusFailed); |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | if (command.GetArgumentCount() == 0) |
| 554 | { |
| 555 | result.AppendError ("No watchpoint specified from which to delete the commands"); |
| 556 | result.SetStatus (eReturnStatusFailed); |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | std::vector<uint32_t> valid_wp_ids; |
Jim Ingham | b0b4513 | 2013-07-02 02:09:46 +0000 | [diff] [blame] | 561 | if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 562 | { |
| 563 | result.AppendError("Invalid watchpoints specification."); |
| 564 | result.SetStatus(eReturnStatusFailed); |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 569 | const size_t count = valid_wp_ids.size(); |
| 570 | for (size_t i = 0; i < count; ++i) |
| 571 | { |
| 572 | uint32_t cur_wp_id = valid_wp_ids.at (i); |
| 573 | if (cur_wp_id != LLDB_INVALID_WATCH_ID) |
| 574 | { |
| 575 | Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); |
| 576 | if (wp) |
| 577 | wp->ClearCallback(); |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", |
| 582 | cur_wp_id); |
| 583 | result.SetStatus (eReturnStatusFailed); |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | return result.Succeeded(); |
| 588 | } |
| 589 | }; |
| 590 | |
| 591 | //------------------------------------------------------------------------- |
| 592 | // CommandObjectWatchpointCommandList |
| 593 | //------------------------------------------------------------------------- |
| 594 | |
| 595 | class CommandObjectWatchpointCommandList : public CommandObjectParsed |
| 596 | { |
| 597 | public: |
| 598 | CommandObjectWatchpointCommandList (CommandInterpreter &interpreter) : |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 599 | CommandObjectParsed(interpreter, |
| 600 | "list", |
| 601 | "List the script or set of commands to be executed when the watchpoint is hit.", |
| 602 | nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 603 | { |
| 604 | CommandArgumentEntry arg; |
| 605 | CommandArgumentData wp_id_arg; |
| 606 | |
| 607 | // Define the first (and only) variant of this arg. |
| 608 | wp_id_arg.arg_type = eArgTypeWatchpointID; |
| 609 | wp_id_arg.arg_repetition = eArgRepeatPlain; |
| 610 | |
| 611 | // There is only one variant this argument could be; put it into the argument entry. |
| 612 | arg.push_back (wp_id_arg); |
| 613 | |
| 614 | // Push the data for the first argument into the m_arguments vector. |
| 615 | m_arguments.push_back (arg); |
| 616 | } |
| 617 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 618 | ~CommandObjectWatchpointCommandList() override = default; |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 619 | |
| 620 | protected: |
Bruce Mitchener | 13d21e9 | 2015-10-07 16:56:17 +0000 | [diff] [blame] | 621 | bool |
| 622 | DoExecute (Args& command, CommandReturnObject &result) override |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 623 | { |
| 624 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
| 625 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 626 | if (target == nullptr) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 627 | { |
| 628 | result.AppendError ("There is not a current executable; there are no watchpoints for which to list commands"); |
| 629 | result.SetStatus (eReturnStatusFailed); |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | const WatchpointList &watchpoints = target->GetWatchpointList(); |
| 634 | size_t num_watchpoints = watchpoints.GetSize(); |
| 635 | |
| 636 | if (num_watchpoints == 0) |
| 637 | { |
| 638 | result.AppendError ("No watchpoints exist for which to list commands"); |
| 639 | result.SetStatus (eReturnStatusFailed); |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | if (command.GetArgumentCount() == 0) |
| 644 | { |
| 645 | result.AppendError ("No watchpoint specified for which to list the commands"); |
| 646 | result.SetStatus (eReturnStatusFailed); |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | std::vector<uint32_t> valid_wp_ids; |
Jim Ingham | b0b4513 | 2013-07-02 02:09:46 +0000 | [diff] [blame] | 651 | if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, command, valid_wp_ids)) |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 652 | { |
| 653 | result.AppendError("Invalid watchpoints specification."); |
| 654 | result.SetStatus(eReturnStatusFailed); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 659 | const size_t count = valid_wp_ids.size(); |
| 660 | for (size_t i = 0; i < count; ++i) |
| 661 | { |
| 662 | uint32_t cur_wp_id = valid_wp_ids.at (i); |
| 663 | if (cur_wp_id != LLDB_INVALID_WATCH_ID) |
| 664 | { |
| 665 | Watchpoint *wp = target->GetWatchpointList().FindByID (cur_wp_id).get(); |
| 666 | |
| 667 | if (wp) |
| 668 | { |
| 669 | const WatchpointOptions *wp_options = wp->GetOptions(); |
| 670 | if (wp_options) |
| 671 | { |
| 672 | // Get the callback baton associated with the current watchpoint. |
| 673 | const Baton *baton = wp_options->GetBaton(); |
| 674 | if (baton) |
| 675 | { |
| 676 | result.GetOutputStream().Printf ("Watchpoint %u:\n", cur_wp_id); |
| 677 | result.GetOutputStream().IndentMore (); |
| 678 | baton->GetDescription(&result.GetOutputStream(), eDescriptionLevelFull); |
| 679 | result.GetOutputStream().IndentLess (); |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | result.AppendMessageWithFormat ("Watchpoint %u does not have an associated command.\n", |
| 684 | cur_wp_id); |
| 685 | } |
| 686 | } |
| 687 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | result.AppendErrorWithFormat("Invalid watchpoint ID: %u.\n", cur_wp_id); |
| 692 | result.SetStatus (eReturnStatusFailed); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return result.Succeeded(); |
| 698 | } |
| 699 | }; |
| 700 | |
| 701 | //------------------------------------------------------------------------- |
| 702 | // CommandObjectWatchpointCommand |
| 703 | //------------------------------------------------------------------------- |
| 704 | |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 705 | CommandObjectWatchpointCommand::CommandObjectWatchpointCommand(CommandInterpreter &interpreter) |
| 706 | : CommandObjectMultiword(interpreter, "command", "Commands for adding, removing and examining LLDB commands " |
| 707 | "executed when the watchpoint is hit (watchpoint 'commmands').", |
| 708 | "command <sub-command> [<sub-command-options>] <watchpoint-id>") |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 709 | { |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 710 | CommandObjectSP add_command_object (new CommandObjectWatchpointCommandAdd (interpreter)); |
| 711 | CommandObjectSP delete_command_object (new CommandObjectWatchpointCommandDelete (interpreter)); |
| 712 | CommandObjectSP list_command_object (new CommandObjectWatchpointCommandList (interpreter)); |
| 713 | |
| 714 | add_command_object->SetCommandName ("watchpoint command add"); |
| 715 | delete_command_object->SetCommandName ("watchpoint command delete"); |
| 716 | list_command_object->SetCommandName ("watchpoint command list"); |
| 717 | |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 718 | LoadSubCommand ("add", add_command_object); |
| 719 | LoadSubCommand ("delete", delete_command_object); |
| 720 | LoadSubCommand ("list", list_command_object); |
Johnny Chen | e9a5627 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Eugene Zelenko | 49bcfd8 | 2016-02-23 01:43:44 +0000 | [diff] [blame] | 723 | CommandObjectWatchpointCommand::~CommandObjectWatchpointCommand() = default; |