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