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 //===----------------------------------------------------------------------===//
10 #include "CommandObjectArgs.h"
14 // Other libraries and framework includes
16 #include "lldb/Interpreter/Args.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Expression/ClangExpression.h"
21 #include "lldb/Expression/ClangExpressionVariable.h"
22 #include "lldb/Expression/ClangFunction.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Interpreter/CommandInterpreter.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Symbol/ClangASTContext.h"
27 #include "lldb/Symbol/ObjectFile.h"
28 #include "lldb/Symbol/Variable.h"
29 #include "lldb/Target/ABI.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Target/StackFrame.h"
36 using namespace lldb_private;
38 // This command is a toy. I'm just using it to have a way to construct the arguments to
42 CommandObjectArgs::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
45 // Keep only one place to reset the values to their defaults
46 OptionParsingStarting();
50 CommandObjectArgs::CommandOptions::~CommandOptions ()
55 CommandObjectArgs::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
59 const int short_option = m_getopt_table[option_idx].val;
60 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
66 CommandObjectArgs::CommandOptions::OptionParsingStarting ()
70 const OptionDefinition*
71 CommandObjectArgs::CommandOptions::GetDefinitions ()
73 return g_option_table;
76 CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) :
77 CommandObjectParsed (interpreter,
79 "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*",
81 m_options (interpreter)
85 CommandObjectArgs::~CommandObjectArgs ()
90 CommandObjectArgs::GetOptions ()
96 CommandObjectArgs::DoExecute (Args& args, CommandReturnObject &result)
98 ConstString target_triple;
101 Process *process = m_exe_ctx.GetProcessPtr();
104 result.AppendError ("Args found no process.");
105 result.SetStatus (eReturnStatusFailed);
109 const ABI *abi = process->GetABI().get();
112 result.AppendError ("The current process has no ABI.");
113 result.SetStatus (eReturnStatusFailed);
117 const size_t num_args = args.GetArgumentCount ();
122 result.AppendError ("args requires at least one argument");
123 result.SetStatus (eReturnStatusFailed);
127 Thread *thread = m_exe_ctx.GetThreadPtr();
131 result.AppendError ("args found no thread.");
132 result.SetStatus (eReturnStatusFailed);
136 lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame ();
137 if (!thread_cur_frame)
139 result.AppendError ("The current thread has no current frame.");
140 result.SetStatus (eReturnStatusFailed);
144 ModuleSP thread_module_sp (thread_cur_frame->GetFrameCodeAddress ().GetModule());
145 if (!thread_module_sp)
147 result.AppendError ("The PC has no associated module.");
148 result.SetStatus (eReturnStatusFailed);
152 ClangASTContext &ast_context = thread_module_sp->GetClangASTContext();
154 ValueList value_list;
156 for (arg_index = 0; arg_index < num_args; ++arg_index)
158 const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index);
160 value.SetValueType(Value::eValueTypeScalar);
161 ClangASTType clang_type;
164 if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int")))
166 Encoding encoding = eEncodingSint;
170 if (int_pos > arg_type_cstr + 1)
172 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
173 result.SetStatus (eReturnStatusFailed);
176 if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u')
178 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
179 result.SetStatus (eReturnStatusFailed);
182 if (arg_type_cstr[0] == 'u')
184 encoding = eEncodingUint;
187 char *width_pos = int_pos + 3;
189 if (!strcmp (width_pos, "8_t"))
191 else if (!strcmp (width_pos, "16_t"))
193 else if (!strcmp (width_pos, "32_t"))
195 else if (!strcmp (width_pos, "64_t"))
199 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
200 result.SetStatus (eReturnStatusFailed);
204 clang_type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width);
206 if (!clang_type.IsValid())
208 result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n",
210 (encoding == eEncodingSint ? "signed" : "unsigned"),
213 result.SetStatus (eReturnStatusFailed);
217 else if (strchr (arg_type_cstr, '*'))
219 if (!strcmp (arg_type_cstr, "void*"))
220 clang_type = ast_context.GetBasicType(eBasicTypeVoid).GetPointerType();
221 else if (!strcmp (arg_type_cstr, "char*"))
222 clang_type = ast_context.GetCStringType (false);
225 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
226 result.SetStatus (eReturnStatusFailed);
232 result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr);
233 result.SetStatus (eReturnStatusFailed);
237 value.SetClangType (clang_type);
238 value_list.PushValue(value);
241 if (!abi->GetArgumentValues (*thread, value_list))
243 result.AppendError ("Couldn't get argument values");
244 result.SetStatus (eReturnStatusFailed);
248 result.GetOutputStream ().Printf("Arguments : \n");
250 for (arg_index = 0; arg_index < num_args; ++arg_index)
252 result.GetOutputStream ().Printf ("%" PRIu64 " (%s): ", (uint64_t)arg_index, args.GetArgumentAtIndex (arg_index));
253 value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ());
254 result.GetOutputStream ().Printf("\n");
257 return result.Succeeded();
261 CommandObjectArgs::CommandOptions::g_option_table[] =
263 { LLDB_OPT_SET_1, false, "debug", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
264 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }