question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

show Dialog Changes Order of Provided Addresses

See original GitHub issue

I have an instruction trace and I’m particularly interested in generating a Ghidra dialogue that lists each address I give it in the order given. I managed to nearly accomplish this with the show dialog. However, the address array I give the show function is implicitly sorted by Ghidra and that is not the behavior I want.

Here is a simple example. The addresses provided are arbitrary, the point is 0x10a09c is the second item in the array and 0x10a098 is the third item in the array. You can replace these addresses with any valid addresses for your binary, just make sure they are not sorted.

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.*;

public class TraceGoTo extends GhidraScript {

	private static String addresses[] = new String[] {"0x10a094", "0x10a09c", "0x10a098"};
			
	public void run() throws Exception {
		Address addrs[] = new Address[3];
		for (int i = 0; i < 1000 && i < addresses.length; i++) {
			addrs[i] = toAddr(addresses[i]);
		}
		show(addrs);		
	}
}

The show dialog should display a table with the addresses given in the exact order they are given. It should not automatically sort the results. I believe what is happening is the table is automatically sorting by address, which again is not what I want.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
dragonmachercommented, Jul 12, 2022

This is crazy.

There are much crazier things than this.

Is there really no way to tell Ghidra to create a table of addresses without automatically sorting it?

There are many ways to do this, but not via scripting.

It should not automatically sort the results.

That depends on you use case. It used to be that all tables were always sorted. In those ancient times we would have simply added an ordinal column to go a long with the addresses, with the sort being on the ordinal column. We added the ability to have tables be unsorted some years back. The code below uses this ability.

Below is a script that shows a table with unsorted addresses with relatively few lines of code, taken from what the script code does internally.

Note: there are no ‘Next’ and ‘Previous’ buttons in this example. However, you can simply use the arrow keys in the table to accomplish the equivalent behavior. You could certainly build a more complicated custom dialog if you really wanted such buttons.

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import docking.widgets.table.TableSortState;
import ghidra.app.plugin.core.table.TableComponentProvider;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.query.TableService;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.address.Address;
import ghidra.util.Swing;
import ghidra.util.table.AddressArrayTableModel;

public class ShowAddressesScript extends GhidraScript {

	@Override
	public void run() throws Exception {

		PluginTool tool = state.getTool();
		TableService tableService = tool.getService(TableService.class);

		List<Address> list = new ArrayList<>();
		list.add(toAddr("00403510"));
		list.add(toAddr("00403530"));
		list.add(toAddr("00403590"));

		Address[] addresses = list.toArray(Address[]::new);

		Swing.runNow(() -> {
			AddressArrayTableModel model = new AddressArrayTableModel(getScriptName(),
				state.getTool(), currentProgram, null);
			model.setTableSortState(TableSortState.createUnsortedSortState());
			model.setAddresses(addresses);

			TableComponentProvider<Address> tableProvider =
				tableService.showTableWithMarkers("Unsorted Addresses", "GhidraScript", model,
					Color.GREEN, null, "Script Results", null);
			tableProvider.installRemoveItemsAction();
		});

	}
}
0reactions
dragonmachercommented, Jul 12, 2022

That is a fairly old version of Ghidra. Perhaps there is a related bug. I verified that changing the order of addresses in the list works as expected for me in Ghidra 10.1.5.

Glad to hear it worked!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I change list order in a dialog box on Android
I have a dialog listing my results from a sqlite database but it list them backward (oldest entries first). How can I resort...
Read more >
Displaying dialogs with DialogFragment - Android Developers
Note: Because the DialogFragment is automatically restored after configuration changes, consider only calling show() based on user actions or ...
Read more >
Modal Dialog Example | APG | WAI - W3C
Following is an example implementation of the design pattern for modal dialogs. The below Add Delivery Address button opens a modal dialog ......
Read more >
Windows 7 Dialog Boxes (Design basics) - Win32 apps
A dialog box is a secondary window that allows users to perform a command, asks users a question, or provides users with information...
Read more >
The Dialog element - HTML: HyperText Markup Language
Use the appropriate .showModal() or .show() method to render dialogs. If creating a custom dialog implementation, ensure all expected default behaviors are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found