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