[pypy-commit] pypy keep-debug-symbols: add a smartstrip tool, which can optionally keep the debug symbols in a separate file, instead of just stripping them away
antocuni
pypy.commits at gmail.com
Tue Oct 31 20:29:37 EDT 2017
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: keep-debug-symbols
Changeset: r92892:005844269621
Date: 2017-11-01 01:19 +0100
http://bitbucket.org/pypy/pypy/changeset/005844269621/
Log: add a smartstrip tool, which can optionally keep the debug symbols
in a separate file, instead of just stripping them away
diff --git a/pypy/tool/release/smartstrip.py b/pypy/tool/release/smartstrip.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/release/smartstrip.py
@@ -0,0 +1,32 @@
+"""
+Strip symbols from an executable, but keep them in a .debug file
+"""
+
+import sys
+import os
+import py
+
+def _strip(exe):
+ if sys.platform == 'win32':
+ pass
+ elif sys.platform == 'darwin':
+ # 'strip' fun: see issue #587 for why -x
+ os.system("strip -x " + str(exe)) # ignore errors
+ else:
+ os.system("strip " + str(exe)) # ignore errors
+
+def _extract_debug_symbols(exe, debug):
+ if sys.platform == 'linux2':
+ os.system("objcopy --only-keep-debug %s %s" % (exe, debug))
+ os.system("objcopy --add-gnu-debuglink=%s %s" % (debug, exe))
+
+def smartstrip(exe, keep_debug=True):
+ exe = py.path.local(exe)
+ debug = py.path.local(str(exe) + '.debug')
+ if keep_debug:
+ _extract_debug_symbols(exe, debug)
+ _strip(exe)
+
+
+if __name__ == '__main__':
+ smartstrip(sys.argv[1])
diff --git a/pypy/tool/release/test/test_smartstrip.py b/pypy/tool/release/test/test_smartstrip.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/release/test/test_smartstrip.py
@@ -0,0 +1,50 @@
+import pytest
+import sys
+import os
+from commands import getoutput
+from pypy.tool.release.smartstrip import smartstrip
+
+ at pytest.fixture
+def exe(tmpdir):
+ src = tmpdir.join("myprog.c")
+ src.write("""
+ int foo(int a, int b) {
+ return a+b;
+ }
+ int main(void) { }
+ """)
+ exe = tmpdir.join("myprog")
+ ret = os.system("gcc -o %s %s" % (exe, src))
+ assert ret == 0
+ return exe
+
+def info_symbol(exe, symbol):
+ out = getoutput("gdb %s -ex 'info symbol %s' -ex 'quit'" % (exe, symbol))
+ lines = out.splitlines()
+ return lines[-1]
+
+ at pytest.mark.skipif(sys.platform == 'win32',
+ reason='strip not supported on windows')
+class TestSmarStrip(object):
+
+ def test_info_symbol(self, exe):
+ info = info_symbol(exe, "foo")
+ assert info == "foo in section .text"
+
+ def test_strip(self, exe):
+ smartstrip(exe, keep_debug=False)
+ info = info_symbol(exe, "foo")
+ assert info.startswith("No symbol table is loaded")
+
+ @pytest.mark.skipif(sys.platform != 'linux2',
+ reason='keep_debug not supported')
+ def test_keep_debug(self, exe, tmpdir):
+ smartstrip(exe, keep_debug=True)
+ debug = tmpdir.join("myprog.debug")
+ assert debug.check(file=True)
+ info = info_symbol(exe, "foo")
+ assert info == "foo in section .text of %s" % exe
+ #
+ debug.remove()
+ info = info_symbol(exe, "foo")
+ assert info.startswith("No symbol table is loaded")
More information about the pypy-commit
mailing list