Skip to content

Commit e7a9115

Browse files
committedOct 14, 2019
remove FILE* bindings from SBInstruction.
Summary: This patch replaces the FILE* python bindings for SBInstruction and SBInstructionList and replaces them with the new, safe SBFile and FileSP bindings. I also re-enable `Test_Disassemble_VST1_64`, because now we can use the file bindings as an additional test of the disassembler, and we can use the disassembler test as a test of the file bindings. The bugs referred to in the comments appear to have been fixed. The radar is closed now and the bugzilla bug does not reproduce with the instructions given. Reviewers: JDevlieghere, jasonmolenda, labath Reviewed By: labath Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D68890 llvm-svn: 374820
1 parent d88c7de commit e7a9115

File tree

9 files changed

+99
-21
lines changed

9 files changed

+99
-21
lines changed
 

‎lldb/include/lldb/API/SBInstruction.h

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class LLDB_API SBInstruction {
5555

5656
void Print(FILE *out);
5757

58+
void Print(SBFile out);
59+
60+
void Print(FileSP out);
61+
5862
bool GetDescription(lldb::SBStream &description);
5963

6064
bool EmulateWithFrame(lldb::SBFrame &frame, uint32_t evaluate_options);

‎lldb/include/lldb/API/SBInstructionList.h

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class LLDB_API SBInstructionList {
4646

4747
void Print(FILE *out);
4848

49+
void Print(SBFile out);
50+
51+
void Print(FileSP out);
52+
4953
bool GetDescription(lldb::SBStream &description);
5054

5155
bool DumpEmulationForAllInstructions(const char *triple);
@@ -56,6 +60,8 @@ class LLDB_API SBInstructionList {
5660
friend class SBTarget;
5761

5862
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp);
63+
bool GetDescription(lldb_private::Stream &description);
64+
5965

6066
private:
6167
lldb::DisassemblerSP m_opaque_sp;

‎lldb/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instruction.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ def fuzz_obj(obj):
99
obj.GetAddress()
1010
obj.GetByteSize()
1111
obj.DoesBranch()
12-
obj.Print(None)
12+
try:
13+
obj.Print(None)
14+
except Exception:
15+
pass
1316
obj.GetDescription(lldb.SBStream())
1417
obj.EmulateWithFrame(lldb.SBFrame(), 0)
1518
obj.DumpEmulation("armv7")

‎lldb/packages/Python/lldbsuite/test/python_api/default-constructor/sb_instructionlist.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ def fuzz_obj(obj):
99
obj.GetSize()
1010
obj.GetInstructionAtIndex(0xffffffff)
1111
obj.AppendInstruction(lldb.SBInstruction())
12-
obj.Print(None)
12+
try:
13+
obj.Print(None)
14+
except Exception:
15+
pass
1316
obj.GetDescription(lldb.SBStream())
1417
obj.DumpEmulationForAllInstructions("armv7")
1518
obj.Clear()

‎lldb/packages/Python/lldbsuite/test/python_api/disassemble-raw-data/TestDisassemble_VST1_64.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import print_function
66

7+
from io import StringIO
8+
import sys
79

810
import lldb
911
from lldbsuite.test.decorators import *
@@ -15,9 +17,6 @@ class Disassemble_VST1_64(TestBase):
1517

1618
mydir = TestBase.compute_mydir(__file__)
1719

18-
@skipTestIfFn(
19-
lambda: True,
20-
"llvm.org/pr24575: all tests get ERRORs in dotest.py after this")
2120
@add_test_categories(['pyapi'])
2221
@no_debug_info_test
2322
@skipIf(triple='^mips')
@@ -33,19 +32,38 @@ def test_disassemble_invalid_vst_1_64_raw_data(self):
3332
0x24, 0xf0, 0x0f, 0x04,
3433
0xa5, 0x46])
3534

35+
assembly = """
36+
push {r4, r5, r6, r7, lr}
37+
add r7, sp, #0xc
38+
push.w {r8, r10, r11}
39+
sub.w r4, sp, #0x40
40+
bic r4, r4, #0xf
41+
mov sp, r4
42+
"""
43+
def split(s):
44+
return [x.strip() for x in s.strip().splitlines()]
45+
3646
insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)
3747

48+
if sys.version_info.major >= 3:
49+
sio = StringIO()
50+
insts.Print(sio)
51+
self.assertEqual(split(assembly), split(sio.getvalue()))
52+
53+
self.assertEqual(insts.GetSize(), len(split(assembly)))
54+
55+
if sys.version_info.major >= 3:
56+
for i,asm in enumerate(split(assembly)):
57+
inst = insts.GetInstructionAtIndex(i)
58+
sio = StringIO()
59+
inst.Print(sio)
60+
self.assertEqual(asm, sio.getvalue().strip())
61+
3862
if self.TraceOn():
3963
print()
4064
for i in insts:
4165
print("Disassembled%s" % str(i))
4266

43-
# Remove the following return statement when the radar is fixed.
44-
return
45-
46-
# rdar://problem/11034702
47-
# VST1 (multiple single elements) encoding?
48-
# The disassembler should not crash!
4967
raw_bytes = bytearray([0x04, 0xf9, 0xed, 0x82])
5068

5169
insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)

‎lldb/scripts/interface/SBInstruction.i

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public:
5757
CanSetBreakpoint ();
5858

5959
void
60-
Print (FILE *out);
60+
Print (lldb::SBFile out);
61+
62+
void
63+
Print (lldb::FileSP BORROWED);
6164

6265
bool
6366
GetDescription (lldb::SBStream &description);

‎lldb/scripts/interface/SBInstructionList.i

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public:
5555
AppendInstruction (lldb::SBInstruction inst);
5656

