2010-08-28 07:31:21 +08:00
|
|
|
//===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// C++ Includes
|
|
|
|
|
|
|
|
#include "lldb/Core/ConstString.h"
|
2011-05-24 05:40:23 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-08-28 07:31:21 +08:00
|
|
|
#include "lldb/Core/Stream.h"
|
2011-01-19 03:36:39 +08:00
|
|
|
#include "lldb/Core/StreamFile.h"
|
2010-09-14 05:34:21 +08:00
|
|
|
#include "lldb/Expression/ClangExpressionDeclMap.h"
|
2010-08-28 07:31:21 +08:00
|
|
|
#include "lldb/Expression/ClangExpressionParser.h"
|
|
|
|
#include "lldb/Expression/ClangUtilityFunction.h"
|
2013-02-14 07:57:48 +08:00
|
|
|
#include "lldb/Expression/ExpressionSourceCode.h"
|
2013-04-19 02:10:51 +08:00
|
|
|
#include "lldb/Expression/IRExecutionUnit.h"
|
2010-08-28 07:31:21 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
#include "lldb/Target/ExecutionContext.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Constructor
|
|
|
|
///
|
|
|
|
/// @param[in] text
|
|
|
|
/// The text of the function. Must be a full translation unit.
|
|
|
|
///
|
|
|
|
/// @param[in] name
|
|
|
|
/// The name of the function, as used in the text.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
ClangUtilityFunction::ClangUtilityFunction (const char *text,
|
|
|
|
const char *name) :
|
2011-01-20 07:00:49 +08:00
|
|
|
ClangExpression (),
|
2013-02-14 07:57:48 +08:00
|
|
|
m_function_text (ExpressionSourceCode::g_expression_prefix),
|
2011-01-20 07:00:49 +08:00
|
|
|
m_function_name (name)
|
2010-08-28 07:31:21 +08:00
|
|
|
{
|
2013-02-14 06:56:14 +08:00
|
|
|
if (text && text[0])
|
|
|
|
m_function_text.append (text);
|
2010-08-28 07:31:21 +08:00
|
|
|
}
|
|
|
|
|
2010-09-25 07:07:41 +08:00
|
|
|
ClangUtilityFunction::~ClangUtilityFunction ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-28 07:31:21 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
/// Install the utility function into a process
|
|
|
|
///
|
|
|
|
/// @param[in] error_stream
|
|
|
|
/// A stream to print parse errors and warnings to.
|
|
|
|
///
|
|
|
|
/// @param[in] exe_ctx
|
|
|
|
/// The execution context to install the utility function to.
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// True on success (no errors); false otherwise.
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
ClangUtilityFunction::Install (Stream &error_stream,
|
|
|
|
ExecutionContext &exe_ctx)
|
2010-09-01 08:58:00 +08:00
|
|
|
{
|
2011-01-20 07:00:49 +08:00
|
|
|
if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
|
2010-09-01 08:58:00 +08:00
|
|
|
{
|
|
|
|
error_stream.PutCString("error: already installed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-28 07:31:21 +08:00
|
|
|
////////////////////////////////////
|
|
|
|
// Set up the target and compiler
|
|
|
|
//
|
|
|
|
|
2011-09-22 12:58:26 +08:00
|
|
|
Target *target = exe_ctx.GetTargetPtr();
|
2010-08-28 07:31:21 +08:00
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
error_stream.PutCString ("error: invalid target\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-24 05:40:23 +08:00
|
|
|
|
2011-09-22 12:58:26 +08:00
|
|
|
Process *process = exe_ctx.GetProcessPtr();
|
2011-05-24 05:40:23 +08:00
|
|
|
|
|
|
|
if (!process)
|
|
|
|
{
|
|
|
|
error_stream.PutCString ("error: invalid process\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-16 05:59:32 +08:00
|
|
|
|
2010-08-28 07:31:21 +08:00
|
|
|
//////////////////////////
|
|
|
|
// Parse the expression
|
|
|
|
//
|
2010-09-14 05:34:21 +08:00
|
|
|
|
2011-01-13 16:53:35 +08:00
|
|
|
bool keep_result_in_memory = false;
|
|
|
|
|
2011-10-29 09:58:46 +08:00
|
|
|
m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
|
2010-12-03 09:38:59 +08:00
|
|
|
|
2013-04-11 08:09:05 +08:00
|
|
|
if (!m_expr_decl_map->WillParse(exe_ctx, NULL))
|
2011-08-02 02:18:33 +08:00
|
|
|
{
|
|
|
|
error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 07:31:21 +08:00
|
|
|
|
2011-02-16 05:59:32 +08:00
|
|
|
ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this);
|
2010-08-28 07:31:21 +08:00
|
|
|
|
|
|
|
unsigned num_errors = parser.Parse (error_stream);
|
|
|
|
|
|
|
|
if (num_errors)
|
|
|
|
{
|
|
|
|
error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
|
2010-09-14 05:34:21 +08:00
|
|
|
|
|
|
|
m_expr_decl_map.reset();
|
|
|
|
|
2010-08-28 07:31:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// JIT the output of the parser
|
|
|
|
//
|
This commit changes the way LLDB executes user
expressions.
Previously, ClangUserExpression assumed that if
there was a constant result for an expression
then it could be determined during parsing. In
particular, the IRInterpreter ran while parser
state (in particular, ClangExpressionDeclMap)
was present. This approach is flawed, because
the IRInterpreter actually is capable of using
external variables, and hence the result might
be different each run. Until now, we papered
over this flaw by re-parsing the expression each
time we ran it.
I have rewritten the IRInterpreter to be
completely independent of the ClangExpressionDeclMap.
Instead of special-casing external variable lookup,
which ties the IRInterpreter closely to LLDB,
we now interpret the exact same IR that the JIT
would see. This IR assumes that materialization
has occurred; hence the recent implementation of the
Materializer, which does not require parser state
(in the form of ClangExpressionDeclMap) to be
present.
Materialization, interpretation, and dematerialization
are now all independent of parsing. This means that
in theory we can parse expressions once and run them
many times. I have three outstanding tasks before
shutting this down:
- First, I will ensure that all of this works with
core files. Core files have a Process but do not
allow allocating memory, which currently confuses
materialization.
- Second, I will make expression breakpoint
conditions remember their ClangUserExpression and
re-use it.
- Third, I will tear out all the redundant code
(for example, materialization logic in
ClangExpressionDeclMap) that is no longer used.
While implementing this fix, I also found a bug in
IRForTarget's handling of floating-point constants.
This should be fixed.
llvm-svn: 179801
2013-04-19 06:06:33 +08:00
|
|
|
|
|
|
|
bool can_interpret = false; // should stay that way
|
This patch modifies the expression parser to allow it
to execute expressions even in the absence of a process.
This allows expressions to run in situations where the
target cannot run -- e.g., to perform calculations based
on type information, or to inspect a binary's static
data.
This modification touches the following files:
lldb-private-enumerations.h
Introduce a new enum specifying the policy for
processing an expression. Some expressions should
always be JITted, for example if they are functions
that will be used over and over again. Some
expressions should always be interpreted, for
example if the target is unsafe to run. For most,
it is acceptable to JIT them, but interpretation
is preferable when possible.
Target.[h,cpp]
Have EvaluateExpression now accept the new enum.
ClangExpressionDeclMap.[cpp,h]
Add support for the IR interpreter and also make
the ClangExpressionDeclMap more robust in the
absence of a process.
ClangFunction.[cpp,h]
Add support for the new enum.
IRInterpreter.[cpp,h]
New implementation.
ClangUserExpression.[cpp,h]
Add support for the new enum, and for running
expressions in the absence of a process.
ClangExpression.h
Remove references to the old DWARF-based method
of evaluating expressions, because it has been
superseded for now.
ClangUtilityFunction.[cpp,h]
Add support for the new enum.
ClangExpressionParser.[cpp,h]
Add support for the new enum, remove references
to DWARF, and add support for checking whether
the expression could be evaluated statically.
IRForTarget.[h,cpp]
Add support for the new enum, and add utility
functions to support the interpreter.
IRToDWARF.cpp
Removed
CommandObjectExpression.cpp
Remove references to the obsolete -i option.
Process.cpp
Modify calls to ClangUserExpression::Evaluate
to pass the correct enum (for dlopen/dlclose)
SBValue.cpp
Add support for the new enum.
SBFrame.cpp
Add support for he new enum.
BreakpointOptions.cpp
Add support for the new enum.
llvm-svn: 139772
2011-09-15 10:13:07 +08:00
|
|
|
|
2013-03-19 08:10:07 +08:00
|
|
|
Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
|
2013-04-05 10:22:57 +08:00
|
|
|
m_jit_end_addr,
|
|
|
|
m_execution_unit_ap,
|
This patch modifies the expression parser to allow it
to execute expressions even in the absence of a process.
This allows expressions to run in situations where the
target cannot run -- e.g., to perform calculations based
on type information, or to inspect a binary's static
data.
This modification touches the following files:
lldb-private-enumerations.h
Introduce a new enum specifying the policy for
processing an expression. Some expressions should
always be JITted, for example if they are functions
that will be used over and over again. Some
expressions should always be interpreted, for
example if the target is unsafe to run. For most,
it is acceptable to JIT them, but interpretation
is preferable when possible.
Target.[h,cpp]
Have EvaluateExpression now accept the new enum.
ClangExpressionDeclMap.[cpp,h]
Add support for the IR interpreter and also make
the ClangExpressionDeclMap more robust in the
absence of a process.
ClangFunction.[cpp,h]
Add support for the new enum.
IRInterpreter.[cpp,h]
New implementation.
ClangUserExpression.[cpp,h]
Add support for the new enum, and for running
expressions in the absence of a process.
ClangExpression.h
Remove references to the old DWARF-based method
of evaluating expressions, because it has been
superseded for now.
ClangUtilityFunction.[cpp,h]
Add support for the new enum.
ClangExpressionParser.[cpp,h]
Add support for the new enum, remove references
to DWARF, and add support for checking whether
the expression could be evaluated statically.
IRForTarget.[h,cpp]
Add support for the new enum, and add utility
functions to support the interpreter.
IRToDWARF.cpp
Removed
CommandObjectExpression.cpp
Remove references to the obsolete -i option.
Process.cpp
Modify calls to ClangUserExpression::Evaluate
to pass the correct enum (for dlopen/dlclose)
SBValue.cpp
Add support for the new enum.
SBFrame.cpp
Add support for he new enum.
BreakpointOptions.cpp
Add support for the new enum.
llvm-svn: 139772
2011-09-15 10:13:07 +08:00
|
|
|
exe_ctx,
|
This commit changes the way LLDB executes user
expressions.
Previously, ClangUserExpression assumed that if
there was a constant result for an expression
then it could be determined during parsing. In
particular, the IRInterpreter ran while parser
state (in particular, ClangExpressionDeclMap)
was present. This approach is flawed, because
the IRInterpreter actually is capable of using
external variables, and hence the result might
be different each run. Until now, we papered
over this flaw by re-parsing the expression each
time we ran it.
I have rewritten the IRInterpreter to be
completely independent of the ClangExpressionDeclMap.
Instead of special-casing external variable lookup,
which ties the IRInterpreter closely to LLDB,
we now interpret the exact same IR that the JIT
would see. This IR assumes that materialization
has occurred; hence the recent implementation of the
Materializer, which does not require parser state
(in the form of ClangExpressionDeclMap) to be
present.
Materialization, interpretation, and dematerialization
are now all independent of parsing. This means that
in theory we can parse expressions once and run them
many times. I have three outstanding tasks before
shutting this down:
- First, I will ensure that all of this works with
core files. Core files have a Process but do not
allow allocating memory, which currently confuses
materialization.
- Second, I will make expression breakpoint
conditions remember their ClangUserExpression and
re-use it.
- Third, I will tear out all the redundant code
(for example, materialization logic in
ClangExpressionDeclMap) that is no longer used.
While implementing this fix, I also found a bug in
IRForTarget's handling of floating-point constants.
This should be fixed.
llvm-svn: 179801
2013-04-19 06:06:33 +08:00
|
|
|
can_interpret,
|
This patch modifies the expression parser to allow it
to execute expressions even in the absence of a process.
This allows expressions to run in situations where the
target cannot run -- e.g., to perform calculations based
on type information, or to inspect a binary's static
data.
This modification touches the following files:
lldb-private-enumerations.h
Introduce a new enum specifying the policy for
processing an expression. Some expressions should
always be JITted, for example if they are functions
that will be used over and over again. Some
expressions should always be interpreted, for
example if the target is unsafe to run. For most,
it is acceptable to JIT them, but interpretation
is preferable when possible.
Target.[h,cpp]
Have EvaluateExpression now accept the new enum.
ClangExpressionDeclMap.[cpp,h]
Add support for the IR interpreter and also make
the ClangExpressionDeclMap more robust in the
absence of a process.
ClangFunction.[cpp,h]
Add support for the new enum.
IRInterpreter.[cpp,h]
New implementation.
ClangUserExpression.[cpp,h]
Add support for the new enum, and for running
expressions in the absence of a process.
ClangExpression.h
Remove references to the old DWARF-based method
of evaluating expressions, because it has been
superseded for now.
ClangUtilityFunction.[cpp,h]
Add support for the new enum.
ClangExpressionParser.[cpp,h]
Add support for the new enum, remove references
to DWARF, and add support for checking whether
the expression could be evaluated statically.
IRForTarget.[h,cpp]
Add support for the new enum, and add utility
functions to support the interpreter.
IRToDWARF.cpp
Removed
CommandObjectExpression.cpp
Remove references to the obsolete -i option.
Process.cpp
Modify calls to ClangUserExpression::Evaluate
to pass the correct enum (for dlopen/dlclose)
SBValue.cpp
Add support for the new enum.
SBFrame.cpp
Add support for he new enum.
BreakpointOptions.cpp
Add support for the new enum.
llvm-svn: 139772
2011-09-15 10:13:07 +08:00
|
|
|
eExecutionPolicyAlways);
|
2011-05-24 05:40:23 +08:00
|
|
|
|
2011-09-22 12:58:26 +08:00
|
|
|
if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
|
2012-09-18 08:08:47 +08:00
|
|
|
m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
|
2010-08-28 07:31:21 +08:00
|
|
|
|
2011-01-19 03:36:39 +08:00
|
|
|
#if 0
|
|
|
|
// jingham: look here
|
|
|
|
StreamFile logfile ("/tmp/exprs.txt", "a");
|
2012-11-30 05:49:15 +08:00
|
|
|
logfile.Printf ("0x%16.16" PRIx64 ": func = %s, source =\n%s\n",
|
2011-01-20 07:00:49 +08:00
|
|
|
m_jit_start_addr,
|
2011-01-19 03:36:39 +08:00
|
|
|
m_function_name.c_str(),
|
|
|
|
m_function_text.c_str());
|
|
|
|
#endif
|
|
|
|
|
2010-12-03 09:38:59 +08:00
|
|
|
m_expr_decl_map->DidParse();
|
|
|
|
|
2010-09-14 05:34:21 +08:00
|
|
|
m_expr_decl_map.reset();
|
|
|
|
|
2010-08-28 07:31:21 +08:00
|
|
|
if (jit_error.Success())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-17 11:51:29 +08:00
|
|
|
const char *error_cstr = jit_error.AsCString();
|
|
|
|
if (error_cstr && error_cstr[0])
|
|
|
|
error_stream.Printf ("error: %s\n", error_cstr);
|
|
|
|
else
|
2011-09-21 05:44:10 +08:00
|
|
|
error_stream.Printf ("error: expression can't be interpreted or run\n");
|
2010-08-28 07:31:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|