1 //===-- CommandObjectArgs.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "CommandObjectArgs.h"
15 #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Value.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Interpreter/Args.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Interpreter/CommandReturnObject.h"
23 #include "lldb/Symbol/ClangASTContext.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/Variable.h"
26 #include "lldb/Target/ABI.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
32 #include "llvm/ADT/StringSwitch.h"
35 using namespace lldb_private;
37 // This command is a toy. I'm just using it to have a way to construct the
42 static OptionDefinition g_arg_options[] = {
44 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation." },
48 CommandObjectArgs::CommandOptions::CommandOptions(
49 CommandInterpreter &interpreter)
51 // Keep only one place to reset the values to their defaults
52 OptionParsingStarting(nullptr);
55 CommandObjectArgs::CommandOptions::~CommandOptions() = default;
57 Error CommandObjectArgs::CommandOptions::SetOptionValue(
58 uint32_t option_idx, llvm::StringRef option_arg,
59 ExecutionContext *execution_context) {
62 const int short_option = m_getopt_table[option_idx].val;
63 error.SetErrorStringWithFormat("invalid short option character '%c'",
69 void CommandObjectArgs::CommandOptions::OptionParsingStarting(
70 ExecutionContext *execution_context) {}
72 llvm::ArrayRef<OptionDefinition>
73 CommandObjectArgs::CommandOptions::GetDefinitions() {
74 return llvm::makeArrayRef(g_arg_options);
77 CommandObjectArgs::CommandObjectArgs(CommandInterpreter &interpreter)
78 : CommandObjectParsed(interpreter, "args",
79 "When stopped at the start of a function, reads "
80 "function arguments of type (u?)int(8|16|32|64)_t, "
83 m_options(interpreter) {}
85 CommandObjectArgs::~CommandObjectArgs() = default;
87 Options *CommandObjectArgs::GetOptions() { return &m_options; }
89 bool CommandObjectArgs::DoExecute(Args &args, CommandReturnObject &result) {
90 ConstString target_triple;
92 Process *process = m_exe_ctx.GetProcessPtr();
94 result.AppendError("Args found no process.");
95 result.SetStatus(eReturnStatusFailed);
99 const ABI *abi = process->GetABI().get();
101 result.AppendError("The current process has no ABI.");
102 result.SetStatus(eReturnStatusFailed);
107 result.AppendError("args requires at least one argument");
108 result.SetStatus(eReturnStatusFailed);
112 Thread *thread = m_exe_ctx.GetThreadPtr();
115 result.AppendError("args found no thread.");
116 result.SetStatus(eReturnStatusFailed);
120 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame();
121 if (!thread_cur_frame) {
122 result.AppendError("The current thread has no current frame.");
123 result.SetStatus(eReturnStatusFailed);
127 ModuleSP thread_module_sp(
128 thread_cur_frame->GetFrameCodeAddress().GetModule());
129 if (!thread_module_sp) {
130 result.AppendError("The PC has no associated module.");
131 result.SetStatus(eReturnStatusFailed);
135 TypeSystem *type_system =
136 thread_module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
137 if (type_system == nullptr) {
138 result.AppendError("Unable to create C type system.");
139 result.SetStatus(eReturnStatusFailed);
143 ValueList value_list;
145 for (auto &arg_entry : args.entries()) {
146 llvm::StringRef arg_type = arg_entry.ref;
148 value.SetValueType(Value::eValueTypeScalar);
149 CompilerType compiler_type;
151 std::size_t int_pos = arg_type.find("int");
152 if (int_pos != llvm::StringRef::npos) {
153 Encoding encoding = eEncodingSint;
158 result.AppendErrorWithFormat("Invalid format: %s.\n",
160 result.SetStatus(eReturnStatusFailed);
163 if (int_pos == 1 && arg_type[0] != 'u') {
164 result.AppendErrorWithFormat("Invalid format: %s.\n",
166 result.SetStatus(eReturnStatusFailed);
169 if (arg_type[0] == 'u') {
170 encoding = eEncodingUint;
173 llvm::StringRef width_spec = arg_type.drop_front(int_pos + 3);
175 auto exp_result = llvm::StringSwitch<llvm::Optional<int>>(width_spec)
180 .Default(llvm::None);
181 if (!exp_result.hasValue()) {
182 result.AppendErrorWithFormat("Invalid format: %s.\n",
184 result.SetStatus(eReturnStatusFailed);
190 type_system->GetBuiltinTypeForEncodingAndBitSize(encoding, width);
192 if (!compiler_type.IsValid()) {
193 result.AppendErrorWithFormat(
194 "Couldn't get Clang type for format %s (%s integer, width %d).\n",
196 (encoding == eEncodingSint ? "signed" : "unsigned"), width);
198 result.SetStatus(eReturnStatusFailed);
201 } else if (arg_type == "void*") {
203 type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
204 } else if (arg_type == "char*") {
206 type_system->GetBasicTypeFromAST(eBasicTypeChar).GetPointerType();
208 result.AppendErrorWithFormat("Invalid format: %s.\n", arg_entry.c_str());
209 result.SetStatus(eReturnStatusFailed);
213 value.SetCompilerType(compiler_type);
214 value_list.PushValue(value);
217 if (!abi->GetArgumentValues(*thread, value_list)) {
218 result.AppendError("Couldn't get argument values");
219 result.SetStatus(eReturnStatusFailed);
223 result.GetOutputStream().Printf("Arguments : \n");
225 for (auto entry : llvm::enumerate(args.entries())) {
226 result.GetOutputStream().Printf("%" PRIu64 " (%s): ", (uint64_t)entry.Index,
227 entry.Value.c_str());
228 value_list.GetValueAtIndex(entry.Index)->Dump(&result.GetOutputStream());
229 result.GetOutputStream().Printf("\n");
232 return result.Succeeded();