Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectArgs.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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 14 | #include "CommandObjectArgs.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/Args.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Debugger.h" |
| 17 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Value.h" |
Sean Callanan | 30e3397 | 2015-09-03 00:48:23 +0000 | [diff] [blame] | 19 | #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Host/Host.h" |
| 21 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandReturnObject.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/ObjectFile.h" |
| 25 | #include "lldb/Symbol/Variable.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 +0000 | [diff] [blame] | 26 | #include "lldb/Target/ABI.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Target/Process.h" |
| 28 | #include "lldb/Target/Target.h" |
| 29 | #include "lldb/Target/Thread.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 30 | #include "lldb/Target/StackFrame.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | // This command is a toy. I'm just using it to have a way to construct the arguments to |
| 36 | // calling functions. |
| 37 | // |
| 38 | |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 39 | CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) : |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 40 | Options() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | { |
| 42 | // Keep only one place to reset the values to their defaults |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 43 | OptionParsingStarting(nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 46 | CommandObjectArgs::CommandOptions::~CommandOptions() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
| 48 | Error |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 49 | CommandObjectArgs::CommandOptions::SetOptionValue(uint32_t option_idx, |
| 50 | const char *option_arg, |
| 51 | ExecutionContext *execution_context) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
| 53 | Error error; |
| 54 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 55 | const int short_option = m_getopt_table[option_idx].val; |
Todd Fiala | 0a70a84 | 2014-05-28 16:43:26 +0000 | [diff] [blame] | 56 | error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | |
| 58 | return error; |
| 59 | } |
| 60 | |
| 61 | void |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 62 | CommandObjectArgs::CommandOptions::OptionParsingStarting( |
| 63 | ExecutionContext *execution_context) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 67 | const OptionDefinition* |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | CommandObjectArgs::CommandOptions::GetDefinitions () |
| 69 | { |
| 70 | return g_option_table; |
| 71 | } |
| 72 | |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 73 | CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 74 | CommandObjectParsed (interpreter, |
| 75 | "args", |
| 76 | "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*", |
| 77 | "args"), |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 78 | m_options (interpreter) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 82 | CommandObjectArgs::~CommandObjectArgs() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | |
| 84 | Options * |
| 85 | CommandObjectArgs::GetOptions () |
| 86 | { |
| 87 | return &m_options; |
| 88 | } |
| 89 | |
| 90 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 91 | CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | { |
| 93 | ConstString target_triple; |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 94 | |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 95 | Process *process = m_exe_ctx.GetProcessPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | if (!process) |
| 97 | { |
| 98 | result.AppendError ("Args found no process."); |
| 99 | result.SetStatus (eReturnStatusFailed); |
| 100 | return false; |
| 101 | } |
| 102 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 103 | const ABI *abi = process->GetABI().get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | if (!abi) |
| 105 | { |
| 106 | result.AppendError ("The current process has no ABI."); |
| 107 | result.SetStatus (eReturnStatusFailed); |
| 108 | return false; |
| 109 | } |
| 110 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 111 | const size_t num_args = args.GetArgumentCount (); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 112 | size_t arg_index; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | |
| 114 | if (!num_args) |
| 115 | { |
| 116 | result.AppendError ("args requires at least one argument"); |
| 117 | result.SetStatus (eReturnStatusFailed); |
| 118 | return false; |
| 119 | } |
| 120 | |
Greg Clayton | f9fc609 | 2013-01-09 19:44:40 +0000 | [diff] [blame] | 121 | Thread *thread = m_exe_ctx.GetThreadPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | |
| 123 | if (!thread) |
| 124 | { |
| 125 | result.AppendError ("args found no thread."); |
| 126 | result.SetStatus (eReturnStatusFailed); |
| 127 | return false; |
| 128 | } |
| 129 | |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 130 | lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | if (!thread_cur_frame) |
| 132 | { |
| 133 | result.AppendError ("The current thread has no current frame."); |
| 134 | result.SetStatus (eReturnStatusFailed); |
| 135 | return false; |
| 136 | } |
| 137 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 138 | ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule()); |
| 139 | if (!thread_module_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | { |
| 141 | result.AppendError ("The PC has no associated module."); |
| 142 | result.SetStatus (eReturnStatusFailed); |
| 143 | return false; |
| 144 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 145 | |
| 146 | TypeSystem *type_system = thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC); |
| 147 | if (type_system == nullptr) |
| 148 | { |
| 149 | result.AppendError ("Unable to create C type system."); |
| 150 | result.SetStatus (eReturnStatusFailed); |
| 151 | return false; |
| 152 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | |
| 154 | ValueList value_list; |
| 155 | |
| 156 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 157 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 158 | const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | Value value; |
| 160 | value.SetValueType(Value::eValueTypeScalar); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 161 | CompilerType compiler_type; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | |
| 163 | char *int_pos; |
Eli Friedman | 59817b1 | 2010-06-09 07:57:51 +0000 | [diff] [blame] | 164 | if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int"))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | { |
| 166 | Encoding encoding = eEncodingSint; |
| 167 | |
| 168 | int width = 0; |
| 169 | |
| 170 | if (int_pos > arg_type_cstr + 1) |
| 171 | { |
| 172 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 173 | result.SetStatus (eReturnStatusFailed); |
| 174 | return false; |
| 175 | } |
| 176 | if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u') |
| 177 | { |
| 178 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 179 | result.SetStatus (eReturnStatusFailed); |
| 180 | return false; |
| 181 | } |
| 182 | if (arg_type_cstr[0] == 'u') |
| 183 | { |
| 184 | encoding = eEncodingUint; |
| 185 | } |
| 186 | |
| 187 | char *width_pos = int_pos + 3; |
| 188 | |
| 189 | if (!strcmp (width_pos, "8_t")) |
| 190 | width = 8; |
| 191 | else if (!strcmp (width_pos, "16_t")) |
| 192 | width = 16; |
| 193 | else if (!strcmp (width_pos, "32_t")) |
| 194 | width = 32; |
| 195 | else if (!strcmp (width_pos, "64_t")) |
| 196 | width = 64; |
| 197 | else |
| 198 | { |
| 199 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 200 | result.SetStatus (eReturnStatusFailed); |
| 201 | return false; |
| 202 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 203 | compiler_type = type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 205 | if (!compiler_type.IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | { |
| 207 | result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n", |
| 208 | arg_type_cstr, |
| 209 | (encoding == eEncodingSint ? "signed" : "unsigned"), |
| 210 | width); |
| 211 | |
| 212 | result.SetStatus (eReturnStatusFailed); |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | else if (strchr (arg_type_cstr, '*')) |
| 217 | { |
| 218 | if (!strcmp (arg_type_cstr, "void*")) |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 219 | compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | else if (!strcmp (arg_type_cstr, "char*")) |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 221 | compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | else |
| 223 | { |
| 224 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 225 | result.SetStatus (eReturnStatusFailed); |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 232 | result.SetStatus (eReturnStatusFailed); |
| 233 | return false; |
| 234 | } |
| 235 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 236 | value.SetCompilerType (compiler_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | value_list.PushValue(value); |
| 238 | } |
| 239 | |
| 240 | if (!abi->GetArgumentValues (*thread, value_list)) |
| 241 | { |
| 242 | result.AppendError ("Couldn't get argument values"); |
| 243 | result.SetStatus (eReturnStatusFailed); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | result.GetOutputStream ().Printf("Arguments : \n"); |
| 248 | |
| 249 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 250 | { |
Deepak Panickal | 99fbc07 | 2014-03-03 15:39:47 +0000 | [diff] [blame] | 251 | result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ()); |
| 253 | result.GetOutputStream ().Printf("\n"); |
| 254 | } |
| 255 | |
| 256 | return result.Succeeded(); |
| 257 | } |
| 258 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 259 | OptionDefinition |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | CommandObjectArgs::CommandOptions::g_option_table[] = |
| 261 | { |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame] | 262 | // clang-format off |
| 263 | {LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."}, |
| 264 | {0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr} |
| 265 | // clang-format on |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | }; |