3

How do I call the user_cmts_* functions from the IDAPython API? I am quite new to the SDK and IDAPython, so I'm kind of lost as to what I'm supposed to pass to these functions, as it's not the user-friendliest docs imo. I tried passing a map as such for user_cmts_begin:

import idaapi

def print_cmt(cmt):
   print cmt

cumap = map(print_cmt, [some address here to test])

idaapi.user_cmts_begin(cumap)

But it throws a type error, so obviously I must be doing something wrong...

For now I've had to resort to doing this:

import idaapi
import re

addr = 0x80000000

while addr < 0x80200000:
    cmt = idaapi.get_cmt(addr, 0) # get non-repeatable comment at current address/line

    if cmt is not None: # skip if the line has no comment
        if re.search("insert regex for non-auto-generated comments here", cmt) is not None:
            print "%08X: %s" % (addr, cmt)

    addr = idaapi.next_not_tail(addr)

The IDAPython docs can be found here:

https://www.hex-rays.com/products/ida/support/idapython_docs/

Could someone show an example?

6

You might want to use the IDAPython wrapper functions. As you mentioned, the IDA API is quite poorly documented. One of the best, but not easiest way to actually understand it is to check the IDAPython wrapper library in your IDA/Python/idc.py folder.

Based on what I've seen in the idc.py file, there's some functions you might want to check out to help you out.

Creates a comment to the current line:

MakeComm(ScreenEA(), "Comment Test")

Creates a repetable comment to the current line:

MakeRptCmt(ScreenEA(), "Repeatable Comment")

Retrieves the comment from the current line:

Specify 1 to get the repeatable comment or 0 to get the normal comment as the second argument. Note that the function CommentEx is a wrapper around GetCommentEx.

c = GetCommentEx(ScreenEA(), 1)
print(c)

Or even easier is to use Comment(ea) or RptCmt(ea), which are wrappers onGetCommentEx():

def Comment(ea):                return GetCommentEx(ea, 0)
"""Returns the regular comment or None"""

def RptCmt(ea):                 return GetCommentEx(ea, 1)
"""Returns the repeatable comment or None"""

And then you have all the special functions to retrieve comments from specific structures such as Enums, Functions, Constants...I don't believe I have seen a specific function to retrieve all comments, but it's fairly easy to build, as you did:

Example:

I've made this code that looks for lines containing a 'jump: 0x???' comment/repeatable comments. Not the best code (slow), but it illustrates the use of the IDA Python comments functions.

def get_comments(_startea, _endea, _filter):
    matches = []
    ea = _startea
    for ea in range(_startea, _endea):
        cmt1 = Comment(ea)
        cmt2 = RptCmt(ea)
        cmt = ""
        if cmt1:
            cmt += cmt1
        if cmt2:
            cmt += cmt2
        if (cmt):
            re_match = re.match(_filter, cmt, re.I)
            if (re_match and len(re_match.groups()) > 0):
                matches.append(re_match.group(3))
    return matches

MakeComm(ScreenEA(), 'jump: 0xBADC0DE')
filter = r'(.*)(jump: 0x)([0-9a-fA-F]+)(.*)'
addrs = get_comments(MinEA(), MaxEA(), filter)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.