Add individual loggers for each module.

This commit is contained in:
Krystian Bacławski
2013-07-20 13:27:34 +02:00
parent 9585396803
commit 1263e5ff7c
6 changed files with 61 additions and 20 deletions
+5
View File
@@ -1,9 +1,14 @@
#!/usr/bin/env python2.7 -B #!/usr/bin/env python2.7 -B
import logging
import sys import sys
from objtools import aout from objtools import aout
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig()
for path in sys.argv[1:]: for path in sys.argv[1:]:
obj = aout.Aout() obj = aout.Aout()
obj.read(path) obj.read(path)
+4
View File
@@ -1,10 +1,14 @@
#!/usr/bin/env python2.7 -B #!/usr/bin/env python2.7 -B
import logging
import sys import sys
from objtools import hunk from objtools import hunk
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig()
for path in sys.argv[1:]: for path in sys.argv[1:]:
print 'Parsing "%s".' % path print 'Parsing "%s".' % path
print '' print ''
+4
View File
@@ -1,10 +1,14 @@
#!/usr/bin/env python2.7 -B #!/usr/bin/env python2.7 -B
import logging
import sys import sys
from objtools import ar from objtools import ar
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig()
for path in sys.argv[1:]: for path in sys.argv[1:]:
print '%s:' % path print '%s:' % path
for num, entry in enumerate(ar.ReadFile(path), start=1): for num, entry in enumerate(ar.ReadFile(path), start=1):
+16 -7
View File
@@ -1,10 +1,16 @@
from collections import namedtuple, Sequence
import logging import logging
import os import os
import struct import struct
from cStringIO import StringIO
from collections import namedtuple, Sequence
from util import hexdump from util import hexdump
log = logging.getLogger(__name__)
class Header(namedtuple('Header', ('mid', 'magic', 'text', 'data', 'bss', class Header(namedtuple('Header', ('mid', 'magic', 'text', 'data', 'bss',
'syms', 'entry', 'trsize', 'drsize'))): 'syms', 'entry', 'trsize', 'drsize'))):
magic_map = {'OMAGIC': 0407, 'NMAGIC': 0410, 'ZMAGIC': 0413} 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} 'HP300': 300, 'HPUX': 0x20C, 'HPUX800': 0x20B}
@classmethod @classmethod
def decode(cls, data): def decode(cls, fh):
data = fh.read(32)
if len(data) != 32: if len(data) != 32:
raise ValueError('Not a valid a.out header!') raise ValueError('Not a valid a.out header!')
@@ -172,11 +180,12 @@ class Aout(object):
def read(self, path): def read(self, path):
self._path = path self._path = path
with open(path) as data: with open(path) as fh:
logging.debug('Reading %r of size %d bytes.', log.debug('Reading %r of size %d bytes.', path, os.path.getsize(path))
path, os.stat(path).st_size)
self._header = Header.decode(data.read(32)) data = StringIO(fh.read())
self._header = Header.decode(data)
self._text = data.read(self._header.text) self._text = data.read(self._header.text)
self._data = data.read(self._header.data) self._data = data.read(self._header.data)
@@ -187,7 +196,7 @@ class Aout(object):
strings = data.read() strings = data.read()
if str_size != len(strings): if str_size != len(strings):
logging.warn('Wrong size of string table!') log.warn('Wrong size of string table!')
self._strings = StringTable.decode(strings) self._strings = StringTable.decode(strings)
+27 -11
View File
@@ -2,15 +2,27 @@ import logging
import os import os
import struct import struct
from cStringIO import StringIO
from collections import namedtuple from collections import namedtuple
log = logging.getLogger(__name__)
class ArEntry(namedtuple('ArEntry', class ArEntry(namedtuple('ArEntry',
'name modtime owner group mode data')): 'name modtime owner group mode data')):
@classmethod @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 = \ 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()) length = int(length.strip())
modtime = int(modtime.strip() or '0') modtime = int(modtime.strip() or '0')
@@ -20,16 +32,18 @@ class ArEntry(namedtuple('ArEntry',
if name.startswith('#1/'): if name.startswith('#1/'):
name_length = int(name[3:]) name_length = int(name[3:])
name = ar.read(name_length).strip('\0') name = fh.read(name_length).strip('\0')
else: else:
name_length = 0 name_length = 0
name = name.strip() 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 # next block starts at even boundary
if length & 1: if length & 1:
ar.seek(1, os.SEEK_CUR) fh.seek(1, os.SEEK_CUR)
return cls(name, modtime, owner, group, mode, data) return cls(name, modtime, owner, group, mode, data)
@@ -37,19 +51,21 @@ class ArEntry(namedtuple('ArEntry',
def ReadFile(path): def ReadFile(path):
entries = [] entries = []
with open(path) as ar: with open(path) as fh:
if ar.read(8) != '!<arch>\n': data = StringIO(fh.read())
if data.read(8) != '!<arch>\n':
raise ValueError('%s is not an ar archive' % path) 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, # Some archives have version information attached at the end of file,
# that confuses ArEntry parser, so just skip it. # that confuses ArEntry parser, so just skip it.
try: try:
entries.append(ArEntry.decode(ar)) entries.append(ArEntry.decode(data))
except struct.error: except struct.error:
break break
+5 -2
View File
@@ -10,6 +10,9 @@ import util
from aout import StringTable, SymbolInfo from aout import StringTable, SymbolInfo
log = logging.getLogger(__name__)
HunkMap = { HunkMap = {
'HUNK_UNIT': 999, 'HUNK_UNIT': 999,
'HUNK_NAME': 1000, 'HUNK_NAME': 1000,
@@ -475,7 +478,7 @@ class HunkFile(file):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
file.__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' self.type = 'object'
@contextmanager @contextmanager
@@ -624,7 +627,7 @@ def ReadFile(path):
try: try:
hunks.append(hunk.parse(hf)) hunks.append(hunk.parse(hf))
except ValueError: 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()) util.hexdump(hf.read())
return hunks return hunks