2010-06-09 00:52:24 +08:00
//===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
# include "CommandObjectExpression.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
2010-11-30 10:22:11 +08:00
# include "CommandObjectThread.h" // For DisplayThreadInfo.
2010-07-23 08:16:21 +08:00
# include "lldb/Interpreter/Args.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Core/Value.h"
# include "lldb/Core/InputReader.h"
2010-09-30 08:54:27 +08:00
# include "lldb/Core/ValueObjectVariable.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Expression/ClangExpressionVariable.h"
This is a major refactoring of the expression parser.
The goal is to separate the parser's data from the data
belonging to the parser's clients. This allows clients
to use the parser to obtain (for example) a JIT compiled
function or some DWARF code, and then discard the parser
state.
Previously, parser state was held in ClangExpression and
used liberally by ClangFunction, which inherited from
ClangExpression. The main effects of this refactoring
are:
- reducing ClangExpression to an abstract class that
declares methods that any client must expose to the
expression parser,
- moving the code specific to implementing the "expr"
command from ClangExpression and
CommandObjectExpression into ClangUserExpression,
a new class,
- moving the common parser interaction code from
ClangExpression into ClangExpressionParser, a new
class, and
- making ClangFunction rely only on
ClangExpressionParser and not depend on the
internal implementation of ClangExpression.
Side effects include:
- the compiler interaction code has been factored
out of ClangFunction and is now in an AST pass
(ASTStructExtractor),
- the header file for ClangFunction is now fully
documented,
- several bugs that only popped up when Clang was
deallocated (which never happened, since the
lifetime of the compiler was essentially infinite)
are now fixed, and
- the developer-only "call" command has been
disabled.
I have tested the expr command and the Objective-C
step-into code, which use ClangUserExpression and
ClangFunction, respectively, and verified that they
work. Please let me know if you encounter bugs or
poor documentation.
llvm-svn: 112249
2010-08-27 09:01:44 +08:00
# include "lldb/Expression/ClangUserExpression.h"
2010-07-24 05:47:22 +08:00
# include "lldb/Expression/ClangFunction.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Expression/DWARFExpression.h"
# include "lldb/Host/Host.h"
2010-07-23 08:16:21 +08:00
# include "lldb/Core/Debugger.h"
2010-06-23 09:19:29 +08:00
# include "lldb/Interpreter/CommandInterpreter.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Interpreter/CommandReturnObject.h"
2010-09-30 08:54:27 +08:00
# include "lldb/Target/ObjCLanguageRuntime.h"
2010-06-09 00:52:24 +08:00
# include "lldb/Symbol/ObjectFile.h"
# include "lldb/Symbol/Variable.h"
# include "lldb/Target/Process.h"
# include "lldb/Target/StackFrame.h"
# include "lldb/Target/Target.h"
2010-07-23 08:16:21 +08:00
# include "llvm/ADT/StringRef.h"
2010-06-09 00:52:24 +08:00
using namespace lldb ;
using namespace lldb_private ;
2011-04-08 06:46:35 +08:00
CommandObjectExpression : : CommandOptions : : CommandOptions ( CommandInterpreter & interpreter ) :
Options ( m_interpreter )
2010-06-09 00:52:24 +08:00
{
// Keep only one place to reset the values to their defaults
ResetOptionValues ( ) ;
}
CommandObjectExpression : : CommandOptions : : ~ CommandOptions ( )
{
}
Error
CommandObjectExpression : : CommandOptions : : SetOptionValue ( int option_idx , const char * option_arg )
{
Error error ;
char short_option = ( char ) m_getopt_table [ option_idx ] . val ;
switch ( short_option )
{
2010-09-08 06:38:08 +08:00
//case 'l':
//if (language.SetLanguageFromCString (option_arg) == false)
//{
// error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
//}
//break;
2010-06-09 00:52:24 +08:00
case ' g ' :
debug = true ;
break ;
case ' f ' :
error = Args : : StringToFormat ( option_arg , format ) ;
break ;
2010-09-30 08:54:27 +08:00
case ' o ' :
print_object = true ;
break ;
2010-11-06 03:25:48 +08:00
case ' u ' :
bool success ;
unwind_on_error = Args : : StringToBoolean ( option_arg , true , & success ) ;
if ( ! success )
error . SetErrorStringWithFormat ( " Could not convert \" %s \" to a boolean value. " , option_arg ) ;
break ;
2010-06-09 00:52:24 +08:00
default :
error . SetErrorStringWithFormat ( " Invalid short option character '%c'. \n " , short_option ) ;
break ;
}
return error ;
}
void
CommandObjectExpression : : CommandOptions : : ResetOptionValues ( )
{
2010-09-08 06:38:08 +08:00
//language.Clear();
2010-06-09 00:52:24 +08:00
debug = false ;
format = eFormatDefault ;
2010-09-30 08:54:27 +08:00
print_object = false ;
2010-11-06 03:25:48 +08:00
unwind_on_error = true ;
2010-06-09 00:52:24 +08:00
show_types = true ;
show_summary = true ;
}
2011-03-25 05:19:54 +08:00
const OptionDefinition *
2010-06-09 00:52:24 +08:00
CommandObjectExpression : : CommandOptions : : GetDefinitions ( )
{
return g_option_table ;
}
2010-09-18 09:14:36 +08:00
CommandObjectExpression : : CommandObjectExpression ( CommandInterpreter & interpreter ) :
CommandObject ( interpreter ,
" expression " ,
2010-10-01 02:16:58 +08:00
" Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope. " ,
2010-10-05 06:28:36 +08:00
NULL ) ,
2011-04-08 06:46:35 +08:00
m_options ( interpreter ) ,
2010-10-01 02:30:25 +08:00
m_expr_line_count ( 0 ) ,
2010-06-09 00:52:24 +08:00
m_expr_lines ( )
{
SetHelpLong (
" Examples: \n \
\ n \
expr my_struct - > a = my_array [ 3 ] \ n \
expr - f bin - - ( index * 8 ) + 5 \ n \
expr char c [ ] = \ " foo \" ; c[0] \n " ) ;
2010-10-05 06:28:36 +08:00
CommandArgumentEntry arg ;
CommandArgumentData expression_arg ;
// Define the first (and only) variant of this arg.
expression_arg . arg_type = eArgTypeExpression ;
expression_arg . arg_repetition = eArgRepeatPlain ;
// There is only one variant this argument could be; put it into the argument entry.
arg . push_back ( expression_arg ) ;
// Push the data for the first argument into the m_arguments vector.
m_arguments . push_back ( arg ) ;
2010-06-09 00:52:24 +08:00
}
CommandObjectExpression : : ~ CommandObjectExpression ( )
{
}
Options *
CommandObjectExpression : : GetOptions ( )
{
return & m_options ;
}
bool
CommandObjectExpression : : Execute
(
Args & command ,
CommandReturnObject & result
)
{
return false ;
}
size_t
CommandObjectExpression : : MultiLineExpressionCallback
(
void * baton ,
2010-06-23 09:19:29 +08:00
InputReader & reader ,
2010-06-09 00:52:24 +08:00
lldb : : InputReaderAction notification ,
const char * bytes ,
size_t bytes_len
)
{
CommandObjectExpression * cmd_object_expr = ( CommandObjectExpression * ) baton ;
switch ( notification )
{
case eInputReaderActivate :
2010-06-23 09:19:29 +08:00
reader . GetDebugger ( ) . GetOutputStream ( ) . Printf ( " %s \n " , " Enter expressions, then terminate with an empty line to evaluate: " ) ;
2010-06-09 00:52:24 +08:00
// Fall through
case eInputReaderReactivate :
//if (out_fh)
2010-06-23 09:19:29 +08:00
// reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
2010-06-09 00:52:24 +08:00
break ;
case eInputReaderDeactivate :
break ;
case eInputReaderGotToken :
+ + cmd_object_expr - > m_expr_line_count ;
if ( bytes & & bytes_len )
{
cmd_object_expr - > m_expr_lines . append ( bytes , bytes_len + 1 ) ;
}
if ( bytes_len = = 0 )
2010-06-23 09:19:29 +08:00
reader . SetIsDone ( true ) ;
2010-06-09 00:52:24 +08:00
//else if (out_fh && !reader->IsDone())
// ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
break ;
2010-11-20 04:47:54 +08:00
case eInputReaderInterrupt :
cmd_object_expr - > m_expr_lines . clear ( ) ;
reader . SetIsDone ( true ) ;
reader . GetDebugger ( ) . GetOutputStream ( ) . Printf ( " %s \n " , " Expression evaluation cancelled. " ) ;
break ;
case eInputReaderEndOfFile :
reader . SetIsDone ( true ) ;
break ;
2010-06-09 00:52:24 +08:00
case eInputReaderDone :
2010-11-20 04:47:54 +08:00
if ( cmd_object_expr - > m_expr_lines . size ( ) > 0 )
2010-06-09 00:52:24 +08:00
{
cmd_object_expr - > EvaluateExpression ( cmd_object_expr - > m_expr_lines . c_str ( ) ,
2010-06-23 09:19:29 +08:00
reader . GetDebugger ( ) . GetOutputStream ( ) ,
reader . GetDebugger ( ) . GetErrorStream ( ) ) ;
2010-06-09 00:52:24 +08:00
}
break ;
}
return bytes_len ;
}
bool
2010-10-05 08:00:42 +08:00
CommandObjectExpression : : EvaluateExpression
(
const char * expr ,
Stream & output_stream ,
Stream & error_stream ,
CommandReturnObject * result
)
2010-06-09 00:52:24 +08:00
{
2010-10-29 08:29:03 +08:00
if ( m_exe_ctx . target )
2010-07-23 08:16:21 +08:00
{
2010-12-14 10:59:59 +08:00
lldb : : ValueObjectSP result_valobj_sp ;
2011-03-25 05:19:54 +08:00
ExecutionResults exe_results ;
2011-01-13 16:53:35 +08:00
bool keep_in_memory = true ;
exe_results = m_exe_ctx . target - > EvaluateExpression ( expr , m_exe_ctx . frame , m_options . unwind_on_error , keep_in_memory , result_valobj_sp ) ;
2010-12-14 10:59:59 +08:00
if ( exe_results = = eExecutionInterrupted & & ! m_options . unwind_on_error )
2010-11-30 10:22:11 +08:00
{
if ( m_exe_ctx . thread )
lldb_private : : DisplayThreadInfo ( m_interpreter , result - > GetOutputStream ( ) , m_exe_ctx . thread , false , true ) ;
else
lldb_private : : DisplayThreadsInfo ( m_interpreter , & m_exe_ctx , * result , true , true ) ;
2010-12-14 10:59:59 +08:00
}
if ( result_valobj_sp )
{
if ( result_valobj_sp - > GetError ( ) . Success ( ) )
{
if ( m_options . format ! = eFormatDefault )
result_valobj_sp - > SetFormat ( m_options . format ) ;
ValueObject : : DumpValueObject ( output_stream ,
result_valobj_sp . get ( ) , // Variable object to dump
result_valobj_sp - > GetName ( ) . GetCString ( ) , // Root object name
0 , // Pointer depth to traverse (zero means stop at pointers)
0 , // Current depth, this is the top most, so zero...
UINT32_MAX , // Max depth to go when dumping concrete types, dump everything...
m_options . show_types , // Show types when dumping?
false , // Show locations of variables, no since this is a host address which we don't care to see
m_options . print_object , // Print the objective C object?
true , // Scope is already checked. Const results are always in scope.
false ) ; // Don't flatten output
if ( result )
result - > SetStatus ( eReturnStatusSuccessFinishResult ) ;
}
else
{
error_stream . PutCString ( result_valobj_sp - > GetError ( ) . AsCString ( ) ) ;
if ( result )
result - > SetStatus ( eReturnStatusFailed ) ;
2010-11-30 10:22:11 +08:00
}
}
2010-12-14 10:59:59 +08:00
}
else
{
error_stream . Printf ( " error: invalid execution context for expression \n " ) ;
return false ;
2010-07-23 08:16:21 +08:00
}
2010-08-12 09:56:52 +08:00
2010-07-23 08:16:21 +08:00
return true ;
2010-06-09 00:52:24 +08:00
}
bool
CommandObjectExpression : : ExecuteRawCommandString
(
const char * command ,
CommandReturnObject & result
)
{
2010-09-18 09:14:36 +08:00
m_exe_ctx = m_interpreter . GetDebugger ( ) . GetExecutionContext ( ) ;
2010-06-09 00:52:24 +08:00
Many improvements to the Platform base class and subclasses. The base Platform
class now implements the Host functionality for a lot of things that make
sense by default so that subclasses can check:
int
PlatformSubclass::Foo ()
{
if (IsHost())
return Platform::Foo (); // Let the platform base class do the host specific stuff
// Platform subclass specific code...
int result = ...
return result;
}
Added new functions to the platform:
virtual const char *Platform::GetUserName (uint32_t uid);
virtual const char *Platform::GetGroupName (uint32_t gid);
The user and group names are cached locally so that remote platforms can avoid
sending packets multiple times to resolve this information.
Added the parent process ID to the ProcessInfo class.
Added a new ProcessInfoMatch class which helps us to match processes up
and changed the Host layer over to using this new class. The new class allows
us to search for processs:
1 - by name (equal to, starts with, ends with, contains, and regex)
2 - by pid
3 - And further check for parent pid == value, uid == value, gid == value,
euid == value, egid == value, arch == value, parent == value.
This is all hookup up to the "platform process list" command which required
adding dumping routines to dump process information. If the Host class
implements the process lookup routines, you can now lists processes on
your local machine:
machine1.foo.com % lldb
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
94727 244 username usergroup username usergroup x86_64-apple-darwin Xcode
92742 92710 username usergroup username usergroup i386-apple-darwin debugserver
This of course also works remotely with the lldb-platform:
machine1.foo.com % lldb-platform --listen 1234
machine2.foo.com % lldb
(lldb) platform create remote-macosx
Platform: remote-macosx
Connected: no
(lldb) platform connect connect://localhost:1444
Platform: remote-macosx
Triple: x86_64-apple-darwin
OS Version: 10.6.7 (10J869)
Kernel: Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386
Hostname: machine1.foo.com
Connected: yes
(lldb) platform process list
PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99556 244 username usergroup username usergroup x86_64-apple-darwin trustevaluation
99548 65539 username usergroup username usergroup x86_64-apple-darwin lldb
99538 1 username usergroup username usergroup x86_64-apple-darwin FileMerge
94943 1 username usergroup username usergroup x86_64-apple-darwin mdworker
94852 244 username usergroup username usergroup x86_64-apple-darwin Safari
The lldb-platform implements everything with the Host:: layer, so this should
"just work" for linux. I will probably be adding more stuff to the Host layer
for launching processes and attaching to processes so that this support should
eventually just work as well.
Modified the target to be able to be created with an architecture that differs
from the main executable. This is needed for iOS debugging since we can have
an "armv6" binary which can run on an "armv7" machine, so we want to be able
to do:
% lldb
(lldb) platform create remote-ios
(lldb) file --arch armv7 a.out
Where "a.out" is an armv6 executable. The platform then can correctly decide
to open all "armv7" images for all dependent shared libraries.
Modified the disassembly to show the current PC value. Example output:
(lldb) disassemble --frame
a.out`main:
0x1eb7: pushl %ebp
0x1eb8: movl %esp, %ebp
0x1eba: pushl %ebx
0x1ebb: subl $20, %esp
0x1ebe: calll 0x1ec3 ; main + 12 at test.c:18
0x1ec3: popl %ebx
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
0x1edb: leal 213(%ebx), %eax
0x1ee1: movl %eax, (%esp)
0x1ee4: calll 0x1f1e ; puts
0x1ee9: calll 0x1f0c ; getchar
0x1eee: movl $20, (%esp)
0x1ef5: calll 0x1e6a ; sleep_loop at test.c:6
0x1efa: movl $12, %eax
0x1eff: addl $20, %esp
0x1f02: popl %ebx
0x1f03: leave
0x1f04: ret
This can be handy when dealing with the new --line options that was recently
added:
(lldb) disassemble --line
a.out`main + 13 at test.c:19
18 {
-> 19 printf("Process: %i\n\n", getpid());
20 puts("Press any key to continue..."); getchar();
-> 0x1ec4: calll 0x1f12 ; getpid
0x1ec9: movl %eax, 4(%esp)
0x1ecd: leal 199(%ebx), %eax
0x1ed3: movl %eax, (%esp)
0x1ed6: calll 0x1f18 ; printf
Modified the ModuleList to have a lookup based solely on a UUID. Since the
UUID is typically the MD5 checksum of a binary image, there is no need
to give the path and architecture when searching for a pre-existing
image in an image list.
Now that we support remote debugging a bit better, our lldb_private::Module
needs to be able to track what the original path for file was as the platform
knows it, as well as where the file is locally. The module has the two
following functions to retrieve both paths:
const FileSpec &Module::GetFileSpec () const;
const FileSpec &Module::GetPlatformFileSpec () const;
llvm-svn: 128563
2011-03-31 02:16:51 +08:00
m_options . Reset ( ) ;
2010-06-09 00:52:24 +08:00
const char * expr = NULL ;
if ( command [ 0 ] = = ' \0 ' )
{
m_expr_lines . clear ( ) ;
m_expr_line_count = 0 ;
2010-09-18 09:14:36 +08:00
InputReaderSP reader_sp ( new InputReader ( m_interpreter . GetDebugger ( ) ) ) ;
2010-06-09 00:52:24 +08:00
if ( reader_sp )
{
Error err ( reader_sp - > Initialize ( CommandObjectExpression : : MultiLineExpressionCallback ,
this , // baton
eInputReaderGranularityLine , // token size, to pass to callback function
2010-06-23 09:19:29 +08:00
NULL , // end token
2010-06-09 00:52:24 +08:00
NULL , // prompt
true ) ) ; // echo input
if ( err . Success ( ) )
{
2010-09-18 09:14:36 +08:00
m_interpreter . GetDebugger ( ) . PushInputReader ( reader_sp ) ;
2010-06-09 00:52:24 +08:00
result . SetStatus ( eReturnStatusSuccessFinishNoResult ) ;
}
else
{
result . AppendError ( err . AsCString ( ) ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
}
else
{
result . AppendError ( " out of memory " ) ;
result . SetStatus ( eReturnStatusFailed ) ;
}
return result . Succeeded ( ) ;
}
if ( command [ 0 ] = = ' - ' )
{
// We have some options and these options MUST end with --.
const char * end_options = NULL ;
const char * s = command ;
while ( s & & s [ 0 ] )
{
end_options = : : strstr ( s , " -- " ) ;
if ( end_options )
{
end_options + = 2 ; // Get past the "--"
if ( : : isspace ( end_options [ 0 ] ) )
{
expr = end_options ;
while ( : : isspace ( * expr ) )
+ + expr ;
break ;
}
}
s = end_options ;
}
if ( end_options )
{
2010-06-23 09:19:29 +08:00
Args args ( command , end_options - command ) ;
2010-09-18 09:14:36 +08:00
if ( ! ParseOptions ( args , result ) )
2010-06-09 00:52:24 +08:00
return false ;
}
}
if ( expr = = NULL )
expr = command ;
2010-06-24 08:16:27 +08:00
2010-10-05 08:31:29 +08:00
if ( EvaluateExpression ( expr , result . GetOutputStream ( ) , result . GetErrorStream ( ) , & result ) )
2010-08-13 08:42:30 +08:00
return true ;
result . SetStatus ( eReturnStatusFailed ) ;
return false ;
2010-06-09 00:52:24 +08:00
}
2011-03-25 05:19:54 +08:00
OptionDefinition
2010-06-09 00:52:24 +08:00
CommandObjectExpression : : CommandOptions : : g_option_table [ ] =
{
2010-09-08 06:38:08 +08:00
//{ LLDB_OPT_SET_ALL, false, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
2010-10-02 03:59:14 +08:00
//{ LLDB_OPT_SET_1, false, "format", 'f', required_argument, NULL, 0, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]", "Specify the format that the expression output should use."},
{ LLDB_OPT_SET_1 , false , " format " , ' f ' , required_argument , NULL , 0 , eArgTypeExprFormat , " Specify the format that the expression output should use. " } ,
2010-11-06 03:25:48 +08:00
{ LLDB_OPT_SET_2 , false , " object-description " , ' o ' , no_argument , NULL , 0 , eArgTypeNone , " Print the object description of the value resulting from the expression. " } ,
{ LLDB_OPT_SET_ALL , false , " unwind-on-error " , ' u ' , required_argument , NULL , 0 , eArgTypeBoolean , " Clean up program state if the expression causes a crash, breakpoint hit or signal. " } ,
2010-10-02 03:59:14 +08:00
{ LLDB_OPT_SET_ALL , false , " debug " , ' g ' , no_argument , NULL , 0 , eArgTypeNone , " Enable verbose debug logging of the expression parsing and evaluation. " } ,
{ LLDB_OPT_SET_ALL , false , " use-ir " , ' i ' , no_argument , NULL , 0 , eArgTypeNone , " [Temporary] Instructs the expression evaluator to use IR instead of ASTs. " } ,
{ 0 , false , NULL , 0 , 0 , NULL , NULL , eArgTypeNone , NULL }
2010-06-09 00:52:24 +08:00
} ;