Debian Codegraph @

interactive-notebook/isystemtap/stap_kernel.py

raw
1 # systemtap jupyter kernel implementation
2 # Copyright (C) 2023 Red Hat Inc.
3 #
4 # This file is part of systemtap, and is free software. You can
5 # redistribute it and/or modify it under the terms of the GNU General
6 # Public License (GPL); either version 2, or (at your option) any
7 # later version.
8 #
9 # The kernel borrows some functions from generic functions from
10 # the jupyter metakernel, https://github.com/Calysto/metakernel
11
12 import base64
13 import logging
14 import sys
15 from .stap_jobjects import *
16 from ipywidgets import register_comm_target
17 from ipywidgets.widgets.widget import Widget
18 import ipykernel
19 assert ipykernel.__version__ > '6.12', \
20 f'The ipykernel version must be at least 6.12, where cellIds are introduced, the current version is {ipykernel.__version__}'
21 from ipykernel.kernelbase import Kernel
22 from ipykernel.kernelapp import IPKernelApp
23 from ipykernel.comm import CommManager
24 import io
25 import os
26 import time
27 # File is created at build time
28 try:
29 from .constants import STAP_VERSION
30 except:
31 STAP_VERSION = '0.0'
32
33 class SystemtapKernel(Kernel):
34 implementation = 'isystemtap'
35 implementation_version = '1.0'
36 language = 'Systemtap'
37 language_version = STAP_VERSION
38 language_info = {
39 'name': 'systemtap',
40 'mimetype': 'text/x-systemtap',
41 'file_extension': '.stp',
42 'codemirror_mode': 'systemtap'
43 }
44 banner = "ISystemtap Kernel"
45 debugger = False # Kernel does not support debugging in the notebook
46 help_links = [
47 {"text": "Systemtap Wiki", "url": "https://sourceware.org/systemtap/"},
48 {"text": "bqplot", "url": "https://bqplot.github.io/bqplot/api/pyplot/"}
49 ]
50
51 @classmethod
52 def run_as_main(cls):
53 IPKernelApp.launch_instance(kernel_class=cls)
54
55 def __init__(self, *args, **kwargs):
56 super().__init__(*args, **kwargs)
57
58 try:
59 sys.stdout.write = self.write
60 except:
61 pass
62
63 # We log to both a file (DEBUG) and stderr (INFO)
64 if self.log is None:
65 self.log = logging.Logger("stap_kernel")
66 logdir = f'{os.getenv("HOME")}/.systemtap/jupyter/logs'
67 if not os.path.exists(logdir):
68 os.mkdir(logdir)
69 logfile = f'{logdir}/kernel_{time.strftime("%Y%m%d_%H%M%S")}.log'
70 f_handler = logging.FileHandler(logfile, mode='w')
71 f_handler.setFormatter(logging.Formatter(
72 '%(asctime)s - %(levelname)s - %(filename)s::%(funcName)s - %(message)s'))
73 f_handler.setLevel(logging.DEBUG)
74 self.log.addHandler(f_handler)
75 # Jupyter plays some games with sys.stderr so we need to open fd=2 in order to log to the terminal
76 s_handler = logging.StreamHandler(io.TextIOWrapper(io.BufferedWriter(io.FileIO(2, mode='w'))))
77 s_handler.setFormatter(logging.Formatter(
78 '[%(levelname).1s %(asctime)s ISystemtapKernel] %(message)s'))
79 s_handler.setLevel(logging.INFO)
80 self.log.addHandler(s_handler)
81 self.log.info(f'For the full log see {logfile}')
82
83 self.jnamespaces = dict() # namespace name -> JNamespace
84
85 # Setup communication channels, which the kernel uses to interact
86 # with widgets
87 self.shell = None
88 self.comm_manager = CommManager(parent=self, kernel=self, shell=self.shell)
89 register_comm_target(self)
90 comm_msg_types = ['comm_open', 'comm_msg', 'comm_close']
91 for msg_type in comm_msg_types:
92 self.shell_handlers[msg_type] = getattr(
93 self.comm_manager, msg_type)
94
95 def display(self, *objects, **kwargs):
96 """Display one or more objects using rich display.
97 Supports a `clear_output` keyword argument that clears the output before displaying.
98 """
99 if kwargs.get('clear_output'):
100 self.send_response(self.iopub_socket, 'clear_output',
101 {'wait': True})
102
103 for item in objects:
104 if isinstance(item, Widget):
105 data = {
106 'text/plain': repr(item),
107 'application/vnd.jupyter.widget-view+json': {
108 'version_major': 2,
109 'version_minor': 0,
110 'model_id': item._model_id
111 }
112 }
113 content = {
114 'data': data,
115 'metadata': {}
116 }
117 self.send_response(
118 self.iopub_socket,
119 'display_data',
120 content
121 )
122 else:
123 try:
124 data = _formatter(item)
125 except Exception as e:
126 self.error(e)
127 return
128 content = {
129 'data': data[0],
130 'metadata': data[1]
131 }
132 self.send_response(
133 self.iopub_socket,
134 'display_data',
135 content
136 )
137
138 def print(self, *objects, **kwargs):
139 """Print objects to the iopub stream, separated by sep and followed by end
140 Items can be strings or Widget instances.
141 """
142 for item in objects:
143 if isinstance(item, Widget):
144 self.display(item)
145
146 objects = [o for o in objects if not isinstance(o, Widget)]
147 message = format_message(*objects, **kwargs)
148
149 stream_content = {
150 'name': 'stdout', 'text': message}
151 self.send_response(self.iopub_socket, 'stream', stream_content)
152
153 def write(self, message):
154 """Write message directly to the iopub stdout with no added end character."""
155 stream_content = {
156 'name': 'stdout', 'text': message}
157 self.send_response(self.iopub_socket, 'stream', stream_content)
158
159 def error(self, *objects, **kwargs):
160 """Print objects to stdout, separated by sep and followed by end
161 Objects are cast to strings.
162 """
163 message = format_message(*objects, **kwargs)
164 self.log.info(f'Error: {message}')
165 stream_content = {
166 'name': 'stderr',
167 'text': message
168 }
169 self.send_response(self.iopub_socket, 'stream', stream_content)
170
171 def do_execute(self, code, silent=False, store_history=False, user_expressions=None,
172 allow_stdin=False, *, cell_id=None):
173 """Execute a cell (with cell_id) containing code.
174 Disregard other user arguments
175 """
176 # NB: ipykernel.kernelbase::_accepts_cell_id requires cell_id in do_execute to be VAR_KEYWORD
177 # So in the above the '*' IS REQUIRED
178 # As of 2022: Jupyterlab supports cell_id but notebook does not
179 cell = JCell(cell_id, kernel=self)
180 return cell.execute(code)
181
182 def do_complete(self, code, cursor_pos):
183 if cursor_pos is None:
184 cursor_pos = len(code)
185
186 if len(code) > cursor_pos:
187 code = code[:cursor_pos]
188
189 line = code.split('\n')[-1]
190 matches = []
191 cursor_start = cursor_pos
192 # We complete magics here and the remainder in the Language Server
193 if len(line) >= 2 and line[:2] == "%%":
194 magic_command = line[2:].split(" ")
195 # Try and complete the magic
196 if len(magic_command) == 1:
197 cursor_start = cursor_pos - len(magic_command[0])
198 matches = (CellExeMode.EDIT, CellExeMode.EXAMPLES, CellExeMode.HELP,
199 CellExeMode.PROBES, CellExeMode.PYTHON, CellExeMode.RUN, CellExeMode.SCRIPT)
200 else:
201 # We can try and offer namespace matching
202 cursor_start = cursor_pos - len(magic_command[1])
203 matches = [ns for ns in self.jnamespaces.keys() if ns.startswith(magic_command[1])]
204
205 return {
206 'matches': matches,
207 'cursor_start': cursor_start,
208 'cursor_end': cursor_pos,
209 'status': 'ok',
210 'metadata': {}
211 }
212
213 def do_shutdown(self, restart):
214 """Shut down the app gracefully"""
215 # Shutdown any running namespace (only 1 can run at a time)
216 for ns in self.jnamespaces.values():
217 try:
218 with open(f'/proc/systemtap/{ns.mod_name}/monitor_control', 'w') as f:
219 f.write('quit')
220 f.flush()
221 except OSError:
222 pass
223 if restart:
224 self.log.info("Restarting kernel...")
225 return {'status': 'ok', 'restart': restart}
226
227 def _formatter(data):
228 reprs = {}
229 reprs['text/plain'] = repr(data)
230
231 lut = [("_repr_png_", "image/png"),
232 ("_repr_jpeg_", "image/jpeg"),
233 ("_repr_html_", "text/html"),
234 ("_repr_markdown_", "text/markdown"),
235 ("_repr_svg_", "image/svg+xml"),
236 ("_repr_latex_", "text/latex"),
237 ("_repr_json_", "application/json"),
238 ("_repr_javascript_", "application/javascript"),
239 ("_repr_pdf_", "application/pdf")]
240
241 for (attr, mimetype) in lut:
242 obj = getattr(data, attr, None)
243 if obj:
244 reprs[mimetype] = obj
245
246 format_dict = {}
247 metadata_dict = {}
248 for (mimetype, value) in reprs.items():
249 metadata = None
250 try:
251 value = value()
252 except Exception:
253 pass
254 if not value:
255 continue
256 if isinstance(value, tuple):
257 metadata = value[1]
258 value = value[0]
259 if isinstance(value, bytes):
260 try:
261 value = value.decode('utf-8')
262 except Exception:
263 value = base64.encodestring(value)
264 value = value.decode('utf-8')
265 try:
266 format_dict[mimetype] = str(value)
267 except:
268 format_dict[mimetype] = value
269 if metadata is not None:
270 metadata_dict[mimetype] = metadata
271 return (format_dict, metadata_dict)
272
273 def format_message(*objects, **kwargs):
274 """
275 Format a message like print() does.
276 """
277 objects = [str(i) for i in objects]
278 sep = kwargs.get('sep', ' ')
279 end = kwargs.get('end', '\n')
280 return sep.join(objects) + end
281