Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0fef968

Browse files
author
Greg Clayton
committedMay 10, 2012
<rdar://problem/11330621>
Fixed the DisassemblerLLVMC disassembler to parse more efficiently instead of parsing opcodes over and over. The InstructionLLVMC class now only reads the opcode in the InstructionLLVMC::Decode function. This can be done very efficiently for ARM and architectures that have fixed opcode sizes. For x64 it still calls the disassembler to get the byte size. Moved the lldb_private::Instruction::Dump(...) function up into the lldb_private::Instruction class and it now uses the function that gets the mnemonic, operandes and comments so that all disassembly is using the same code. Added StreamString::FillLastLineToColumn() to allow filling a line up to a column with a character (which is used by the lldb_private::Instruction::Dump(...) function). Modified the Opcode::GetData() fucntion to "do the right thing" for thumb instructions. git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@156532 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 038fa8e commit 0fef968

File tree

17 files changed

+487
-701
lines changed

17 files changed

+487
-701
lines changed
 

‎include/lldb/Core/Disassembler.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ class Instruction
4242
}
4343

4444
const char *
45-
GetMnemonic (ExecutionContextScope *exe_scope)
45+
GetMnemonic (const ExecutionContext* exe_ctx)
4646
{
47-
CalculateMnemonicOperandsAndCommentIfNeeded (exe_scope);
47+
CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
4848
return m_opcode_name.c_str();
4949
}
5050
const char *
51-
GetOperands (ExecutionContextScope *exe_scope)
51+
GetOperands (const ExecutionContext* exe_ctx)
5252
{
53-
CalculateMnemonicOperandsAndCommentIfNeeded (exe_scope);
53+
CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
5454
return m_mnemocics.c_str();
5555
}
5656

5757
const char *
58-
GetComment (ExecutionContextScope *exe_scope)
58+
GetComment (const ExecutionContext* exe_ctx)
5959
{
60-
CalculateMnemonicOperandsAndCommentIfNeeded (exe_scope);
60+
CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
6161
return m_comment.c_str();
6262
}
6363

6464
virtual void
65-
CalculateMnemonicOperandsAndComment (ExecutionContextScope *exe_scope) = 0;
65+
CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx) = 0;
6666

6767
lldb::AddressClass
6868
GetAddressClass ();
@@ -81,8 +81,7 @@ class Instruction
8181
uint32_t max_opcode_byte_size,
8282
bool show_address,
8383
bool show_bytes,
84-
const ExecutionContext *exe_ctx,
85-
bool raw) = 0;
84+
const ExecutionContext* exe_ctx);
8685

8786
virtual bool
8887
DoesBranch () const = 0;
@@ -121,6 +120,9 @@ class Instruction
121120
{
122121
return m_opcode;
123122
}
123+
124+
uint32_t
125+
GetData (DataExtractor &data);
124126

125127
protected:
126128
Address m_address; // The section offset address of this instruction
@@ -130,20 +132,22 @@ class Instruction
130132
// The usual value will be eAddressClassCode, but often when
131133
// disassembling memory, you might run into data. This can
132134
// help us to disassemble appropriately.
133-
lldb::AddressClass m_address_class;
135+
private:
136+
lldb::AddressClass m_address_class; // Use GetAddressClass () accessor function!
137+
protected:
134138
Opcode m_opcode; // The opcode for this instruction
135139
std::string m_opcode_name;
136140
std::string m_mnemocics;
137141
std::string m_comment;
138142
bool m_calculated_strings;
139143

140144
void
141-
CalculateMnemonicOperandsAndCommentIfNeeded (ExecutionContextScope *exe_scope)
145+
CalculateMnemonicOperandsAndCommentIfNeeded (const ExecutionContext* exe_ctx)
142146
{
143147
if (!m_calculated_strings)
144148
{
145149
m_calculated_strings = true;
146-
CalculateMnemonicOperandsAndComment(exe_scope);
150+
CalculateMnemonicOperandsAndComment(exe_ctx);
147151
}
148152
}
149153
};
@@ -200,19 +204,11 @@ class PseudoInstruction :
200204
virtual
201205
~PseudoInstruction ();
202206

203-
virtual void
204-
Dump (Stream *s,
205-
uint32_t max_opcode_byte_size,
206-
bool show_address,
207-
bool show_bytes,
208-
const ExecutionContext* exe_ctx,
209-
bool raw);
210-
211207
virtual bool
212208
DoesBranch () const;
213209

