Skip to content

Commit cc8ac41

Browse files
authored
Merge pull request #1 from TorgoTorgo/subclass_ipythonkernel
Subclass ipythonkernel
2 parents fa5064d + 8ae24ab commit cc8ac41

File tree

3 files changed

+20
-45
lines changed

3 files changed

+20
-45
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.ipynb_checkpoints/
2+
__pycache__/
3+
*.egg-info/

ghidra-notebook/ghidra_kernel.py

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#!/usr/bin/env python3
22
from ghidra_bridge import GhidraBridge
3-
from ipykernel.kernelbase import Kernel
4-
import logging
5-
import traceback
6-
from io import StringIO
7-
from contextlib import redirect_stdout
3+
from ipykernel.ipkernel import IPythonKernel
84

9-
class GhidraKernel(Kernel):
5+
class GhidraKernel(IPythonKernel):
106
implementation = 'ghidra_kernel'
117
implementation_version = '1.0.0'
128
language_info = {
@@ -26,70 +22,43 @@ class GhidraKernel(Kernel):
2622
@property
2723
def bridge(self):
2824
if self._bridge is None:
29-
# Reset any state
30-
self._globals = {}
31-
self._locals = {}
3225
print("Connecting to the bridge")
33-
self._bridge = GhidraBridge(namespace=self._globals,
26+
self._bridge = GhidraBridge(namespace=self.shell.user_ns, # from IPythonKernel
3427
interactive_mode=True,
3528
hook_import=True)
3629
return self._bridge
3730
else:
3831
return self._bridge
32+
3933
_banner: str = None
4034
@property
4135
def banner(self):
4236
if self._banner is None:
43-
self._banner = "Ghidra Python3 Kernel"
37+
self._banner = "Ghidra Python3 Kernel (connected to {host}:{port})".format(host=self.bridge.client.host, port=self.bridge.client.port)
4438
return self._banner
4539
else:
4640
return self._banner
47-
_globals: dict = {}
48-
_locals: dict = {}
49-
50-
41+
5142
def __init__(self, **kwargs):
5243
print("Starting GhidraKernel...")
53-
Kernel.__init__(self, **kwargs)
54-
_ = self.bridge
55-
56-
def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
57-
_ = self.bridge
58-
out_buff = StringIO()
59-
try:
60-
with redirect_stdout(out_buff):
61-
exec(code, self._globals, self._locals)
62-
result = out_buff.getvalue()
63-
content = {
64-
'name': 'stdout',
65-
'text': str(result)
66-
}
67-
except Exception as e:
68-
result = traceback.format_exc()
69-
content = {'name': 'stderr', 'text': str(result)}
70-
self.send_response(self.iopub_socket, 'stream', content)
71-
72-
return {
73-
'status': 'ok',
74-
'execution_count': self.execution_count,
75-
'payload': [],
76-
'user_expressions': {}
77-
}
78-
44+
super(GhidraKernel, self).__init__(**kwargs)
45+
self.shell.user_ns["bridge"] = self.bridge
46+
7947
def do_shutdown(self, restart):
8048
# We probably don't want to _actually_
8149
# shut down the server within Ghidra
8250
# self.bridge.remote_shutdown()
8351
self._bridge = None
84-
self._locals = {}
85-
self._globals = {}
52+
53+
super(GhidraKernel, self).do_shutdown(restart)
8654

8755
if restart:
8856
_ = self.bridge
8957
return {
9058
'status': 'ok',
9159
'restart': restart
92-
}
60+
}
61+
9362
if __name__ == '__main__':
9463
from ipykernel.kernelapp import IPKernelApp
9564
IPKernelApp.launch_instance(kernel_class=GhidraKernel)

ghidra-notebook/install.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"display_name": "Ghidra",
1111
"language": "python3",
1212
"codemirror_mode": "python3",
13-
"env": {}
13+
"env": {},
14+
"metadata": {
15+
"debugger": True
16+
}
1417
}
1518

1619

0 commit comments

Comments
 (0)