-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathswitch_active_program.cpp
More file actions
77 lines (65 loc) · 2.29 KB
/
Copy pathswitch_active_program.cpp
File metadata and controls
77 lines (65 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2024-2026 Elias Bachaalany
// SPDX-License-Identifier: MPL-2.0
//
// switch_active_program: import two binaries into one Ghidra project, query
// the first active program, close it, then open the second project program.
//
// Usage:
// switch_active_program <ghidra_dir> <project_dir> <project_name> <binary_a> <binary_b>
#include <iostream>
#include <string>
#include "libghidra/ghidra.hpp"
int main(int argc, char* argv[]) {
if (argc != 6) {
std::cerr << "Usage: " << argv[0]
<< " <ghidra_dir> <project_dir> <project_name> <binary_a> <binary_b>\n";
return 1;
}
ghidra::HeadlessOptions opts;
opts.ghidra_dir = argv[1];
opts.project_dir = argv[2];
opts.project_name = argv[3];
opts.binary = argv[4];
opts.binaries = {argv[5]};
opts.port = 0;
opts.shutdown = "save";
try {
auto host = ghidra::launch_headless(std::move(opts));
libghidra::client::ListProjectFilesRequest list_req;
list_req.programs_only = true;
auto listed = host->ListProjectFiles(list_req);
if (!listed.ok() || listed.value->files.size() < 2) {
std::cerr << "Expected at least two project programs\n";
return 1;
}
const auto first = listed.value->files[0].path;
const auto second = listed.value->files[1].path;
auto first_open = host->OpenProgram({.program_path = first});
if (!first_open.ok()) {
std::cerr << "Open first failed: " << first_open.status.message << "\n";
return 1;
}
std::cout << "Active: " << first << " -> " << first_open.value->program_name << "\n";
auto closed = host->CloseProgram(ghidra::ShutdownPolicy::kSave);
if (!closed.ok() || !closed.value->closed) {
std::cerr << "CloseProgram failed\n";
return 1;
}
libghidra::client::OpenProgramRequest open_second;
open_second.project_path = argv[2];
open_second.project_name = argv[3];
open_second.program_path = second;
auto second_open = host->OpenProgram(open_second);
if (!second_open.ok()) {
std::cerr << "Open second failed: " << second_open.status.message << "\n";
return 1;
}
std::cout << "Active: " << second << " -> " << second_open.value->program_name << "\n";
host.close(true);
}
catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}