214210
virtual void
215-
CalculateMnemonicOperandsAndComment (ExecutionContextScope *exe_scope)
211+
CalculateMnemonicOperandsAndComment (const ExecutionContext* exe_ctx)
216212
{
217213
// TODO: fill this in and put opcode name into Instruction::m_opcode_name,
218214
// mnemonic into Instruction::m_mnemonics, and any comment into

‎include/lldb/Core/Opcode.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ namespace lldb_private {
6767
SetOpcodeBytes (bytes, length);
6868
}
6969

70+
void
71+
Clear()
72+
{
73+
m_type = Opcode::eTypeInvalid;
74+
}
7075
Opcode::Type
7176
GetType () const
7277
{
@@ -205,9 +210,10 @@ namespace lldb_private {
205210
return 0;
206211
}
207212

213+
// Get the opcode exactly as it would be laid out in memory.
208214
uint32_t
209-
GetData (DataExtractor &data) const;
210-
215+
GetData (DataExtractor &data,
216+
lldb::AddressClass address_class) const;
211217

212218
protected:
213219

@@ -240,8 +246,8 @@ namespace lldb_private {
240246
uint64_t inst64;
241247
struct
242248
{
243-
uint8_t length;
244249
uint8_t bytes[16]; // This must be big enough to handle any opcode for any supported target.
250+
uint8_t length;
245251
} inst;
246252
} m_data;
247253
};

‎include/lldb/Core/StreamString.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class StreamString : public Stream
4949
const std::string &
5050
GetString() const;
5151

52+
void
53+
FillLastLineToColumn (uint32_t column, char fill_char);
54+
5255
protected:
5356
std::string m_packet;
5457

‎lldb.xcodeproj/xcshareddata/xcschemes/lldb-tool.xcscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@
106106
isEnabled = "NO">
107107
</CommandLineArgument>
108108
<CommandLineArgument
109-
argument = "/Volumes/work/gclayton/Documents/devb/attach/a.out"
110-
isEnabled = "NO">
109+
argument = "~/Documents/src/args/a.out"
110+
isEnabled = "YES">
111111
</CommandLineArgument>
112112
<CommandLineArgument
113113
argument = "/bin/cat"

‎source/API/SBInstruction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ SBInstruction::GetMnemonic(SBTarget target)
8282
target_sp->CalculateExecutionContext (exe_ctx);
8383
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
8484
}
85-
return m_opaque_sp->GetMnemonic(exe_ctx.GetBestExecutionContextScope());
85+
return m_opaque_sp->GetMnemonic(&exe_ctx);
8686
}
8787
return NULL;
8888
}
@@ -101,7 +101,7 @@ SBInstruction::GetOperands(SBTarget target)
101101
target_sp->CalculateExecutionContext (exe_ctx);
102102
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
103103
}
104-
return m_opaque_sp->GetOperands(exe_ctx.GetBestExecutionContextScope());
104+
return m_opaque_sp->GetOperands(&exe_ctx);
105105
}
106106
return NULL;
107107
}
@@ -120,7 +120,7 @@ SBInstruction::GetComment(SBTarget target)
120120
target_sp->CalculateExecutionContext (exe_ctx);
121121
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
122122
}
123-
return m_opaque_sp->GetComment(exe_ctx.GetBestExecutionContextScope());
123+
return m_opaque_sp->GetComment(&exe_ctx);
124124
}
125125
return NULL;
126126
}
@@ -140,7 +140,7 @@ SBInstruction::GetData (SBTarget target)
140140
if (m_opaque_sp)
141141
{
142142
DataExtractorSP data_extractor_sp (new DataExtractor());
143-
if (m_opaque_sp->GetOpcode().GetData (*data_extractor_sp))
143+
if (m_opaque_sp->GetData (*data_extractor_sp))
144144
{
145145
sb_data.SetOpaque (data_extractor_sp);
146146
}
@@ -171,7 +171,7 @@ SBInstruction::GetDescription (lldb::SBStream &s)
171171
{
172172
// Use the "ref()" instead of the "get()" accessor in case the SBStream
173173
// didn't have a stream already created, one will get created...
174-
m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
174+
m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL);
175175
return true;
176176
}
177177
return false;
@@ -186,7 +186,7 @@ SBInstruction::Print (FILE *out)
186186
if (m_opaque_sp)
187187
{
188188
StreamFile out_stream (out, false);
189-
m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
189+
m_opaque_sp->Dump (&out_stream, 0, true, false, NULL);
190190
}
191191
}
192192

‎source/API/SBInstructionList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ SBInstructionList::GetDescription (lldb::SBStream &description)
105105
Instruction *inst = m_opaque_sp->GetInstructionList().GetInstructionAtIndex (i).get();
106106
if (inst == NULL)
107107
break;
108-
inst->Dump (&sref, max_opcode_byte_size, true, false, NULL, false);
108+
inst->Dump (&sref, max_opcode_byte_size, true, false, NULL);
109109
sref.EOL();
110110
}
111111
return true;

