diff --git a/lib/DebugHelper/DebugHelper.cpp b/lib/DebugHelper/DebugHelper.cpp index 902904103..d50d79782 100644 --- a/lib/DebugHelper/DebugHelper.cpp +++ b/lib/DebugHelper/DebugHelper.cpp @@ -51,6 +51,20 @@ static MDString *getMD(const Instruction *Instruction, unsigned Kind) { return String; } +static void replaceAll(std::string &Input, + const std::string &From, + const std::string &To) { + if(From.empty()) + return; + + size_t Start = 0; + while((Start = Input.find(From, Start)) != std::string::npos) { + Input.replace(Start, From.length(), To); + Start += To.length(); + } + +} + /// Writes the text contained in the metadata with the specified kind ID to the /// output stream, unless that metadata is exactly the same as in the previous /// instruction. @@ -71,8 +85,11 @@ static void writeMetadataIfNew(const Instruction *TheInstruction, } } while (TheInstruction != nullptr && PrevMD == nullptr); - if (TheInstruction == nullptr || PrevMD != MD) - Output << Prefix << MD->getString(); + if (TheInstruction == nullptr || PrevMD != MD) { + std::string Text = MD->getString().str(); + replaceAll(Text, "\n", " "); + Output << Prefix << Text << "\n"; + } } } diff --git a/tools/revamb-dump/IsolateFunctions.cpp b/tools/revamb-dump/IsolateFunctions.cpp index 07d495cd0..fff1ba323 100644 --- a/tools/revamb-dump/IsolateFunctions.cpp +++ b/tools/revamb-dump/IsolateFunctions.cpp @@ -430,7 +430,8 @@ bool IFI::cloneInstruction(BasicBlock *NewBB, // Assert if we encounter a basic block and we don't find a // reference in the ValueToValueMap revng_assert(LocalVMap.count(CurrentOperand) != 0); - } else if (!isa(CurrentOperand)) { + } else if (!isa(CurrentOperand) + and !isa(CurrentOperand)) { // Manage values that are themself users (recursive exploration // of the operands) taking care of avoiding to add operands of // constants diff --git a/tools/revamb/InstructionTranslator.cpp b/tools/revamb/InstructionTranslator.cpp index ef3a1674c..d857cbfbf 100644 --- a/tools/revamb/InstructionTranslator.cpp +++ b/tools/revamb/InstructionTranslator.cpp @@ -515,7 +515,7 @@ void IT::finalizeNewPCMarkers(std::string &CoveragePath) { Call->setArgOperand(2, Builder.getInt32(static_cast(IsJT))); // TODO: by default we should leave these - for (unsigned I = 3; I < ArgCount - 1; I++) + for (unsigned I = 4; I < ArgCount - 1; I++) Call->setArgOperand(I, Call->getArgOperand(ArgCount - 1)); } } @@ -574,7 +574,7 @@ IT::newInstruction(PTCInstruction *Instr, uint64_t NextPC = Next != nullptr ? PTC::Instruction(Next).pc() : EndPC; std::stringstream OriginalStringStream; - disassembleOriginal(OriginalStringStream, PC); + disassemble(OriginalStringStream, PC, NextPC - PC); std::string OriginalString = OriginalStringStream.str(); LLVMContext &Context = TheModule.getContext(); MDString *MDOriginalString = MDString::get(Context, OriginalString); @@ -610,7 +610,8 @@ IT::newInstruction(PTCInstruction *Instr, // in case we have to split a basic block std::vector Args = { Builder.getInt64(PC), Builder.getInt64(NextPC - PC), - Builder.getInt32(-1) }; + Builder.getInt32(-1), + MetadataAsValue::get(Context, MDOriginalString) }; for (AllocaInst *Local : Variables.locals()) Args.push_back(Local); diff --git a/tools/revamb/PTCDump.cpp b/tools/revamb/PTCDump.cpp index 74d692440..3276bd94c 100644 --- a/tools/revamb/PTCDump.cpp +++ b/tools/revamb/PTCDump.cpp @@ -243,7 +243,10 @@ int dumpInstruction(std::ostream &Result, return EXIT_SUCCESS; } -void disassembleOriginal(std::ostream &Result, uint64_t PC) { +void disassemble(std::ostream &Result, + uint64_t PC, + uint32_t MaxBytes, + uint32_t InstructionCount) { char *BufferPtr = nullptr; size_t BufferLenPtr = 0; FILE *MemoryStream = open_memstream(&BufferPtr, &BufferLenPtr); @@ -252,7 +255,7 @@ void disassembleOriginal(std::ostream &Result, uint64_t PC) { // Using SIZE_MAX is not very nice but the code should disassemble only a // single instruction nonetheless. - ptc.disassemble(MemoryStream, PC, SIZE_MAX, 1); + ptc.disassemble(MemoryStream, PC, MaxBytes, InstructionCount); fflush(MemoryStream); revng_assert(BufferPtr != nullptr); @@ -277,7 +280,7 @@ int dumpTranslation(std::ostream &Result, PTCInstructionList *Instructions) { if (is64) PC |= Instruction.args[1] << 32; - disassembleOriginal(Result, PC); + disassemble(Result, PC); } Result << std::dec << Index << ": "; diff --git a/tools/revamb/PTCDump.h b/tools/revamb/PTCDump.h index 23cb67b5a..2058fbe3b 100644 --- a/tools/revamb/PTCDump.h +++ b/tools/revamb/PTCDump.h @@ -37,6 +37,11 @@ int dumpTranslation(std::ostream &Result, PTCInstructionList *Instructions); /// /// \param Result the output stream /// \param PC the program counter in the current context. -void disassembleOriginal(std::ostream &Result, uint64_t PC); +/// \param MaxSize the maximum number of bytes to disassemble. +/// \param InstructionCount the maximum number of instructions to disassemble. +void disassemble(std::ostream &Result, + uint64_t PC, + uint32_t MaxBytes=4096, + uint32_t InstructionCount=4096); #endif // PTCDUMP_H