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