Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting a function signature from script #1126

Closed
james-tate opened this issue Oct 9, 2019 · 4 comments
Closed

Setting a function signature from script #1126

james-tate opened this issue Oct 9, 2019 · 4 comments

Comments

@james-tate
Copy link

For the life of me I cannot fine a script API call for setting a functions signature. I have found, FunctionUtility.updateFunction​(dst,src), and how the GUI does it in EditFunctionSignatureDialog.java. I have been able to setVarArgs(). Does script API call exist for setting a function's arguments? If so can someone point me to it?

@dragonmacher
Copy link
Collaborator

dragonmacher commented Oct 9, 2019

I was a bit surprised at just how unobvious this actually was (I would have expected more related methods in the scripting API).

The 'Ghidra Way' of modifying items that are stored in the database, such as a function, involves using an existing Command (or creating one as needed). In this case, you should be using the ghidra.app.cmd.function.ApplyFunctionSignatureCmd class. It has multiple constructors, with the base constructor taking an Address and a FunctionSignature. So, in you script, build a FunctionSignature to you liking and use that to create the above command. Then, use the script API runCommand(command) method to have the new signature applied.

Command objects represent capsules/units of work that get performed together. Further, when a command is run, it is wrapped in a database transaction, which means the entire operation is atomic. Finally, this atomicity allows the undo/redo feature to work correctly.

@james-tate
Copy link
Author

Perfect! I found the ApplyFunctionSignatureCmd class, but could not figure out how to run the command after it was constructed. I was looking for an execute(cmd) and totally missed the runCommand(cmd). Thanks!

@astrelsky
Copy link
Contributor

Perfect! I found the ApplyFunctionSignatureCmd class, but could not figure out how to run the command after it was constructed. I was looking for an execute(cmd) and totally missed the runCommand(cmd). Thanks!

Can also use cmd.applyTo(currentProgram)

@crashingstatic
Copy link

I wrote a Ghidra script which does the correction automatically.

https://gist.github.com/crashingstatic/71f9ca586e43178fbea3317a93e1df59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants