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

View File

@ -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)

View File

@ -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 ''

View File

@ -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):

View File

@ -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)

View File

@ -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) != '!<arch>\n':
with open(path) as fh:
data = StringIO(fh.read())
if data.read(8) != '!<arch>\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

View File

@ -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