‎source/Core/Disassembler.cpp

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ Disassembler::PrintInstructions
448448
strm.PutCString(inst_is_at_pc ? "-> " : " ");
449449
}
450450
const bool show_bytes = (options & eOptionShowBytes) != 0;
451-
const bool raw = (options & eOptionRawOuput) != 0;
452-
inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx, raw);
451+
inst->Dump(&strm, max_opcode_byte_size, true, show_bytes, &exe_ctx);
453452
strm.EOL();
454453
}
455454
else
@@ -529,6 +528,69 @@ Instruction::GetAddressClass ()
529528
return m_address_class;
530529
}
531530

531+
void
532+
Instruction::Dump (lldb_private::Stream *s,
533+
uint32_t max_opcode_byte_size,
534+
bool show_address,
535+
bool show_bytes,
536+
const ExecutionContext* exe_ctx)
537+
{
538+
const size_t opcode_column_width = 7;
539+
const size_t operand_column_width = 25;
540+
541+
CalculateMnemonicOperandsAndCommentIfNeeded (exe_ctx);
542+
543+
StreamString ss;
544+
545+
if (show_address)
546+
{
547+
m_address.Dump(&ss,
548+
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL,
549+
Address::DumpStyleLoadAddress,
550+
Address::DumpStyleModuleWithFileAddress,
551+
0);
552+
553+
ss.PutCString(": ");
554+
}
555+
556+
if (show_bytes)
557+
{
558+
if (m_opcode.GetType() == Opcode::eTypeBytes)
559+
{
560+
// x86_64 and i386 are the only ones that use bytes right now so
561+
// pad out the byte dump to be able to always show 15 bytes (3 chars each)
562+
// plus a space
563+
if (max_opcode_byte_size > 0)
564+
m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
565+
else
566+
m_opcode.Dump (&ss, 15 * 3 + 1);
567+
}
568+
else
569+
{
570+
// Else, we have ARM which can show up to a uint32_t 0x00000000 (10 spaces)
571+
// plus two for padding...
572+
if (max_opcode_byte_size > 0)
573+
m_opcode.Dump (&ss, max_opcode_byte_size * 3 + 1);
574+
else
575+
m_opcode.Dump (&ss, 12);
576+
}
577+
}
578+
579+
const size_t opcode_pos = ss.GetSize();
580+
581+
ss.PutCString (m_opcode_name.c_str());
582+
ss.FillLastLineToColumn (opcode_pos + opcode_column_width, ' ');
583+
ss.PutCString (m_mnemocics.c_str());
584+
585+
if (!m_comment.empty())
586+
{
587+
ss.FillLastLineToColumn (opcode_pos + opcode_column_width + operand_column_width, ' ');
588+
ss.PutCString (" ; ");
589+
ss.PutCString (m_comment.c_str());
590+
}
591+
s->Write (ss.GetData(), ss.GetSize());
592+
}
593+
532594
bool
533595
Instruction::DumpEmulation (const ArchSpec &arch)
534596
{
@@ -828,6 +890,13 @@ Instruction::Emulate (const ArchSpec &arch,
828890
return false;
829891
}
830892

893+
894+
uint32_t
895+
Instruction::GetData (DataExtractor &data)
896+
{
897+
return m_opcode.GetData(data, GetAddressClass ());
898+
}
899+
831900
InstructionList::InstructionList() :
832901
m_instructions()
833902
{
@@ -884,7 +953,7 @@ InstructionList::Dump (Stream *s,
884953
{
885954
if (pos != begin)
886955
s->EOL();
887-
(*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx, false);
956+
(*pos)->Dump(s, max_opcode_byte_size, show_address, show_bytes, exe_ctx);
888957
}
889958
}
890959

@@ -1065,27 +1134,6 @@ PseudoInstruction::~PseudoInstruction ()
10651134
{
10661135
}
10671136

1068-
void
1069-
PseudoInstruction::Dump (lldb_private::Stream *s,
1070-
uint32_t max_opcode_byte_size,
1071-
bool show_address,
1072-
bool show_bytes,
1073-
const lldb_private::ExecutionContext* exe_ctx,
1074-
bool raw)
1075-
{
1076-
if (!s)
1077-
return;
1078-
1079-
if (show_bytes)
1080-
m_opcode.Dump (s, max_opcode_byte_size);
1081-
1082-
if (m_description.size() > 0)
1083-
s->Printf ("%s", m_description.c_str());
1084-
else
1085-
s->Printf ("<unknown>");
1086-
1087-
}
1088-
10891137
bool
10901138
PseudoInstruction::DoesBranch () const
10911139
{

0 commit comments

Comments
 (0)
This repository has been archived.