Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.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 | |
| 10 | #include "lldb/Expression/ClangExpressionParser.h" |
| 11 | |
| 12 | #include "lldb/Core/ArchSpec.h" |
| 13 | #include "lldb/Core/DataBufferHeap.h" |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Debugger.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Disassembler.h" |
| 16 | #include "lldb/Core/Stream.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamString.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 18 | #include "lldb/Expression/ClangASTSource.h" |
| 19 | #include "lldb/Expression/ClangExpression.h" |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/RecordingMemoryManager.h" |
| 23 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 24 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Process.h" |
| 26 | #include "lldb/Target/Target.h" |
| 27 | |
| 28 | #include "clang/AST/ASTContext.h" |
| 29 | #include "clang/AST/ExternalASTSource.h" |
| 30 | #include "clang/Basic/FileManager.h" |
| 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Basic/Version.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 33 | #include "clang/CodeGen/CodeGenAction.h" |
| 34 | #include "clang/CodeGen/ModuleBuilder.h" |
| 35 | #include "clang/Driver/CC1Options.h" |
| 36 | #include "clang/Driver/OptTable.h" |
| 37 | #include "clang/Frontend/CompilerInstance.h" |
| 38 | #include "clang/Frontend/CompilerInvocation.h" |
| 39 | #include "clang/Frontend/FrontendActions.h" |
| 40 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 41 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 42 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 43 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 44 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
| 45 | #include "clang/Lex/Preprocessor.h" |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 46 | #include "clang/Parse/ParseAST.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 47 | #include "clang/Rewrite/FrontendActions.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 48 | #include "clang/Sema/SemaConsumer.h" |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 49 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 50 | |
| 51 | #include "llvm/ADT/StringRef.h" |
| 52 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 53 | |
Peter Collingbourne | c3f5573 | 2011-06-03 20:40:12 +0000 | [diff] [blame] | 54 | #if !defined(__APPLE__) |
| 55 | #define USE_STANDARD_JIT |
| 56 | #endif |
| 57 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 58 | #if defined (USE_STANDARD_JIT) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 59 | #include "llvm/ExecutionEngine/JIT.h" |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 60 | #else |
| 61 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 62 | #endif |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 63 | #include "llvm/LLVMContext.h" |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 64 | #include "llvm/Module.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 65 | #include "llvm/Support/ErrorHandling.h" |
| 66 | #include "llvm/Support/MemoryBuffer.h" |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 67 | #include "llvm/Support/DynamicLibrary.h" |
| 68 | #include "llvm/Support/Host.h" |
| 69 | #include "llvm/Support/Signals.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 70 | #include "llvm/Target/TargetRegistry.h" |
| 71 | #include "llvm/Target/TargetSelect.h" |
| 72 | |
| 73 | using namespace clang; |
| 74 | using namespace llvm; |
| 75 | using namespace lldb_private; |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // Utility Methods for Clang |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 82 | llvm::sys::Path P = |
| 83 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 84 | (void*)(intptr_t) GetBuiltinIncludePath); |
| 85 | |
| 86 | if (!P.isEmpty()) { |
| 87 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 88 | P.eraseComponent(); // Remove /bin from foo/bin |
| 89 | |
| 90 | // Get foo/lib/clang/<version>/include |
| 91 | P.appendComponent("lib"); |
| 92 | P.appendComponent("clang"); |
| 93 | P.appendComponent(CLANG_VERSION_STRING); |
| 94 | P.appendComponent("include"); |
| 95 | } |
| 96 | |
| 97 | return P.str(); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // Main driver for Clang |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 106 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 107 | |
| 108 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 109 | |
| 110 | // We cannot recover from llvm errors. |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 115 | using namespace clang::frontend; |
| 116 | |
| 117 | switch (CI.getFrontendOpts().ProgramAction) { |
| 118 | default: |
| 119 | llvm_unreachable("Invalid program action!"); |
| 120 | |
| 121 | case ASTDump: return new ASTDumpAction(); |
| 122 | case ASTPrint: return new ASTPrintAction(); |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 123 | case ASTDumpXML: return new ASTDumpXMLAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 124 | case ASTView: return new ASTViewAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 125 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 126 | case DumpTokens: return new DumpTokensAction(); |
| 127 | case EmitAssembly: return new EmitAssemblyAction(); |
| 128 | case EmitBC: return new EmitBCAction(); |
| 129 | case EmitHTML: return new HTMLPrintAction(); |
| 130 | case EmitLLVM: return new EmitLLVMAction(); |
| 131 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 132 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 133 | case EmitObj: return new EmitObjAction(); |
| 134 | case FixIt: return new FixItAction(); |
| 135 | case GeneratePCH: return new GeneratePCHAction(); |
| 136 | case GeneratePTH: return new GeneratePTHAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 137 | case InitOnly: return new InitOnlyAction(); |
| 138 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 139 | |
| 140 | case PluginAction: { |
| 141 | for (FrontendPluginRegistry::iterator it = |
| 142 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 143 | it != ie; ++it) { |
| 144 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
| 145 | llvm::OwningPtr<PluginASTAction> P(it->instantiate()); |
| 146 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 147 | return 0; |
| 148 | return P.take(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 153 | << CI.getFrontendOpts().ActionName; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 158 | case PrintPreamble: return new PrintPreambleAction(); |
| 159 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 160 | case RewriteMacros: return new RewriteMacrosAction(); |
| 161 | case RewriteObjC: return new RewriteObjCAction(); |
| 162 | case RewriteTest: return new RewriteTestAction(); |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 163 | //case RunAnalysis: return new AnalysisAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 164 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 169 | // Create the underlying action. |
| 170 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 171 | if (!Act) |
| 172 | return 0; |
| 173 | |
| 174 | // If there are any AST files to merge, create a frontend action |
| 175 | // adaptor to perform the merge. |
| 176 | if (!CI.getFrontendOpts().ASTMergeFiles.empty()) |
| 177 | Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0], |
| 178 | CI.getFrontendOpts().ASTMergeFiles.size()); |
| 179 | |
| 180 | return Act; |
| 181 | } |
| 182 | |
| 183 | //===----------------------------------------------------------------------===// |
| 184 | // Implementation of ClangExpressionParser |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 187 | ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope, |
| 188 | ClangExpression &expr) : |
| 189 | m_expr (expr), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 190 | m_compiler (), |
| 191 | m_code_generator (NULL), |
| 192 | m_execution_engine (), |
| 193 | m_jitted_functions () |
| 194 | { |
| 195 | // Initialize targets first, so that --version shows registered targets. |
| 196 | static struct InitializeLLVM { |
| 197 | InitializeLLVM() { |
| 198 | llvm::InitializeAllTargets(); |
| 199 | llvm::InitializeAllAsmPrinters(); |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 200 | llvm::InitializeAllTargetMCs(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 201 | } |
| 202 | } InitializeLLVM; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 203 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 204 | // 1. Create a new compiler instance. |
| 205 | m_compiler.reset(new CompilerInstance()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 206 | |
| 207 | // 2. Set options. |
| 208 | |
| 209 | // Parse expressions as Objective C++ regardless of context. |
| 210 | // Our hook into Clang's lookup mechanism only works in C++. |
| 211 | m_compiler->getLangOpts().CPlusPlus = true; |
Greg Clayton | f51ed30 | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 212 | |
| 213 | // Setup objective C |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 214 | m_compiler->getLangOpts().ObjC1 = true; |
Greg Clayton | f51ed30 | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 215 | m_compiler->getLangOpts().ObjC2 = true; |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 216 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 217 | Process *process = NULL; |
| 218 | if (exe_scope) |
| 219 | process = exe_scope->CalculateProcess(); |
| 220 | |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 221 | if (process) |
| 222 | { |
| 223 | if (process->GetObjCLanguageRuntime()) |
| 224 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 225 | if (process->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2) |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 226 | { |
| 227 | m_compiler->getLangOpts().ObjCNonFragileABI = true; // NOT i386 |
| 228 | m_compiler->getLangOpts().ObjCNonFragileABI2 = true; // NOT i386 |
| 229 | } |
| 230 | } |
| 231 | } |
Greg Clayton | f51ed30 | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 232 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 233 | m_compiler->getLangOpts().ThreadsafeStatics = false; |
| 234 | m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access |
| 235 | m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name |
Sean Callanan | 2431244 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 236 | //m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 237 | |
| 238 | // Set CodeGen options |
| 239 | m_compiler->getCodeGenOpts().EmitDeclMetadata = true; |
| 240 | m_compiler->getCodeGenOpts().InstrumentFunctions = false; |
| 241 | |
| 242 | // Disable some warnings. |
| 243 | m_compiler->getDiagnosticOpts().Warnings.push_back("no-unused-value"); |
| 244 | |
| 245 | // Set the target triple. |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 246 | Target *target = NULL; |
| 247 | if (exe_scope) |
| 248 | target = exe_scope->CalculateTarget(); |
| 249 | |
| 250 | // TODO: figure out what to really do when we don't have a valid target. |
| 251 | // Sometimes this will be ok to just use the host target triple (when we |
| 252 | // evaluate say "2+3", but other expressions like breakpoint conditions |
| 253 | // and other things that _are_ target specific really shouldn't just be |
| 254 | // using the host triple. This needs to be fixed in a better way. |
| 255 | if (target && target->GetArchitecture().IsValid()) |
Sean Callanan | 2a8c338 | 2011-04-14 02:01:31 +0000 | [diff] [blame] | 256 | { |
| 257 | std::string triple = target->GetArchitecture().GetTriple().str(); |
| 258 | |
| 259 | int dash_count = 0; |
Johnny Chen | 2bc9eb3 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 260 | for (size_t i = 0; i < triple.size(); ++i) |
Sean Callanan | 2a8c338 | 2011-04-14 02:01:31 +0000 | [diff] [blame] | 261 | { |
| 262 | if (triple[i] == '-') |
| 263 | dash_count++; |
| 264 | if (dash_count == 3) |
| 265 | { |
| 266 | triple.resize(i); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | m_compiler->getTargetOpts().Triple = triple; |
| 272 | } |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 273 | else |
Sean Callanan | 2a8c338 | 2011-04-14 02:01:31 +0000 | [diff] [blame] | 274 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 275 | m_compiler->getTargetOpts().Triple = llvm::sys::getHostTriple(); |
Sean Callanan | 2a8c338 | 2011-04-14 02:01:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 278 | // 3. Set up various important bits of infrastructure. |
| 279 | m_compiler->createDiagnostics(0, 0); |
| 280 | |
| 281 | // Create the target instance. |
| 282 | m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), |
| 283 | m_compiler->getTargetOpts())); |
| 284 | |
| 285 | assert (m_compiler->hasTarget()); |
| 286 | |
| 287 | // Inform the target of the language options |
| 288 | // |
| 289 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 290 | // created. This complexity should be lifted elsewhere. |
| 291 | m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts()); |
| 292 | |
| 293 | // 4. Set up the diagnostic buffer for reporting errors |
| 294 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 295 | m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 296 | |
| 297 | // 5. Set up the source management objects inside the compiler |
| 298 | |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 299 | clang::FileSystemOptions file_system_options; |
| 300 | m_file_manager.reset(new clang::FileManager(file_system_options)); |
Sean Callanan | 8a3b0a8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 301 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 302 | if (!m_compiler->hasSourceManager()) |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 303 | m_compiler->createSourceManager(*m_file_manager.get()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 304 | |
| 305 | m_compiler->createFileManager(); |
| 306 | m_compiler->createPreprocessor(); |
| 307 | |
| 308 | // 6. Most of this we get from the CompilerInstance, but we |
| 309 | // also want to give the context an ExternalASTSource. |
Sean Callanan | ee8fc72 | 2010-11-19 20:20:02 +0000 | [diff] [blame] | 310 | m_selector_table.reset(new SelectorTable()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 311 | m_builtin_context.reset(new Builtin::Context(m_compiler->getTarget())); |
| 312 | |
| 313 | std::auto_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(), |
| 314 | m_compiler->getSourceManager(), |
| 315 | m_compiler->getTarget(), |
| 316 | m_compiler->getPreprocessor().getIdentifierTable(), |
Sean Callanan | ee8fc72 | 2010-11-19 20:20:02 +0000 | [diff] [blame] | 317 | *m_selector_table.get(), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 318 | *m_builtin_context.get(), |
| 319 | 0)); |
| 320 | |
| 321 | ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); |
| 322 | |
| 323 | if (decl_map) |
| 324 | { |
| 325 | OwningPtr<clang::ExternalASTSource> ast_source(new ClangASTSource(*ast_context, *decl_map)); |
| 326 | ast_context->setExternalSource(ast_source); |
| 327 | } |
| 328 | |
| 329 | m_compiler->setASTContext(ast_context.release()); |
| 330 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 331 | std::string module_name("$__lldb_module"); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 332 | |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 333 | m_llvm_context.reset(new LLVMContext()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 334 | m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(), |
| 335 | module_name, |
| 336 | m_compiler->getCodeGenOpts(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 337 | *m_llvm_context)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | ClangExpressionParser::~ClangExpressionParser() |
| 341 | { |
| 342 | } |
| 343 | |
| 344 | unsigned |
| 345 | ClangExpressionParser::Parse (Stream &stream) |
| 346 | { |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 347 | TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient()); |
| 348 | |
| 349 | diag_buf->FlushDiagnostics (m_compiler->getDiagnostics()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 350 | |
| 351 | MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__); |
| 352 | FileID memory_buffer_file_id = m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); |
| 353 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 354 | diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 355 | |
| 356 | ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get()); |
| 357 | |
| 358 | if (ast_transformer) |
| 359 | ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext()); |
| 360 | else |
| 361 | ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext()); |
| 362 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 363 | diag_buf->EndSourceFile(); |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 364 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 365 | TextDiagnosticBuffer::const_iterator diag_iterator; |
| 366 | |
| 367 | int num_errors = 0; |
Sean Callanan | 7617c29 | 2010-11-01 20:28:09 +0000 | [diff] [blame] | 368 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 369 | for (diag_iterator = diag_buf->warn_begin(); |
| 370 | diag_iterator != diag_buf->warn_end(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 371 | ++diag_iterator) |
| 372 | stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); |
| 373 | |
| 374 | num_errors = 0; |
| 375 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 376 | for (diag_iterator = diag_buf->err_begin(); |
| 377 | diag_iterator != diag_buf->err_end(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 378 | ++diag_iterator) |
| 379 | { |
| 380 | num_errors++; |
| 381 | stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); |
| 382 | } |
| 383 | |
Sean Callanan | 7617c29 | 2010-11-01 20:28:09 +0000 | [diff] [blame] | 384 | for (diag_iterator = diag_buf->note_begin(); |
| 385 | diag_iterator != diag_buf->note_end(); |
| 386 | ++diag_iterator) |
| 387 | stream.Printf("note: %s\n", (*diag_iterator).second.c_str()); |
| 388 | |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 389 | if (!num_errors) |
| 390 | { |
| 391 | if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes()) |
| 392 | { |
| 393 | stream.Printf("error: Couldn't infer the type of a variable\n"); |
| 394 | num_errors++; |
| 395 | } |
| 396 | } |
| 397 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 398 | return num_errors; |
| 399 | } |
| 400 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 401 | static bool FindFunctionInModule (std::string &mangled_name, |
| 402 | llvm::Module *module, |
| 403 | const char *orig_name) |
| 404 | { |
| 405 | for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end(); |
| 406 | fi != fe; |
| 407 | ++fi) |
| 408 | { |
| 409 | if (fi->getName().str().find(orig_name) != std::string::npos) |
| 410 | { |
| 411 | mangled_name = fi->getName().str(); |
| 412 | return true; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return false; |
| 417 | } |
| 418 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 419 | Error |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 420 | ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_allocation_addr, |
| 421 | lldb::addr_t &func_addr, |
| 422 | lldb::addr_t &func_end, |
| 423 | ExecutionContext &exe_ctx, |
| 424 | IRForTarget::StaticDataAllocator *data_allocator, |
| 425 | bool &evaluated_statically, |
| 426 | lldb::ClangExpressionVariableSP &const_result, |
| 427 | ExecutionPolicy execution_policy) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 428 | { |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 429 | func_allocation_addr = LLDB_INVALID_ADDRESS; |
| 430 | func_addr = LLDB_INVALID_ADDRESS; |
| 431 | func_end = LLDB_INVALID_ADDRESS; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 432 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 433 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 434 | Error err; |
| 435 | |
| 436 | llvm::Module *module = m_code_generator->ReleaseModule(); |
| 437 | |
| 438 | if (!module) |
| 439 | { |
| 440 | err.SetErrorToGenericError(); |
| 441 | err.SetErrorString("IR doesn't contain a module"); |
| 442 | return err; |
| 443 | } |
| 444 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 445 | // Find the actual name of the function (it's often mangled somehow) |
| 446 | |
| 447 | std::string function_name; |
| 448 | |
| 449 | if (!FindFunctionInModule(function_name, module, m_expr.FunctionName())) |
| 450 | { |
| 451 | err.SetErrorToGenericError(); |
| 452 | err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName()); |
| 453 | return err; |
| 454 | } |
| 455 | else |
| 456 | { |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 457 | if (log) |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 458 | log->Printf("Found function %s for %s", function_name.c_str(), m_expr.FunctionName()); |
| 459 | } |
| 460 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 461 | ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL |
| 462 | |
| 463 | if (decl_map) |
| 464 | { |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 465 | Stream *error_stream = NULL; |
| 466 | |
| 467 | if (exe_ctx.target) |
| 468 | error_stream = &exe_ctx.target->GetDebugger().GetErrorStream(); |
| 469 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 470 | IRForTarget ir_for_target(decl_map, |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 471 | m_expr.NeedsVariableResolution(), |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 472 | execution_policy, |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 473 | const_result, |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 474 | data_allocator, |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 475 | error_stream, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 476 | function_name.c_str()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 477 | |
| 478 | if (!ir_for_target.runOnModule(*module)) |
| 479 | { |
| 480 | err.SetErrorToGenericError(); |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 481 | err.SetErrorString("Couldn't prepare the expression for execution in the target"); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 482 | return err; |
| 483 | } |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 484 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 485 | if (execution_policy != eExecutionPolicyAlways && ir_for_target.interpretSuccess()) |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 486 | { |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 487 | evaluated_statically = true; |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 488 | err.Clear(); |
| 489 | return err; |
| 490 | } |
| 491 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame^] | 492 | if (!exe_ctx.process || execution_policy == eExecutionPolicyNever) |
| 493 | { |
| 494 | err.SetErrorToGenericError(); |
| 495 | err.SetErrorString("Execution needed to run in the target, but the target can't be run"); |
| 496 | return err; |
| 497 | } |
| 498 | |
| 499 | if (m_expr.NeedsValidation() && exe_ctx.process && exe_ctx.process->GetDynamicCheckers()) |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 500 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 501 | IRDynamicChecks ir_dynamic_checks(*exe_ctx.process->GetDynamicCheckers(), function_name.c_str()); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 502 | |
| 503 | if (!ir_dynamic_checks.runOnModule(*module)) |
| 504 | { |
| 505 | err.SetErrorToGenericError(); |
| 506 | err.SetErrorString("Couldn't add dynamic checks to the expression"); |
| 507 | return err; |
| 508 | } |
| 509 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 512 | // llvm will own this pointer when llvm::ExecutionEngine::createJIT is called |
| 513 | // below so we don't need to free it. |
| 514 | RecordingMemoryManager *jit_memory_manager = new RecordingMemoryManager(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 515 | |
| 516 | std::string error_string; |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 517 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 518 | #if defined (USE_STANDARD_JIT) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 519 | m_execution_engine.reset(llvm::ExecutionEngine::createJIT (module, |
| 520 | &error_string, |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 521 | jit_memory_manager, |
Sean Callanan | c2c6f77 | 2010-10-26 00:31:56 +0000 | [diff] [blame] | 522 | CodeGenOpt::Less, |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 523 | true, |
Peter Collingbourne | c4d4fb5 | 2011-07-30 22:42:24 +0000 | [diff] [blame] | 524 | Reloc::Default, |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 525 | CodeModel::Small)); |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 526 | #else |
| 527 | EngineBuilder builder(module); |
| 528 | builder.setEngineKind(EngineKind::JIT) |
| 529 | .setErrorStr(&error_string) |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 530 | .setRelocationModel(llvm::Reloc::PIC_) |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 531 | .setJITMemoryManager(jit_memory_manager) |
| 532 | .setOptLevel(CodeGenOpt::Less) |
| 533 | .setAllocateGVsWithCode(true) |
| 534 | .setCodeModel(CodeModel::Small) |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 535 | .setUseMCJIT(true); |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 536 | m_execution_engine.reset(builder.create()); |
| 537 | #endif |
Sean Callanan | 9ac3a96 | 2010-11-02 23:20:00 +0000 | [diff] [blame] | 538 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 539 | if (!m_execution_engine.get()) |
| 540 | { |
| 541 | err.SetErrorToGenericError(); |
| 542 | err.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); |
| 543 | return err; |
| 544 | } |
| 545 | |
| 546 | m_execution_engine->DisableLazyCompilation(); |
| 547 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 548 | llvm::Function *function = module->getFunction (function_name.c_str()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 549 | |
| 550 | // We don't actually need the function pointer here, this just forces it to get resolved. |
| 551 | |
| 552 | void *fun_ptr = m_execution_engine->getPointerToFunction(function); |
| 553 | |
| 554 | // Errors usually cause failures in the JIT, but if we're lucky we get here. |
| 555 | |
| 556 | if (!fun_ptr) |
| 557 | { |
| 558 | err.SetErrorToGenericError(); |
| 559 | err.SetErrorString("Couldn't JIT the function"); |
| 560 | return err; |
| 561 | } |
| 562 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 563 | m_jitted_functions.push_back (ClangExpressionParser::JittedFunction(function_name.c_str(), (lldb::addr_t)fun_ptr)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 564 | |
| 565 | ExecutionContext &exc_context(exe_ctx); |
| 566 | |
| 567 | if (exc_context.process == NULL) |
| 568 | { |
| 569 | err.SetErrorToGenericError(); |
| 570 | err.SetErrorString("Couldn't write the JIT compiled code into the target because there is no target"); |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | // Look over the regions allocated for the function compiled. The JIT |
| 575 | // tries to allocate the functions & stubs close together, so we should try to |
| 576 | // write them that way too... |
| 577 | // For now I only write functions with no stubs, globals, exception tables, |
| 578 | // etc. So I only need to write the functions. |
| 579 | |
| 580 | size_t alloc_size = 0; |
| 581 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 582 | std::map<uint8_t *, uint8_t *>::iterator fun_pos = jit_memory_manager->m_functions.begin(); |
| 583 | std::map<uint8_t *, uint8_t *>::iterator fun_end = jit_memory_manager->m_functions.end(); |
Greg Clayton | 9d2b321 | 2011-05-15 23:56:52 +0000 | [diff] [blame] | 584 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 585 | for (; fun_pos != fun_end; ++fun_pos) |
Greg Clayton | 9d2b321 | 2011-05-15 23:56:52 +0000 | [diff] [blame] | 586 | { |
| 587 | size_t mem_size = fun_pos->second - fun_pos->first; |
| 588 | if (log) |
| 589 | log->Printf ("JIT memory: [%p - %p) size = %zu", fun_pos->first, fun_pos->second, mem_size); |
| 590 | alloc_size += mem_size; |
| 591 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 592 | |
| 593 | Error alloc_error; |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 594 | func_allocation_addr = exc_context.process->AllocateMemory (alloc_size, |
| 595 | lldb::ePermissionsReadable|lldb::ePermissionsExecutable, |
| 596 | alloc_error); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 597 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 598 | if (func_allocation_addr == LLDB_INVALID_ADDRESS) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 599 | { |
| 600 | err.SetErrorToGenericError(); |
| 601 | err.SetErrorStringWithFormat("Couldn't allocate memory for the JITted function: %s", alloc_error.AsCString("unknown error")); |
| 602 | return err; |
| 603 | } |
| 604 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 605 | lldb::addr_t cursor = func_allocation_addr; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 606 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 607 | for (fun_pos = jit_memory_manager->m_functions.begin(); fun_pos != fun_end; fun_pos++) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 608 | { |
| 609 | lldb::addr_t lstart = (lldb::addr_t) (*fun_pos).first; |
| 610 | lldb::addr_t lend = (lldb::addr_t) (*fun_pos).second; |
| 611 | size_t size = lend - lstart; |
| 612 | |
| 613 | Error write_error; |
| 614 | |
| 615 | if (exc_context.process->WriteMemory(cursor, (void *) lstart, size, write_error) != size) |
| 616 | { |
| 617 | err.SetErrorToGenericError(); |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 618 | err.SetErrorStringWithFormat("Couldn't copy JIT code for function into the target: %s", write_error.AsCString("unknown error")); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 619 | return err; |
| 620 | } |
| 621 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 622 | jit_memory_manager->AddToLocalToRemoteMap (lstart, size, cursor); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 623 | cursor += size; |
| 624 | } |
| 625 | |
| 626 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 627 | |
| 628 | for (pos = m_jitted_functions.begin(); pos != end; pos++) |
| 629 | { |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 630 | (*pos).m_remote_addr = jit_memory_manager->GetRemoteAddressForLocal ((*pos).m_local_addr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 631 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 632 | if (!(*pos).m_name.compare(function_name.c_str())) |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 633 | { |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 634 | func_end = jit_memory_manager->GetRemoteRangeForLocal ((*pos).m_local_addr).second; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 635 | func_addr = (*pos).m_remote_addr; |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 636 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Sean Callanan | 6dff827 | 2010-11-08 03:49:50 +0000 | [diff] [blame] | 639 | if (log) |
| 640 | { |
| 641 | log->Printf("Code can be run in the target."); |
| 642 | |
| 643 | StreamString disassembly_stream; |
| 644 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 645 | Error err = DisassembleFunction(disassembly_stream, exe_ctx, jit_memory_manager); |
Sean Callanan | 6dff827 | 2010-11-08 03:49:50 +0000 | [diff] [blame] | 646 | |
| 647 | if (!err.Success()) |
| 648 | { |
| 649 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 654 | } |
| 655 | } |
| 656 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 657 | err.Clear(); |
| 658 | return err; |
| 659 | } |
| 660 | |
| 661 | Error |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 662 | ClangExpressionParser::DisassembleFunction (Stream &stream, ExecutionContext &exe_ctx, RecordingMemoryManager *jit_memory_manager) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 663 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 664 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 665 | |
| 666 | const char *name = m_expr.FunctionName(); |
| 667 | |
| 668 | Error ret; |
| 669 | |
| 670 | ret.Clear(); |
| 671 | |
| 672 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 673 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 674 | |
| 675 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 676 | |
| 677 | for (pos = m_jitted_functions.begin(); pos < end; pos++) |
| 678 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 679 | if (strstr(pos->m_name.c_str(), name)) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 680 | { |
| 681 | func_local_addr = pos->m_local_addr; |
| 682 | func_remote_addr = pos->m_remote_addr; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (func_local_addr == LLDB_INVALID_ADDRESS) |
| 687 | { |
| 688 | ret.SetErrorToGenericError(); |
| 689 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", name); |
| 690 | return ret; |
| 691 | } |
| 692 | |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 693 | if (log) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 694 | log->Printf("Found function, has local address 0x%llx and remote address 0x%llx", (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
| 695 | |
| 696 | std::pair <lldb::addr_t, lldb::addr_t> func_range; |
| 697 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 698 | func_range = jit_memory_manager->GetRemoteRangeForLocal(func_local_addr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 699 | |
| 700 | if (func_range.first == 0 && func_range.second == 0) |
| 701 | { |
| 702 | ret.SetErrorToGenericError(); |
| 703 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", name); |
| 704 | return ret; |
| 705 | } |
| 706 | |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 707 | if (log) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 708 | log->Printf("Function's code range is [0x%llx-0x%llx]", func_range.first, func_range.second); |
| 709 | |
| 710 | if (!exe_ctx.target) |
| 711 | { |
| 712 | ret.SetErrorToGenericError(); |
| 713 | ret.SetErrorString("Couldn't find the target"); |
| 714 | } |
| 715 | |
| 716 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second - func_remote_addr, 0)); |
| 717 | |
| 718 | Error err; |
| 719 | exe_ctx.process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); |
| 720 | |
| 721 | if (!err.Success()) |
| 722 | { |
| 723 | ret.SetErrorToGenericError(); |
| 724 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | ArchSpec arch(exe_ctx.target->GetArchitecture()); |
| 729 | |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 730 | Disassembler *disassembler = Disassembler::FindPlugin(arch, NULL); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 731 | |
| 732 | if (disassembler == NULL) |
| 733 | { |
| 734 | ret.SetErrorToGenericError(); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 735 | ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | if (!exe_ctx.process) |
| 740 | { |
| 741 | ret.SetErrorToGenericError(); |
| 742 | ret.SetErrorString("Couldn't find the process"); |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | DataExtractor extractor(buffer_sp, |
| 747 | exe_ctx.process->GetByteOrder(), |
| 748 | exe_ctx.target->GetArchitecture().GetAddressByteSize()); |
| 749 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 750 | if (log) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 751 | { |
| 752 | log->Printf("Function data has contents:"); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 753 | extractor.PutToLog (log.get(), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 754 | 0, |
| 755 | extractor.GetByteSize(), |
| 756 | func_remote_addr, |
| 757 | 16, |
| 758 | DataExtractor::TypeUInt8); |
| 759 | } |
| 760 | |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 761 | disassembler->DecodeInstructions (Address (NULL, func_remote_addr), extractor, 0, UINT32_MAX, false); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 762 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 763 | InstructionList &instruction_list = disassembler->GetInstructionList(); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 764 | const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 765 | for (uint32_t instruction_index = 0, num_instructions = instruction_list.GetSize(); |
| 766 | instruction_index < num_instructions; |
| 767 | ++instruction_index) |
| 768 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 769 | Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 770 | instruction->Dump (&stream, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 771 | max_opcode_byte_size, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 772 | true, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 773 | true, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 774 | &exe_ctx, |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 775 | true); |
| 776 | stream.PutChar('\n'); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return ret; |
| 780 | } |