5757
void
58-
Print (FILE *out);
58+
Print (lldb::SBFile out);
59+
60+
void
61+
Print (lldb::FileSP BORROWED);
5962

6063
bool
6164
GetDescription (lldb::SBStream &description);

‎lldb/source/API/SBInstruction.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "lldb/API/SBAddress.h"
1313
#include "lldb/API/SBFrame.h"
14+
#include "lldb/API/SBFile.h"
1415

1516
#include "lldb/API/SBInstruction.h"
1617
#include "lldb/API/SBStream.h"
@@ -255,10 +256,21 @@ bool SBInstruction::GetDescription(lldb::SBStream &s) {
255256
return false;
256257
}
257258

258-
void SBInstruction::Print(FILE *out) {
259-
LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), out);
259+
void SBInstruction::Print(FILE *outp) {
260+
LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), outp);
261+
FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
262+
Print(out);
263+
}
264+
265+
void SBInstruction::Print(SBFile out) {
266+
LLDB_RECORD_METHOD(void, SBInstruction, Print, (SBFile), out);
267+
Print(out.GetFile());
268+
}
269+
270+
void SBInstruction::Print(FileSP out_sp) {
271+
LLDB_RECORD_METHOD(void, SBInstruction, Print, (FileSP), out_sp);
260272

261-
if (out == nullptr)
273+
if (!out_sp || !out_sp->IsValid())
262274
return;
263275

264276
lldb::InstructionSP inst_sp(GetOpaque());
@@ -269,7 +281,7 @@ void SBInstruction::Print(FILE *out) {
269281
if (module_sp)
270282
module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
271283
sc);
272-
StreamFile out_stream(out, false);
284+
StreamFile out_stream(out_sp);
273285
FormatEntity::Entry format;
274286
FormatEntity::Parse("${addr}: ", format);
275287
inst_sp->Dump(&out_stream, 0, true, false, nullptr, &sc, nullptr, &format,
@@ -358,6 +370,8 @@ void RegisterMethods<SBInstruction>(Registry &R) {
358370
LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription,
359371
(lldb::SBStream &));
360372
LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FILE *));
373+
LLDB_REGISTER_METHOD(void, SBInstruction, Print, (SBFile));
374+
LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FileSP));
361375
LLDB_REGISTER_METHOD(bool, SBInstruction, EmulateWithFrame,
362376
(lldb::SBFrame &, uint32_t));
363377
LLDB_REGISTER_METHOD(bool, SBInstruction, DumpEmulation, (const char *));

‎lldb/source/API/SBInstructionList.cpp

+28-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include "lldb/API/SBAddress.h"
1212
#include "lldb/API/SBInstruction.h"
1313
#include "lldb/API/SBStream.h"
14+
#include "lldb/API/SBFile.h"
1415
#include "lldb/Core/Disassembler.h"
1516
#include "lldb/Core/Module.h"
17+
#include "lldb/Core/StreamFile.h"
1618
#include "lldb/Symbol/SymbolContext.h"
1719
#include "lldb/Utility/Stream.h"
1820

@@ -118,21 +120,41 @@ void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) {
118120

119121
void SBInstructionList::Print(FILE *out) {
120122
LLDB_RECORD_METHOD(void, SBInstructionList, Print, (FILE *), out);
121-
122123
if (out == nullptr)
123124
return;
125+
StreamFile stream(out, false);
126+
GetDescription(stream);
124127
}
125128

126-
bool SBInstructionList::GetDescription(lldb::SBStream &description) {
129+
void SBInstructionList::Print(SBFile out) {
130+
LLDB_RECORD_METHOD(void, SBInstructionList, Print, (SBFile), out);
131+
if (!out.IsValid())
132+
return;
133+
StreamFile stream(out.GetFile());
134+
GetDescription(stream);
135+
}
136+
137+
void SBInstructionList::Print(FileSP out_sp) {
138+
LLDB_RECORD_METHOD(void, SBInstructionList, Print, (FileSP), out_sp);
139+
if (!out_sp || !out_sp->IsValid())
140+
return;
141+
StreamFile stream(out_sp);
142+
GetDescription(stream);
143+
}
144+
145+
bool SBInstructionList::GetDescription(lldb::SBStream &stream) {
127146
LLDB_RECORD_METHOD(bool, SBInstructionList, GetDescription,
128-
(lldb::SBStream &), description);
147+
(lldb::SBStream &), stream);
148+
return GetDescription(stream.ref());
149+
}
150+
151+
bool SBInstructionList::GetDescription(Stream &sref) {
129152

130153
if (m_opaque_sp) {
131154
size_t num_instructions = GetSize();
132155
if (num_instructions) {
133156
// Call the ref() to make sure a stream is created if one deesn't exist
134157
// already inside description...
135-
Stream &sref = description.ref();
136158
const uint32_t max_opcode_byte_size =
137159
m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
138160
FormatEntity::Entry format;
@@ -200,6 +222,8 @@ void RegisterMethods<SBInstructionList>(Registry &R) {
200222
LLDB_REGISTER_METHOD(void, SBInstructionList, AppendInstruction,
201223
(lldb::SBInstruction));
202224
LLDB_REGISTER_METHOD(void, SBInstructionList, Print, (FILE *));
225+
LLDB_REGISTER_METHOD(void, SBInstructionList, Print, (SBFile));
226+
LLDB_REGISTER_METHOD(void, SBInstructionList, Print, (FileSP));
203227
LLDB_REGISTER_METHOD(bool, SBInstructionList, GetDescription,
204228
(lldb::SBStream &));
205229
LLDB_REGISTER_METHOD(bool, SBInstructionList,

0 commit comments

Comments
 (0)