Remove parameters from symbolized stack traces.
The expanded parameters take a lot horizontal space
and almost always push the file:line of the screen,
or text-wrap generating multiple lines per frame.
This CL tries to make the output less cluttered by
removing parameters from the unmangled method names.
It is possible to add the parameters back using
the --verbose command line argument.
Test: Add unit tests, investigate crashes from logcat
Change-Id: I42d1e26dbc2fa9db8b7bd95ce449cb2bd93f93f8
diff --git a/scripts/symbol.py b/scripts/symbol.py
index 5ec4e48..0a255e8 100755
--- a/scripts/symbol.py
+++ b/scripts/symbol.py
@@ -60,6 +60,7 @@
ARCH = None
+VERBOSE = False
# These are private. Do not access them from other modules.
_CACHED_TOOLCHAIN = None
@@ -484,6 +485,37 @@
return symbol
return "%s+%d" % (symbol, offset)
+def FormatSymbolWithoutParameters(symbol):
+ """Remove parameters from function.
+
+ Rather than trying to parse the demangled C++ signature,
+ it just removes matching top level parenthesis.
+ """
+ if not symbol:
+ return symbol
+
+ result = symbol
+ result = result.replace(") const", ")") # Strip const keyword.
+ result = result.replace("operator<<", "operator\u00AB") # Avoid unmatched '<'.
+ result = result.replace("operator>>", "operator\u00BB") # Avoid unmatched '>'.
+ result = result.replace("operator->", "operator\u2192") # Avoid unmatched '>'.
+
+ nested = [] # Keeps tract of current nesting level of parenthesis.
+ for i in reversed(range(len(result))): # Iterate backward to make cutting easier.
+ c = result[i]
+ if c == ')' or c == '>':
+ if len(nested) == 0:
+ end = i + 1 # Mark the end of top-level pair.
+ nested.append(c)
+ if c == '(' or c == '<':
+ if len(nested) == 0 or {')':'(', '>':'<'}[nested.pop()] != c:
+ return symbol # Malformed: character does not match its pair.
+ if len(nested) == 0 and c == '(' and (end - i) > 2:
+ result = result[:i] + result[end:] # Remove substring (i, end).
+ if len(nested) > 0:
+ return symbol # Malformed: missing pair.
+
+ return result.strip()
def GetAbiFromToolchain(toolchain_var, bits):
toolchain = os.environ.get(toolchain_var)
@@ -748,5 +780,32 @@
"Could not determine arch from input, use --arch=XXX to specify it",
SetAbi, [])
+class FormatSymbolWithoutParametersTests(unittest.TestCase):
+ def test_c(self):
+ self.assertEqual(FormatSymbolWithoutParameters("foo"), "foo")
+ self.assertEqual(FormatSymbolWithoutParameters("foo+42"), "foo+42")
+
+ def test_simple(self):
+ self.assertEqual(FormatSymbolWithoutParameters("foo(int i)"), "foo")
+ self.assertEqual(FormatSymbolWithoutParameters("foo(int i)+42"), "foo+42")
+ self.assertEqual(FormatSymbolWithoutParameters("bar::foo(int i)+42"), "bar::foo+42")
+ self.assertEqual(FormatSymbolWithoutParameters("operator()"), "operator()")
+
+ def test_templates(self):
+ self.assertEqual(FormatSymbolWithoutParameters("bar::foo<T>(vector<T>& v)"), "bar::foo<T>")
+ self.assertEqual(FormatSymbolWithoutParameters("bar<T>::foo(vector<T>& v)"), "bar<T>::foo")
+ self.assertEqual(FormatSymbolWithoutParameters("bar::foo<T>(vector<T<U>>& v)"), "bar::foo<T>")
+ self.assertEqual(FormatSymbolWithoutParameters("bar::foo<(EnumType)0>(vector<(EnumType)0>& v)"),
+ "bar::foo<(EnumType)0>")
+
+ def test_nested(self):
+ self.assertEqual(FormatSymbolWithoutParameters("foo(int i)::bar(int j)"), "foo::bar")
+
+ def test_unballanced(self):
+ self.assertEqual(FormatSymbolWithoutParameters("foo(bar(int i)"), "foo(bar(int i)")
+ self.assertEqual(FormatSymbolWithoutParameters("foo)bar(int i)"), "foo)bar(int i)")
+ self.assertEqual(FormatSymbolWithoutParameters("foo<bar(int i)"), "foo<bar(int i)")
+ self.assertEqual(FormatSymbolWithoutParameters("foo>bar(int i)"), "foo>bar(int i)")
+
if __name__ == '__main__':
unittest.main(verbosity=2)