Skip to content

Commit 1c6ca35

Browse files
committed
[lldb] Suppress persistent result when running po
Remove the persistent result variable after executing `po`. Without this change, the following behavior happens: ``` (lldb) p thing (NSObject *) $0 = 0x600000008000 (lldb) po thing <NSObject: 0x600000008000> (lldb) p thing (NSObject *) $2 = 0x600000008000 (lldb) p $1 (NSObject *) $1 = 0x600000008000 ``` Even though `po` hides the persistent result variable, it's still created - as $1 in this example. It can be accessed even though its existence is not evident. With this change, the persistent result is removed after the object description has printed. Instead, this is the behavior: ``` (lldb) p thing (NSObject *) $0 = 0x600000008000 (lldb) po thing <NSObject: 0x600000008000> (lldb) p thing (NSObject *) $1 = 0x600000008000 ``` The difference here is that the `po` doens't silently create a persistent result. Differential Revision: https://reviews.llvm.org/D144044 (cherry picked from commit 3328ee5)
1 parent 54fd14d commit 1c6ca35

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Target/Process.h"
2222
#include "lldb/Target/StackFrame.h"
2323
#include "lldb/Target/Target.h"
24+
#include "lldb/lldb-private-enumerations.h"
2425

2526
// BEGIN SWIFT
2627
#include "lldb/Symbol/CompileUnit.h"
@@ -361,6 +362,9 @@ EvaluateExpressionOptions
361362
CommandObjectExpression::GetEvalOptions(const Target &target) {
362363
EvaluateExpressionOptions options;
363364
options.SetCoerceToId(m_varobj_options.use_objc);
365+
if (m_command_options.m_verbosity ==
366+
eLanguageRuntimeDescriptionDisplayVerbosityCompact)
367+
options.SetSuppressPersistentResult(m_varobj_options.use_objc);
364368
options.SetUnwindOnError(m_command_options.unwind_on_error);
365369
options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints);
366370
options.SetKeepInMemory(true);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OBJC_SOURCES := main.m
2+
3+
include Makefile.rules
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Test behavior of `po` and persistent results.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test import lldbutil
9+
10+
11+
class TestCase(TestBase):
12+
def setUp(self):
13+
TestBase.setUp(self)
14+
self.build()
15+
lldbutil.run_to_source_breakpoint(self, "break here", lldb.SBFileSpec("main.m"))
16+
17+
@skipUnlessDarwin
18+
def test_po_does_not_print_persistent_result(self):
19+
"""Test `po` doesn't advertise a persistent result variable."""
20+
self.expect("po obj", matching=False, substrs=["$0 = "])
21+
22+
@skipUnlessDarwin
23+
def test_po_does_not_keep_persistent_result(self):
24+
"""Test `po` doesn't leak a persistent result variable."""
25+
self.expect("po obj")
26+
# Verify `po` used a temporary persistent result. In other words, there
27+
# should be no $0 at this point.
28+
self.expect("expression $0", error=True)
29+
self.expect("expression obj", substrs=["$0 = "])
30+
31+
@skipUnlessDarwin
32+
def test_expression_description_verbosity(self):
33+
"""Test printing object description _and_ opt-in to persistent results."""
34+
self.expect("expression -O -vfull -- obj", substrs=["$0 = "])
35+
self.expect("expression $0", substrs=["$0 = "])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import <objc/NSObject.h>
2+
3+
int main() {
4+
NSObject *obj = [NSObject new];
5+
return 0; // break here
6+
}

0 commit comments

Comments
 (0)