Skip to content

Commit

Permalink
GP-4439: Add raw-gdb.sh and raw-python.sh. Add @no-image tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsadeveloper789 committed Mar 26, 2024
1 parent 523f6e4 commit f9bea77
Show file tree
Hide file tree
Showing 17 changed files with 651 additions and 333 deletions.
57 changes: 57 additions & 0 deletions Ghidra/Debug/Debugger-agent-gdb/data/debugger-launchers/raw-gdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
## ###
# IP: GHIDRA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
#@title raw gdb
#@no-image
#@desc <html><body width="300px">
#@desc <h3>Start <tt>gdb</tt></h3>
#@desc <p>This will start <tt>gdb</tt> and connect to it. It will not launch
#@desc a target, so you can (must) set up your target manually.
#@desc GDB must already
#@desc be installed on your system, and it must embed the Python 3 interpreter. You will also
#@desc need <tt>protobuf</tt> and <tt>psutil</tt> installed for Python 3.</p>
#@desc </body></html>
#@menu-group raw
#@icon icon.debugger
#@help TraceRmiLauncherServicePlugin#gdb
#@env OPT_GDB_PATH:str="gdb" "Path to gdb" "The path to gdb. Omit the full path to resolve using the system PATH."
#@env OPT_ARCH:str="i386:x86-64" "Architecture" "Target architecture"

if [ -d ${GHIDRA_HOME}/ghidra/.git ]
then
export PYTHONPATH=$GHIDRA_HOME/ghidra/Ghidra/Debug/Debugger-agent-gdb/build/pypkg/src:$PYTHONPATH
export PYTHONPATH=$GHIDRA_HOME/ghidra/Ghidra/Debug/Debugger-rmi-trace/build/pypkg/src:$PYTHONPATH
elif [ -d ${GHIDRA_HOME}/.git ]
then
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-agent-gdb/build/pypkg/src:$PYTHONPATH
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-rmi-trace/build/pypkg/src:$PYTHONPATH
else
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-agent-gdb/pypkg/src:$PYTHONPATH
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-rmi-trace/pypkg/src:$PYTHONPATH
fi

"$OPT_GDB_PATH" \
-q \
-ex "set pagination off" \
-ex "set confirm off" \
-ex "show version" \
-ex "python import ghidragdb" \
-ex "set architecture $OPT_ARCH" \
-ex "ghidra trace connect \"$GHIDRA_TRACE_RMI_ADDR\"" \
-ex "ghidra trace start" \
-ex "ghidra trace sync-enable" \
-ex "set confirm on" \
-ex "set pagination on"
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def ensure_batch(self):
def end_batch(self):
if self.batch is None:
return
commands.STATE.client.end_batch()
self.batch = None
commands.STATE.client.end_batch()

