Vector35 / binaryninja-api

Public API, examples, documentation and issues for Binary Ninja
https://binary.ninja/
MIT License
908 stars 207 forks source link

Native UI view to list all comments (similar to tags, etc) #5488

Open 0xdevalias opened 4 months ago

0xdevalias commented 4 months ago

What is the feature you'd like to have?

It would be cool if there was a native UI view that lets us see all comments that a user has added to the decompilation; similar to how we can currently look at tags, etc.

Is your feature request related to a problem?

I know I have added comments to various functions in my decompilation that related to division optimisations that weren't de-optimised (eg. https://github.com/Vector35/binaryninja-api/issues/2043#issuecomment-1569287264 ), but I can't remember where they were to go look them up again now.

Are any alternative solutions acceptable?

Being able to search for comments might be a partial solution, assuming I remembered something about the comment to search for, but that won't always be the case:

Additional Information:

From @CouleeApps on Slack:

there is not currently an "all comments" list like there is for tags

it would not be hard to script given there are accessors for comments (a bit unwieldy though)

we've wanted to merge comments into tags since... basically as long as tags have existed

turns out comments are harder than expected

0xdevalias commented 4 months ago

ChatGPT suggested this (unverified), which if I had access to the API might work as a workaround in the meantime:

from binaryninja import BinaryView, Function

# Load the current binary view
bv = BinaryView.get_view_of_file("/path/to/your/binary")

# Dictionary to store comments with function names as keys
comments = {}

# Iterate through all functions in the binary view
for func in bv.functions:
  # Initialize an empty list to store comments for the current function
  func_comments = []

  # Iterate through all comments in the function
  for addr, comment in func.comments.items():
    func_comments.append((addr, comment))

  # Add the function's comments to the dictionary
  comments[func.name] = func_comments

# Print the comments
for func_name, func_comments in comments.items():
  print(f"Comments for function {func_name}:")
  for addr, comment in func_comments:
    print(f"  Address: {addr:#x}, Comment: {comment}")

# Save comments to a file
with open("comments.txt", "w") as f:
  for func_name, func_comments in comments.items():
    f.write(f"Comments for function {func_name}:\n")
    for addr, comment in func_comments:
      f.write(f"  Address: {addr:#x}, Comment: {comment}\n")
    f.write("\n")