From 1263e5ff7cfd6ed1fd73b096e7e162f82d4d38b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Bac=C5=82awski?= Date: Sat, 20 Jul 2013 13:27:34 +0200 Subject: [PATCH] Add individual loggers for each module. --- tools/dumpaout.py | 5 +++++ tools/dumphunks.py | 4 ++++ tools/lsar.py | 4 ++++ tools/objtools/aout.py | 23 ++++++++++++++++------- tools/objtools/ar.py | 38 +++++++++++++++++++++++++++----------- tools/objtools/hunk.py | 7 +++++-- 6 files changed, 61 insertions(+), 20 deletions(-) diff --git a/tools/dumpaout.py b/tools/dumpaout.py index 1cbc677..342d2b1 100755 --- a/tools/dumpaout.py +++ b/tools/dumpaout.py @@ -1,9 +1,14 @@ #!/usr/bin/env python2.7 -B +import logging import sys + from objtools import aout + if __name__ == '__main__': + logging.basicConfig() + for path in sys.argv[1:]: obj = aout.Aout() obj.read(path) diff --git a/tools/dumphunks.py b/tools/dumphunks.py index 9350d4e..a6b4caa 100755 --- a/tools/dumphunks.py +++ b/tools/dumphunks.py @@ -1,10 +1,14 @@ #!/usr/bin/env python2.7 -B +import logging import sys + from objtools import hunk if __name__ == '__main__': + logging.basicConfig() + for path in sys.argv[1:]: print 'Parsing "%s".' % path print '' diff --git a/tools/lsar.py b/tools/lsar.py index 1eab4ad..c1b4d54 100755 --- a/tools/lsar.py +++ b/tools/lsar.py @@ -1,10 +1,14 @@ #!/usr/bin/env python2.7 -B +import logging import sys + from objtools import ar if __name__ == '__main__': + logging.basicConfig() + for path in sys.argv[1:]: print '%s:' % path for num, entry in enumerate(ar.ReadFile(path), start=1): diff --git a/tools/objtools/aout.py b/tools/objtools/aout.py index 6ddefa0..c466a3f 100644 --- a/tools/objtools/aout.py +++ b/tools/objtools/aout.py @@ -1,10 +1,16 @@ -from collections import namedtuple, Sequence import logging import os import struct + +from cStringIO import StringIO +from collections import namedtuple, Sequence + from util import hexdump +log = logging.getLogger(__name__) + + class Header(namedtuple('Header', ('mid', 'magic', 'text', 'data', 'bss', 'syms', 'entry', 'trsize', 'drsize'))): magic_map = {'OMAGIC': 0407, 'NMAGIC': 0410, 'ZMAGIC': 0413} @@ -12,7 +18,9 @@ class Header(namedtuple('Header', ('mid', 'magic', 'text', 'data', 'bss', 'HP300': 300, 'HPUX': 0x20C, 'HPUX800': 0x20B} @classmethod - def decode(cls, data): + def decode(cls, fh): + data = fh.read(32) + if len(data) != 32: raise ValueError('Not a valid a.out header!') @@ -172,11 +180,12 @@ class Aout(object): def read(self, path): self._path = path - with open(path) as data: - logging.debug('Reading %r of size %d bytes.', - path, os.stat(path).st_size) + with open(path) as fh: + log.debug('Reading %r of size %d bytes.', path, os.path.getsize(path)) - self._header = Header.decode(data.read(32)) + data = StringIO(fh.read()) + + self._header = Header.decode(data) self._text = data.read(self._header.text) self._data = data.read(self._header.data) @@ -187,7 +196,7 @@ class Aout(object): strings = data.read() if str_size != len(strings): - logging.warn('Wrong size of string table!') + log.warn('Wrong size of string table!') self._strings = StringTable.decode(strings) diff --git a/tools/objtools/ar.py b/tools/objtools/ar.py index b3b35f6..850b6a4 100644 --- a/tools/objtools/ar.py +++ b/tools/objtools/ar.py @@ -2,15 +2,27 @@ import logging import os import struct +from cStringIO import StringIO from collections import namedtuple +log = logging.getLogger(__name__) + + class ArEntry(namedtuple('ArEntry', 'name modtime owner group mode data')): @classmethod - def decode(cls, ar): + def decode(cls, fh): + data = fh.read(60) + + if len(data) != 60: + raise ValueError('Not a valid ar archive header!') + name, modtime, owner, group, mode, length, magic = \ - struct.unpack('16s12s6s6s8s10s2s', ar.read(60)) + struct.unpack('16s12s6s6s8s10s2s', data) + + if magic != '`\n': + raise ValueError('Not a valid ar archive header!') length = int(length.strip()) modtime = int(modtime.strip() or '0') @@ -20,16 +32,18 @@ class ArEntry(namedtuple('ArEntry', if name.startswith('#1/'): name_length = int(name[3:]) - name = ar.read(name_length).strip('\0') + name = fh.read(name_length).strip('\0') else: name_length = 0 name = name.strip() - data = ar.read(length - name_length) + data = fh.read(length - name_length) + + log.debug('entry: file %r, size %d,', name, len(data)) # next block starts at even boundary if length & 1: - ar.seek(1, os.SEEK_CUR) + fh.seek(1, os.SEEK_CUR) return cls(name, modtime, owner, group, mode, data) @@ -37,19 +51,21 @@ class ArEntry(namedtuple('ArEntry', def ReadFile(path): entries = [] - with open(path) as ar: - if ar.read(8) != '!\n': + with open(path) as fh: + data = StringIO(fh.read()) + + if data.read(8) != '!\n': raise ValueError('%s is not an ar archive' % path) - ar_size = os.stat(path).st_size + size = os.path.getsize(path) - logging.debug('Reading ar archive %r of size %d bytes.', path, ar_size) + log.debug('Reading ar archive %r of size %d bytes.', path, size) - while ar.tell() < ar_size: + while data.tell() < size: # Some archives have version information attached at the end of file, # that confuses ArEntry parser, so just skip it. try: - entries.append(ArEntry.decode(ar)) + entries.append(ArEntry.decode(data)) except struct.error: break diff --git a/tools/objtools/hunk.py b/tools/objtools/hunk.py index b6a74c4..e3f8377 100644 --- a/tools/objtools/hunk.py +++ b/tools/objtools/hunk.py @@ -10,6 +10,9 @@ import util from aout import StringTable, SymbolInfo +log = logging.getLogger(__name__) + + HunkMap = { 'HUNK_UNIT': 999, 'HUNK_NAME': 1000, @@ -475,7 +478,7 @@ class HunkFile(file): def __init__(self, *args, **kwargs): file.__init__(self, *args, **kwargs) - self.size = os.stat(self.name).st_size + self.size = os.path.getsize(self.name) self.type = 'object' @contextmanager @@ -624,7 +627,7 @@ def ReadFile(path): try: hunks.append(hunk.parse(hf)) except ValueError: - logging.error('Parse error at position 0x%x.', hf.tell()) + log.error('Parse error at position 0x%x.', hf.tell()) util.hexdump(hf.read()) return hunks