tags:

views:

194

answers:

3

Hello,

I have a txt file like :

test.txt

Symbols from __ctype_tab.o:

Name                  Value   Class        Type         Size     Line  Section

__ctype             |00000000|   D  |            OBJECT|00000004|     |.data
__ctype_tab         |00000000|   r  |            OBJECT|00000101|     |.rodata


Symbols from _ashldi3.o:

Name                  Value   Class        Type         Size     Line  Section

__ashldi3           |00000000|   T  |              FUNC|00000050|     |.text


Symbols from _ashrdi3.o:

Name                  Value   Class        Type         Size     Line  Section

__ashrdi3           |00000000|   T  |              FUNC|00000058|     |.text


Symbols from _fixdfdi.o:

Name                  Value   Class        Type         Size     Line  Section

__fixdfdi           |00000000|   T  |              FUNC|0000004c|     |.text
__fixunsdfdi        |        |   U  |            NOTYPE|        |     |*UND*


Symbols from _fixsfdi.o:

Name                  Value   Class        Type         Size     Line  Section

__fixsfdi           |00000000|   T  |              FUNC|0000004c|     |.text
__fixunssfdi        |        |   U  |            NOTYPE|        |     |*UND*


Symbols from _fixunssfdi.o:

Name                  Value   Class        Type         Size     Line  Section

__cmpdi2            |        |   U  |            NOTYPE|        |     |*UND*
__fixunssfdi        |00000000|   T  |              FUNC|00000228|     |.text
__floatdidf         |        |   U  |            NOTYPE|        |     |*UND*

What i want to do is , i will be given a function whose type is NOTYPE . i need to search the txt and find under which .o it's defined(ie;with type FUNC).When i get the .o file,i may see other functions as NOTYPE. Then i have to search where those are defined.it goes on. Finally i want to return a list of names of all .o files which contained the functions.

My piece of code:

notypeDict , funcDict  = {} , {}
notypeList , funcList = [] , []
currObj , prevObj = '' , ''

fp = open(r'C:\test.txt','r') # file path cms here
fileList = fp.readlines()

for line in fileList:
    if '.o' in line:    # line containg .o
        currObj=line.split()[-1][0:-1]        
    if '|' not in line: # line containg |
        pass
    else:   # other lines
        dataList=[dataItem.strip()  for dataItem in line.strip().split('|')]    # a list of each word in line
        name=dataList[0].strip()    # name of the function
        notypeDict[prevObj] = notypeList    #   notypeDict is a dictionary which contains .o as key and a list of NOTYPE function name 
        funcDict[prevObj] = funcList    # funcDict is a dictionary which contains .o as key and a list of FUNC function names

        if prevObj == currObj :
            pass
        if prevObj != currObj :       
            notypeList , funcList = [] , []

        if dataList[3] == 'NOTYPE' :                
            notypeList.append(name)
        if dataList[3] == 'FUNC' :
            funcList.append(name)

        prevObj = currObj    

print 'notypeDict' , notypeDict
print '\n\nfuncDict' , funcDict

Here i will get two dictionaries, notypeDict and funcDict.

notypeDict has .o as key and a list of NOTYPE functions as value funcDict has .o as key and a list of FUNC functions as value.

I reached till here.

But don't getting ideas how to proceed to achieve my target.

I think my question is clear.

Please help me out.

+1  A: 

I would use regular expressions with capture groups for the different kinds of interesting lines in your file; I'd go through the file line by line, and as I found an interesting line (i.e. matched the regex), I'd process the captured data from the regex appropriately.

After having built up dictionaries etc., answering questions based on the data is easy.

Barry Kelly
+1  A: 

What do you think the following does?

   if '.o' in line:    # line containg .o
        currObj=line.split()[-1][0:-1]        
   if '|' not in line: # line containg |
        pass
   else:   # other lines

Does it find lines with '.o' or '|' or other?

No. Actually, it doesn't.

It finds lines that contain '.o'. And does something with them.

Then it checks that line again for '|' or "other". All of your '.o' lines are processed two times.

Once as a '.o', then again as a "not |".

You might mean elif instead of if.


This code

    if prevObj == currObj :
        pass
    if prevObj != currObj :       
        notypeList , funcList = [] , []

Is rather more complex than it needs to be. Doesn't cause a problem, per se, but it is silly-looking.


This code

    if dataList[3] == 'NOTYPE' :                
        notypeList.append(name)
    if dataList[3] == 'FUNC' :
        funcList.append(name)

is probably good. However, it looks bad because the conditions are exclusive and would look better as elif.

S.Lott
I agree Sir about using elif
+1  A: 

What about this code? It is based on your two dictionaries. Just call find_dep_for_func(notype_funcname).

def find_ofile(funcname):
    """This will find .o file for given function."""
    for ofile, fns in funcDict.iteritems():
        if funcname in fns:
            return ofile                
    raise Exception("Cannot find function "+funcname)

def find_dependencies(ofile, deps = None):
    """This will find dependent .o files for given .o file."""
    olist = deps if deps else set([])
    for fn in notypeDict[ofile]:
        ofile = find_ofile(fn)
        if not ofile in olist:
            olist.add(ofile)
            olist = find_dependencies(ofile, olist)
    return olist

def find_dep_for_func(notype_funcname):
    return find_dependencies(find_ofile(funcname))
Jiri