def check_skip_continue(self):
skip = self.skip_continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public interface TraceRmiLaunchOffer {
* @param sessions any terminal sessions created while launching the back-end. If there are more
* than one, they are distinguished by launcher-defined keys. If there are no
* sessions, then there was likely a catastrophic error in the launcher.
* @param acceptor the acceptor if waiting for a connection
* @param connection if the target connected back to Ghidra, that connection
* @param trace if the connection started a trace, the (first) trace it created
* @param exception optional error, if failed
Expand Down Expand Up @@ -138,7 +139,7 @@ default PromptMode getPromptMode() {
/**
* Re-write the launcher arguments, if desired
*
* @param launcher the launcher that will create the target
* @param offer the offer that will create the target
* @param arguments the arguments suggested by the offer or saved settings
* @param relPrompt describes the timing of this callback relative to prompting the user
* @return the adjusted arguments
Expand Down Expand Up @@ -176,7 +177,7 @@ default LaunchResult launchProgram(TaskMonitor monitor) {
* memorized. The opinion will generate each offer fresh each time, so it's important that the
* "same offer" have the same configuration name. Note that the name <em>cannot</em> depend on
* the program name, but can depend on the model factory and program language and/or compiler
* spec. This name cannot contain semicolons ({@ code ;}).
* spec. This name cannot contain semicolons ({@code ;}).
*
* @return the configuration name
*/
Expand Down Expand Up @@ -262,6 +263,8 @@ default String getMenuGroup() {
* The order of entries in the quick-launch drop-down menu is always most-recently to
* least-recently used. An entry that has never been used does not appear in the quick launch
* menu.
*
* @return the sub-group name for ordering in the menu
*/
default String getMenuOrder() {
return "";
Expand All @@ -285,4 +288,11 @@ default HelpLocation getHelpLocation() {
* @return the parameters
*/
Map<String, ParameterDescription<?>> getParameters();

/**
* Check if this offer requires an open program
*
* @return true if required
*/
boolean requiresImage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
## ###
# IP: GHIDRA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

#@title raw python
#@no-image
#@desc <html><body width="300px">
#@desc <h3>Start <tt>gdb</tt></h3>
#@desc <p>This will start <tt>python</tt>, import <tt>ghidratrace</tt> and connect to it.
#@desc This connector is made for those wanting to explore the TraceRMI API and possibly develop
#@desc a new connector. You will need <tt>protobuf</tt> installed for Python 3.</p>
#@desc </body></html>
#@menu-group raw
#@icon icon.debugger
#@help TraceRmiLauncherServicePlugin#gdb
#@env OPT_PYTHON_EXE:str="python" "Path to python" "The path to the Python 3 interpreter. Omit the full path to resolve using the system PATH."
#@env OPT_LANG:str="DATA:LE:64:default" "Ghidra Language" "The Ghidra LanguageID for the trace"
#@env OPT_COMP:str="pointer64" "Ghidra Compiler" "The Ghidra CompilerSpecID for the trace"

if [ -d ${GHIDRA_HOME}/ghidra/.git ]
then
export PYTHONPATH=$GHIDRA_HOME/ghidra/Ghidra/Debug/Debugger-rmi-trace/build/pypkg/src:$PYTHONPATH
elif [ -d ${GHIDRA_HOME}/.git ]
then
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-rmi-trace/build/pypkg/src:$PYTHONPATH
else
export PYTHONPATH=$GHIDRA_HOME/Ghidra/Debug/Debugger-rmi-trace/pypkg/src:$PYTHONPATH
fi

"$OPT_PYTHON_EXE" -i ../support/raw-python3.py
36 changes: 36 additions & 0 deletions Ghidra/Debug/Debugger-rmi-trace/data/support/raw-python3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## ###
# IP: GHIDRA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
from concurrent.futures import ThreadPoolExecutor
import os
import socket
import sys

from ghidratrace import *
from ghidratrace.client import *


REGISTRY = MethodRegistry(ThreadPoolExecutor(max_workers=1))

host = os.getenv("GHIDRA_TRACE_RMI_HOST")
port = int(os.getenv("GHIDRA_TRACE_RMI_PORT"))
c = socket.socket()
c.connect((host, port))
client = Client(
c, f"python-{sys.version_info.major}.{sys.version_info.minor}", REGISTRY)
print(f"Connected to {client.description} at {host}:{port}")

trace = client.create_trace("noname", os.getenv(
"OPT_LANG"), os.getenv("OPT_COMP"))
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ protected void launchBackEnd(TaskMonitor monitor, Map<String, TerminalSession> s
List<String> commandLine = new ArrayList<>();
Map<String, String> env = new HashMap<>(System.getenv());
prepareSubprocess(commandLine, env, args, address);
env.put("GHIDRA_LANGUAGE_ID", program.getLanguageID().toString());
if (program != null) {
env.put("GHIDRA_LANGUAGE_ID", program.getLanguageID().toString());
}

for (Map.Entry<String, TtyCondition> ent : attrs.extraTtys().entrySet()) {
if (!ent.getValue().isActive(args)) {
Expand All @@ -116,4 +118,9 @@ protected void launchBackEnd(TaskMonitor monitor, Map<String, TerminalSession> s
sessions.put("Shell",
runInTerminal(commandLine, env, script.getParentFile(), sessions.values()));
}

@Override
public boolean requiresImage() {
return !attrs.noImage();
}
}
Loading

0 comments on commit f9bea77

Please sign in to comment.