Skip to content

[cmpcodesize][1] Extract otool subprocess calls #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions utils/cmpcodesize/cmpcodesize/compare.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import os
import subprocess
import collections
from operator import itemgetter

from cmpcodesize import otool

Prefixes = {
# Cpp
"__Z" : "CPP",
Expand Down Expand Up @@ -64,19 +65,10 @@ def addFunction(sizes, function, startAddr, endAddr, groupByPrefix):
sizes[function] += size


def flatten(*args):
for x in args:
if hasattr(x, '__iter__'):
for y in flatten(*x):
yield y
else:
yield x


def readSizes(sizes, fileName, functionDetails, groupByPrefix):
# Check if multiple architectures are supported by the object file.
# Prefer arm64 if available.
architectures = subprocess.check_output(["otool", "-V", "-f", fileName]).split("\n")
architectures = otool.fat_headers(fileName).split('\n')
arch = None
archPattern = re.compile('architecture ([\S]+)')
for architecture in architectures:
Expand All @@ -86,16 +78,14 @@ def readSizes(sizes, fileName, functionDetails, groupByPrefix):
arch = archMatch.group(1)
if "arm64" in arch:
arch = "arm64"
if arch is not None:
archParams = ["-arch", arch]
else:
archParams = []

if functionDetails:
content = subprocess.check_output(flatten(["otool", archParams, "-l", "-v", "-t", fileName])).split("\n")
content += subprocess.check_output(flatten(["otool", archParams, "-v", "-s", "__TEXT", "__textcoal_nt", fileName])).split("\n")
content = otool.load_commands(fileName,
architecture=arch,
include_text_sections=True).split('\n')
content += otool.text_sections(fileName, architecture=arch).split('\n')
else:
content = subprocess.check_output(flatten(["otool", archParams, "-l", fileName])).split("\n")
content = otool.load_commands(fileName, architecture=arch).split('\n')

sectName = None
currFunc = None
Expand Down
45 changes: 45 additions & 0 deletions utils/cmpcodesize/cmpcodesize/otool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import subprocess


def _command_for_architecture(architecture=None):
if architecture is None:
return ['otool']
else:
return ['otool', '-arch', architecture]


def fat_headers(path):
"""
Returns the headers for the executable at the given path.
Raises a subprocess.CalledProcessError if otool encounters an error,
such as not finding a file at the given path.
"""
return subprocess.check_output(['otool', '-V', '-f', path])


def load_commands(path, architecture=None, include_text_sections=False):
"""
Returns the load commands for the executable at the given path,
for the given architecture. If print_text_section is specified,
the disassembled text section of the load commands is also output.

Raises a subprocess.CalledProcessError if otool encounters an error,
such as not finding a file at the given path.
"""
command = _command_for_architecture(architecture) + ['-l']
if include_text_sections:
command += ['-v', '-t']
return subprocess.check_output(command + [path])


def text_sections(path, architecture=None):
"""
Returns the contents of the text sections of the executable at the
given path, for the given architecture.

Raises a subprocess.CalledProcessError if otool encounters an error,
such as not finding a file at the given path.
"""
return subprocess.check_output(
_command_for_architecture(architecture) +
['-v', '-s', '__TEXT', '__textcoal_nt', path])
73 changes: 73 additions & 0 deletions utils/cmpcodesize/tests/test_otool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import subprocess
import unittest

from cmpcodesize import otool


# Store parameters passed to subprocess.check_output into
# this global variable.
_subprocess_check_output_arguments = []


# We'll monkey-patch subprocess.check_output with this stub
# function, which simply records whatever's passed to
# check_output into the global variables above.
def _stub_subprocess_check_output(arguments, *args, **kwargs):
global _subprocess_check_output_arguments
_subprocess_check_output_arguments = arguments


class OtoolTestCase(unittest.TestCase):
def setUp(self):
# Monkey-patch subprocess.check_output with our stub function.
self._original_check_output = subprocess.check_output
subprocess.check_output = _stub_subprocess_check_output

def tearDown(self):
# Undo the monkey-patching.
subprocess.check_output = self._original_check_output

def test_fat_headers(self):
otool.fat_headers('/path/to/foo')
self.assertEqual(_subprocess_check_output_arguments,
['otool', '-V', '-f', '/path/to/foo'])

def test_load_commands_with_no_architecture(self):
otool.load_commands('/path/to/bar')
self.assertEqual(_subprocess_check_output_arguments,
['otool', '-l', '/path/to/bar'])

def test_load_commands_with_architecture(self):
otool.load_commands('/path/to/baz', architecture='arch-foo')
self.assertEqual(
_subprocess_check_output_arguments,
['otool', '-arch', 'arch-foo', '-l', '/path/to/baz'])

def test_load_commands_no_architecture_but_including_text_sections(self):
otool.load_commands(
'/path/to/flim', include_text_sections=True)
self.assertEqual(
_subprocess_check_output_arguments,
['otool', '-l', '-v', '-t', '/path/to/flim'])

def test_load_commands_with_architecture_and_including_text_sections(self):
otool.load_commands(
'/path/to/flam',
architecture='arch-bar',
include_text_sections=True)
self.assertEqual(
_subprocess_check_output_arguments,
['otool', '-arch', 'arch-bar', '-l', '-v', '-t', '/path/to/flam'])

def test_text_sections_no_architecture(self):
otool.text_sections('/path/to/fish')
self.assertEqual(
_subprocess_check_output_arguments,
['otool', '-v', '-s', '__TEXT', '__textcoal_nt', '/path/to/fish'])

def test_text_sections_with_architecture(self):
otool.text_sections('/path/to/frosh', architecture='arch-baz')
self.assertEqual(
_subprocess_check_output_arguments,
['otool', '-arch', 'arch-baz', '-v', '-s',
'__TEXT', '__textcoal_nt', '/path/to/frosh'])