-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Add scripts to export function metrics (JSON/CSV) and function call graph (DOT) #8574
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
base: master
Are you sure you want to change the base?
Add scripts to export function metrics (JSON/CSV) and function call graph (DOT) #8574
Conversation
ryanmkurtz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense to add this functionality to the existing ExportFunctionInfoScript. GhidraScript.askChoices() could be used to choose the export format.
- Consolidated ExportFunctionMetricsToJson, ExportFunctionMetricsToCsv, and ExportFunctionCallGraphToDot into ExportFunctionInfoScript.java - Added format selection using GhidraScript.askChoices() as requested in code review - Users can now choose between: JSON (Simple), JSON (Detailed metrics), CSV (Detailed metrics), and DOT (Function call graph) - All export formats support optional selection/highlight filtering - Maintains backward compatibility with existing simple JSON export functionality
|
|
Thanks, I hope to review it next week. |
|
Apologies, I was away for a couple of weeks. I hope to actually review it this week. |
| monitor.incrementProgress(1); | ||
|
|
||
| String fEntry = f.getEntryPoint().toString(); | ||
| idByEntry.computeIfAbsent(fEntry, k -> nextId++); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler doesn't like nextId being modified within the lambda. This logic will have to change.
ExportFunctionInfoScript.java:435: error: local variables referenced from a lambda expression must be final or effectively final
idByEntry.computeIfAbsent(fEntry, k -> nextId++);
| continue; | ||
| } | ||
| String cEntry = callee.getEntryPoint().toString(); | ||
| idByEntry.computeIfAbsent(cEntry, k -> nextId++); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExportFunctionInfoScript.java:443: error: local variables referenced from a lambda expression must be final or effectively final
idByEntry.computeIfAbsent(cEntry, k -> nextId++);
| "Select the format for exporting function information:", formats, ExportFormat.JSON_SIMPLE); | ||
|
|
||
| // Determine file extension based on format | ||
| String extension; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable extension
| } | ||
| } | ||
| catch (CancelledException ce) { | ||
| // Respect cancellation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just allow any CancelledExceptions to propagate out of the run() method, rather than trying to handle each one like this. You'd want the script to immediately stop running when cancel is clicked anyway.
|
|
||
| // Instruction count | ||
| int instCount = 0; | ||
| for (Iterator<Instruction> insIt = listing.getInstructions(body, true); insIt.hasNext();) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should just be able to use a for-each style loop here (and elsewhere)
Summary
ExportFunctionMetricsToJson: per-function metrics to JSON with a summary section and optional selection/highlight filtering.ExportFunctionMetricsToCsv: same metrics to CSV with a stable header.ExportFunctionCallGraphToDot: exports the program function call graph to Graphviz DOT; supports selection/highlight filtering.Motivation
Details
name,entry,namespacesize_bytes(address count),address_ranges,instruction_count,basic_blockscyclomatic_complexity,parameters,localsexternal,thunk,variadic,inline,no_return,custom_storagestack_purge_size,calling_convention,callers,calleesreturn_type,prototype,prototype_with_ccfunctionName\nentryAddressand writes simple edges from callers to callees.Usage
.json.csv.dotFiles Changed
Ghidra/Features/Base/ghidra_scripts/ExportFunctionMetricsToJson.java:1Ghidra/Features/Base/ghidra_scripts/ExportFunctionMetricsToCsv.java:1Ghidra/Features/Base/ghidra_scripts/ExportFunctionCallGraphToDot.java:1How To Test
program,functions[], andsummary.dot -Tpng out.dot -o out.png, and reflects expected edges.Scope / Compliance
ghidra_scripts.Notes
size_bytesreports address count in the function body;address_rangesclarifies segmentation.Optional Checklist