Skip to content

libdebug.commlink.pipe_manager

PipeManager

Class for managing pipes of the child process.

Source code in libdebug/commlink/pipe_manager.py
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
class PipeManager:
    """Class for managing pipes of the child process."""

    timeout_default: int = 2
    prompt_default: str = "$ "

    def __init__(self: PipeManager, stdin_write: int, stdout_read: int, stderr_read: int) -> None:
        """Initializes the PipeManager class.

        Args:
            stdin_write (int): file descriptor for stdin write.
            stdout_read (int): file descriptor for stdout read.
            stderr_read (int): file descriptor for stderr read.
        """
        self._stdin_write: int = stdin_write
        self._stdout_read: int = stdout_read
        self._stderr_read: int = stderr_read
        self._stderr_is_open: bool = True
        self._stdout_is_open: bool = True
        self._internal_debugger: InternalDebugger = provide_internal_debugger(self)

        self.__stdout_buffer: BufferData = BufferData(b"")
        self.__stderr_buffer: BufferData = BufferData(b"")

        self.__end_interactive_event: Event = Event()

    def _raw_recv(
        self: PipeManager,
        numb: int | None = None,
        timeout: float | None = None,
        stderr: bool = False,
    ) -> int:
        """Receives at most numb bytes from the child process.

        Args:
            numb (int | None, optional): number of bytes to receive. Defaults to None.
            timeout (float, optional): timeout in seconds. Defaults to None.
            stderr (bool, optional): receive from stderr. Defaults to False.

        Returns:
            int: number of bytes received.
        """
        pipe_read: int = self._stderr_read if stderr else self._stdout_read

        if not pipe_read:
            raise RuntimeError("No pipe of the child process")

        data_buffer = self.__stderr_buffer if stderr else self.__stdout_buffer

        received_numb = 0

        if numb is not None and timeout is not None:
            # Checking the numb
            if numb < 0:
                raise ValueError("The number of bytes to receive must be positive")

            # Setting the alarm
            end_time = time.time() + timeout

            while numb > received_numb:
                if (remaining_time := max(0, end_time - time.time())) == 0:
                    # Timeout reached
                    break

                try:
                    ready, _, _ = select.select([pipe_read], [], [], remaining_time)
                    if ready:
                        data = os.read(pipe_read, 4096)
                        received_numb += len(data)
                        data_buffer.append(data)
                    else:
                        # No more data available in the pipe at the moment
                        break
                except OSError as e:
                    if e.errno != EAGAIN:
                        if stderr:
                            self._stderr_is_open = False
                        else:
                            self._stdout_is_open = False
        elif timeout is not None:
            try:
                ready, _, _ = select.select([pipe_read], [], [], timeout)
                if ready:
                    data = os.read(pipe_read, 4096)
                    received_numb += len(data)
                    data_buffer.append(data)
            except OSError as e:
                if e.errno != EAGAIN:
                    if stderr:
                        self._stderr_is_open = False
                    else:
                        self._stdout_is_open = False
        else:
            try:
                data = os.read(pipe_read, 4096)
                if data:
                    received_numb += len(data)
                    data_buffer.append(data)
            except OSError as e:
                if e.errno != EAGAIN:
                    if stderr:
                        self._stderr_is_open = False
                    else:
                        self._stdout_is_open = False

        if received_numb:
            liblog.pipe(f"{'stderr' if stderr else 'stdout'} {received_numb}B: {data_buffer[:received_numb]!r}")
        return received_numb

    def close(self: PipeManager) -> None:
        """Closes all the pipes of the child process."""
        os.close(self._stdin_write)
        os.close(self._stdout_read)
        os.close(self._stderr_read)

    def _buffered_recv(self: PipeManager, numb: int, timeout: int, stderr: bool) -> bytes:
        """Receives at most numb bytes from the child process stdout or stderr.

        Args:
            numb (int): number of bytes to receive.
            timeout (int): timeout in seconds.
            stderr (bool): receive from stderr.

        Returns:
            bytes: received bytes from the child process stdout or stderr.
        """
        data_buffer = self.__stderr_buffer if stderr else self.__stdout_buffer
        open_flag = self._stderr_is_open if stderr else self._stdout_is_open

        data_buffer_len = len(data_buffer)

        if data_buffer_len >= numb:
            # We have enough data in the buffer
            received = data_buffer[:numb]
            data_buffer.overwrite(data_buffer[numb:])
        elif open_flag:
            # We can receive more data
            remaining = numb - data_buffer_len
            self._raw_recv(numb=remaining, timeout=timeout, stderr=stderr)
            received = data_buffer[:numb]
            data_buffer.overwrite(data_buffer[numb:])
        elif data_buffer_len != 0:
            # The pipe is not available but we have some data in the buffer. We will return just that
            received = data_buffer.get_data()
            data_buffer.clear()
        else:
            # The pipe is not available and no data is buffered
            raise RuntimeError(f"Broken {'stderr' if stderr else 'stdout'} pipe. Is the child process still alive?")
        return received

    def recv(
        self: PipeManager,
        numb: int = 4096,
        timeout: int = timeout_default,
    ) -> bytes:
        """Receives at most numb bytes from the child process stdout.

        Args:
            numb (int, optional): number of bytes to receive. Defaults to 4096.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.

        Returns:
            bytes: received bytes from the child process stdout.
        """
        return self._buffered_recv(numb=numb, timeout=timeout, stderr=False)

    def recverr(
        self: PipeManager,
        numb: int = 4096,
        timeout: int = timeout_default,
    ) -> bytes:
        """Receives at most numb bytes from the child process stderr.

        Args:
            numb (int, optional): number of bytes to receive. Defaults to 4096.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.

        Returns:
            bytes: received bytes from the child process stderr.
        """
        return self._buffered_recv(numb=numb, timeout=timeout, stderr=True)

    def _recvonceuntil(
        self: PipeManager,
        delims: bytes,
        drop: bool = False,
        timeout: float = timeout_default,
        stderr: bool = False,
        optional: bool = False,
    ) -> bytes:
        """Receives data from the child process until the delimiters are found.

        Args:
            delims (bytes): delimiters where to stop.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (float, optional): timeout in seconds. Defaults to timeout_default.
            stderr (bool, optional): receive from stderr. Defaults to False.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stdout.
        """
        if isinstance(delims, str):
            liblog.warning("The delimiters are a string, converting to bytes")
            delims = delims.encode()

        # Buffer for the received data
        data_buffer = self.__stderr_buffer if stderr else self.__stdout_buffer

        # Setting the alarm
        end_time = time.time() + timeout
        while True:
            open_flag = self._stderr_is_open if stderr else self._stdout_is_open

            if (until := data_buffer.find(delims)) != -1:
                break

            if (remaining_time := max(0, end_time - time.time())) == 0:
                raise TimeoutError("Timeout reached")

            if not open_flag:
                # The delimiters are not in the buffer and the pipe is not available
                raise RuntimeError(f"Broken {'stderr' if stderr else 'stdout'} pipe. Is the child process still alive?")

            received_numb = self._raw_recv(stderr=stderr, timeout=remaining_time)

            if (
                received_numb == 0
                and not self._internal_debugger.running
                and self._internal_debugger.is_debugging
                and (event := self._internal_debugger.resume_context.get_event_type())
            ):
                # We will not receive more data, the child process is not running
                if optional:
                    return b""
                event = self._internal_debugger.resume_context.get_event_type()
                raise RuntimeError(
                    f"Receive until error. The debugged process has stopped due to the following event(s). {event}",
                )
        received_data = data_buffer[:until]
        if not drop:
            # Include the delimiters in the received data
            received_data += data_buffer[until : until + len(delims)]
        remaining_data = data_buffer[until + len(delims) :]
        data_buffer.overwrite(remaining_data)
        return received_data

    def _recvuntil(
        self: PipeManager,
        delims: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: float = timeout_default,
        stderr: bool = False,
        optional: bool = False,
    ) -> bytes:
        """Receives data from the child process until the delimiters are found occurences time.

        Args:
            delims (bytes): delimiters where to stop.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (float, optional): timeout in seconds. Defaults to timeout_default.
            stderr (bool, optional): receive from stderr. Defaults to False.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stdout.
        """
        if occurences <= 0:
            raise ValueError("The number of occurences to receive must be positive")

        # Buffer for the received data
        data_buffer = b""

        # Setting the alarm
        end_time = time.time() + timeout

        for _ in range(occurences):
            # Adjust the timeout for select to the remaining time
            remaining_time = None if end_time is None else max(0, end_time - time.time())

            data_buffer += self._recvonceuntil(
                delims=delims,
                drop=drop,
                timeout=remaining_time,
                stderr=stderr,
                optional=optional,
            )

        return data_buffer

    def recvuntil(
        self: PipeManager,
        delims: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> bytes:
        """Receives data from the child process stdout until the delimiters are found.

        Args:
            delims (bytes): delimiters where to stop.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stdout.
        """
        return self._recvuntil(
            delims=delims,
            occurences=occurences,
            drop=drop,
            timeout=timeout,
            stderr=False,
            optional=optional,
        )

    def recverruntil(
        self: PipeManager,
        delims: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> bytes:
        """Receives data from the child process stderr until the delimiters are found.

        Args:
            delims (bytes): delimiters where to stop.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stderr.
        """
        return self._recvuntil(
            delims=delims,
            occurences=occurences,
            drop=drop,
            timeout=timeout,
            stderr=True,
            optional=optional,
        )

    def recvline(
        self: PipeManager,
        numlines: int = 1,
        drop: bool = True,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> bytes:
        """Receives numlines lines from the child process stdout.

        Args:
            numlines (int, optional): number of lines to receive. Defaults to 1.
            drop (bool, optional): drop the line ending. Defaults to True.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received lines from the child process stdout.
        """
        return self.recvuntil(delims=b"\n", occurences=numlines, drop=drop, timeout=timeout, optional=optional)

    def recverrline(
        self: PipeManager,
        numlines: int = 1,
        drop: bool = True,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> bytes:
        """Receives numlines lines from the child process stderr.

        Args:
            numlines (int, optional): number of lines to receive. Defaults to 1.
            drop (bool, optional): drop the line ending. Defaults to True.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received lines from the child process stdout.
        """
        return self.recverruntil(delims=b"\n", occurences=numlines, drop=drop, timeout=timeout, optional=optional)

    def send(self: PipeManager, data: bytes) -> int:
        """Sends data to the child process stdin.

        Args:
            data (bytes): data to send.

        Returns:
            int: number of bytes sent.

        Raises:
            RuntimeError: no stdin pipe of the child process.
        """
        if not self._stdin_write:
            raise RuntimeError("No stdin pipe of the child process")

        liblog.pipe(f"Sending {len(data)} bytes to the child process: {data!r}")

        if isinstance(data, str):
            liblog.warning("The input data is a string, converting to bytes")
            data = data.encode()

        try:
            number_bytes = os.write(self._stdin_write, data)
        except OSError as e:
            raise RuntimeError("Broken pipe. Is the child process still running?") from e

        return number_bytes

    def sendline(self: PipeManager, data: bytes) -> int:
        """Sends data to the child process stdin and append a newline.

        Args:
            data (bytes): data to send.

        Returns:
            int: number of bytes sent.
        """
        if isinstance(data, str):
            liblog.warning("The input data is a string, converting to bytes")
            data = data.encode()
        return self.send(data=data + b"\n")

    def sendafter(
        self: PipeManager,
        delims: bytes,
        data: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> tuple[bytes, int]:
        """Sends data to the child process stdin after the delimiters are found in the stdout.

        Args:
            delims (bytes): delimiters where to stop.
            data (bytes): data to send.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stdout.
            int: number of bytes sent.
        """
        received = self.recvuntil(delims=delims, occurences=occurences, drop=drop, timeout=timeout, optional=optional)
        sent = self.send(data)
        return (received, sent)

    def sendaftererr(
        self: PipeManager,
        delims: bytes,
        data: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> tuple[bytes, int]:
        """Sends data to the child process stdin after the delimiters are found in stderr.

        Args:
            delims (bytes): delimiters where to stop.
            data (bytes): data to send.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stderr.
            int: number of bytes sent.
        """
        received = self.recverruntil(
            delims=delims,
            occurences=occurences,
            drop=drop,
            timeout=timeout,
            optional=optional,
        )
        sent = self.send(data)
        return (received, sent)

    def sendlineafter(
        self: PipeManager,
        delims: bytes,
        data: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> tuple[bytes, int]:
        """Sends line to the child process stdin after the delimiters are found in the stdout.

        Args:
            delims (bytes): delimiters where to stop.
            data (bytes): data to send.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stdout.
            int: number of bytes sent.
        """
        received = self.recvuntil(delims=delims, occurences=occurences, drop=drop, timeout=timeout, optional=optional)
        sent = self.sendline(data)
        return (received, sent)

    def sendlineaftererr(
        self: PipeManager,
        delims: bytes,
        data: bytes,
        occurences: int = 1,
        drop: bool = False,
        timeout: int = timeout_default,
        optional: bool = False,
    ) -> tuple[bytes, int]:
        """Sends line to the child process stdin after the delimiters are found in the stderr.

        Args:
            delims (bytes): delimiters where to stop.
            data (bytes): data to send.
            occurences (int, optional): number of delimiters to find. Defaults to 1.
            drop (bool, optional): drop the delimiter. Defaults to False.
            timeout (int, optional): timeout in seconds. Defaults to timeout_default.
            optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

        Returns:
            bytes: received data from the child process stderr.
            int: number of bytes sent.
        """
        received = self.recverruntil(
            delims=delims,
            occurences=occurences,
            drop=drop,
            timeout=timeout,
            optional=optional,
        )
        sent = self.sendline(data)
        return (received, sent)

    def _recv_for_interactive(self: PipeManager) -> None:
        """Receives data from the child process."""
        stdout_has_warned = False
        stderr_has_warned = False

        while not (self.__end_interactive_event.is_set() or (stdout_has_warned and stderr_has_warned)):
            # We can afford to treat stdout and stderr sequentially. This approach should also prevent
            # messing up the order of the information printed by the child process.
            # To avoid starvation, we switch between pipes upon receiving a bunch of data from one of them.
            if self._stdout_is_open:
                while True:
                    new_recv = self._raw_recv()
                    payload = self.__stdout_buffer.get_data()

                    if not (new_recv or payload):
                        # No more data available in the stdout pipe at the moment
                        break

                    sys.stdout.write(payload)
                    self.__stdout_buffer.clear()
            elif not stdout_has_warned:
                # The child process has closed the stdout pipe and we have to print the warning message
                liblog.warning("The stdout pipe of the child process is not available anymore")
                stdout_has_warned = True
            if self._stderr_is_open:
                while True:
                    new_recv = self._raw_recv(stderr=True)
                    payload = self.__stderr_buffer.get_data()

                    if not (new_recv or payload):
                        # No more data available in the stderr pipe at the moment
                        break

                    sys.stderr.write(payload)
                    self.__stderr_buffer.clear()
            elif not stderr_has_warned:
                # The child process has closed the stderr pipe
                liblog.warning("The stderr pipe of the child process is not available anymore")
                stderr_has_warned = True

    def interactive(self: PipeManager, prompt: str = prompt_default, auto_quit: bool = False) -> None:
        """Manually interact with the child process.

        Args:
            prompt (str, optional): prompt for the interactive mode. Defaults to "$ " (prompt_default).
            auto_quit (bool, optional): whether to automatically quit the interactive mode when the child process is not running. Defaults to False.
        """
        liblog.info("Calling interactive mode")

        # Set up and run the terminal
        with extend_internal_debugger(self):
            libterminal = LibTerminal(prompt, self.sendline, self.__end_interactive_event, auto_quit)

        # Receive data from the child process's stdout and stderr pipes
        self._recv_for_interactive()

        # Be sure that the interactive mode has ended
        # If the the stderr and stdout pipes are closed, the interactive mode will continue until the user manually
        # stops it
        self.__end_interactive_event.wait()

        # Unset the interactive mode event
        self.__end_interactive_event.clear()

        # Reset the terminal
        libterminal.reset()

        liblog.info("Exiting interactive mode")

__init__(stdin_write, stdout_read, stderr_read)

Initializes the PipeManager class.

Parameters:

Name Type Description Default
stdin_write int

file descriptor for stdin write.

required
stdout_read int

file descriptor for stdout read.

required
stderr_read int

file descriptor for stderr read.

required
Source code in libdebug/commlink/pipe_manager.py
def __init__(self: PipeManager, stdin_write: int, stdout_read: int, stderr_read: int) -> None:
    """Initializes the PipeManager class.

    Args:
        stdin_write (int): file descriptor for stdin write.
        stdout_read (int): file descriptor for stdout read.
        stderr_read (int): file descriptor for stderr read.
    """
    self._stdin_write: int = stdin_write
    self._stdout_read: int = stdout_read
    self._stderr_read: int = stderr_read
    self._stderr_is_open: bool = True
    self._stdout_is_open: bool = True
    self._internal_debugger: InternalDebugger = provide_internal_debugger(self)

    self.__stdout_buffer: BufferData = BufferData(b"")
    self.__stderr_buffer: BufferData = BufferData(b"")

    self.__end_interactive_event: Event = Event()

close()

Closes all the pipes of the child process.

Source code in libdebug/commlink/pipe_manager.py
def close(self: PipeManager) -> None:
    """Closes all the pipes of the child process."""
    os.close(self._stdin_write)
    os.close(self._stdout_read)
    os.close(self._stderr_read)

interactive(prompt=prompt_default, auto_quit=False)

Manually interact with the child process.

Parameters:

Name Type Description Default
prompt str

prompt for the interactive mode. Defaults to "$ " (prompt_default).

prompt_default
auto_quit bool

whether to automatically quit the interactive mode when the child process is not running. Defaults to False.

False
Source code in libdebug/commlink/pipe_manager.py
def interactive(self: PipeManager, prompt: str = prompt_default, auto_quit: bool = False) -> None:
    """Manually interact with the child process.

    Args:
        prompt (str, optional): prompt for the interactive mode. Defaults to "$ " (prompt_default).
        auto_quit (bool, optional): whether to automatically quit the interactive mode when the child process is not running. Defaults to False.
    """
    liblog.info("Calling interactive mode")

    # Set up and run the terminal
    with extend_internal_debugger(self):
        libterminal = LibTerminal(prompt, self.sendline, self.__end_interactive_event, auto_quit)

    # Receive data from the child process's stdout and stderr pipes
    self._recv_for_interactive()

    # Be sure that the interactive mode has ended
    # If the the stderr and stdout pipes are closed, the interactive mode will continue until the user manually
    # stops it
    self.__end_interactive_event.wait()

    # Unset the interactive mode event
    self.__end_interactive_event.clear()

    # Reset the terminal
    libterminal.reset()

    liblog.info("Exiting interactive mode")

recv(numb=4096, timeout=timeout_default)

Receives at most numb bytes from the child process stdout.

Parameters:

Name Type Description Default
numb int

number of bytes to receive. Defaults to 4096.

4096
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default

Returns:

Name Type Description
bytes bytes

received bytes from the child process stdout.

Source code in libdebug/commlink/pipe_manager.py
def recv(
    self: PipeManager,
    numb: int = 4096,
    timeout: int = timeout_default,
) -> bytes:
    """Receives at most numb bytes from the child process stdout.

    Args:
        numb (int, optional): number of bytes to receive. Defaults to 4096.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.

    Returns:
        bytes: received bytes from the child process stdout.
    """
    return self._buffered_recv(numb=numb, timeout=timeout, stderr=False)

recverr(numb=4096, timeout=timeout_default)

Receives at most numb bytes from the child process stderr.

Parameters:

Name Type Description Default
numb int

number of bytes to receive. Defaults to 4096.

4096
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default

Returns:

Name Type Description
bytes bytes

received bytes from the child process stderr.

Source code in libdebug/commlink/pipe_manager.py
def recverr(
    self: PipeManager,
    numb: int = 4096,
    timeout: int = timeout_default,
) -> bytes:
    """Receives at most numb bytes from the child process stderr.

    Args:
        numb (int, optional): number of bytes to receive. Defaults to 4096.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.

    Returns:
        bytes: received bytes from the child process stderr.
    """
    return self._buffered_recv(numb=numb, timeout=timeout, stderr=True)

recverrline(numlines=1, drop=True, timeout=timeout_default, optional=False)

Receives numlines lines from the child process stderr.

Parameters:

Name Type Description Default
numlines int

number of lines to receive. Defaults to 1.

1
drop bool

drop the line ending. Defaults to True.

True
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received lines from the child process stdout.

Source code in libdebug/commlink/pipe_manager.py
def recverrline(
    self: PipeManager,
    numlines: int = 1,
    drop: bool = True,
    timeout: int = timeout_default,
    optional: bool = False,
) -> bytes:
    """Receives numlines lines from the child process stderr.

    Args:
        numlines (int, optional): number of lines to receive. Defaults to 1.
        drop (bool, optional): drop the line ending. Defaults to True.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received lines from the child process stdout.
    """
    return self.recverruntil(delims=b"\n", occurences=numlines, drop=drop, timeout=timeout, optional=optional)

recverruntil(delims, occurences=1, drop=False, timeout=timeout_default, optional=False)

Receives data from the child process stderr until the delimiters are found.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stderr.

Source code in libdebug/commlink/pipe_manager.py
def recverruntil(
    self: PipeManager,
    delims: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> bytes:
    """Receives data from the child process stderr until the delimiters are found.

    Args:
        delims (bytes): delimiters where to stop.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stderr.
    """
    return self._recvuntil(
        delims=delims,
        occurences=occurences,
        drop=drop,
        timeout=timeout,
        stderr=True,
        optional=optional,
    )

recvline(numlines=1, drop=True, timeout=timeout_default, optional=False)

Receives numlines lines from the child process stdout.

Parameters:

Name Type Description Default
numlines int

number of lines to receive. Defaults to 1.

1
drop bool

drop the line ending. Defaults to True.

True
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received lines from the child process stdout.

Source code in libdebug/commlink/pipe_manager.py
def recvline(
    self: PipeManager,
    numlines: int = 1,
    drop: bool = True,
    timeout: int = timeout_default,
    optional: bool = False,
) -> bytes:
    """Receives numlines lines from the child process stdout.

    Args:
        numlines (int, optional): number of lines to receive. Defaults to 1.
        drop (bool, optional): drop the line ending. Defaults to True.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received lines from the child process stdout.
    """
    return self.recvuntil(delims=b"\n", occurences=numlines, drop=drop, timeout=timeout, optional=optional)

recvuntil(delims, occurences=1, drop=False, timeout=timeout_default, optional=False)

Receives data from the child process stdout until the delimiters are found.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stdout.

Source code in libdebug/commlink/pipe_manager.py
def recvuntil(
    self: PipeManager,
    delims: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> bytes:
    """Receives data from the child process stdout until the delimiters are found.

    Args:
        delims (bytes): delimiters where to stop.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stdout.
    """
    return self._recvuntil(
        delims=delims,
        occurences=occurences,
        drop=drop,
        timeout=timeout,
        stderr=False,
        optional=optional,
    )

send(data)

Sends data to the child process stdin.

Parameters:

Name Type Description Default
data bytes

data to send.

required

Returns:

Name Type Description
int int

number of bytes sent.

Raises:

Type Description
RuntimeError

no stdin pipe of the child process.

Source code in libdebug/commlink/pipe_manager.py
def send(self: PipeManager, data: bytes) -> int:
    """Sends data to the child process stdin.

    Args:
        data (bytes): data to send.

    Returns:
        int: number of bytes sent.

    Raises:
        RuntimeError: no stdin pipe of the child process.
    """
    if not self._stdin_write:
        raise RuntimeError("No stdin pipe of the child process")

    liblog.pipe(f"Sending {len(data)} bytes to the child process: {data!r}")

    if isinstance(data, str):
        liblog.warning("The input data is a string, converting to bytes")
        data = data.encode()

    try:
        number_bytes = os.write(self._stdin_write, data)
    except OSError as e:
        raise RuntimeError("Broken pipe. Is the child process still running?") from e

    return number_bytes

sendafter(delims, data, occurences=1, drop=False, timeout=timeout_default, optional=False)

Sends data to the child process stdin after the delimiters are found in the stdout.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
data bytes

data to send.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stdout.

int int

number of bytes sent.

Source code in libdebug/commlink/pipe_manager.py
def sendafter(
    self: PipeManager,
    delims: bytes,
    data: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> tuple[bytes, int]:
    """Sends data to the child process stdin after the delimiters are found in the stdout.

    Args:
        delims (bytes): delimiters where to stop.
        data (bytes): data to send.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stdout.
        int: number of bytes sent.
    """
    received = self.recvuntil(delims=delims, occurences=occurences, drop=drop, timeout=timeout, optional=optional)
    sent = self.send(data)
    return (received, sent)

sendaftererr(delims, data, occurences=1, drop=False, timeout=timeout_default, optional=False)

Sends data to the child process stdin after the delimiters are found in stderr.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
data bytes

data to send.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stderr.

int int

number of bytes sent.

Source code in libdebug/commlink/pipe_manager.py
def sendaftererr(
    self: PipeManager,
    delims: bytes,
    data: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> tuple[bytes, int]:
    """Sends data to the child process stdin after the delimiters are found in stderr.

    Args:
        delims (bytes): delimiters where to stop.
        data (bytes): data to send.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stderr.
        int: number of bytes sent.
    """
    received = self.recverruntil(
        delims=delims,
        occurences=occurences,
        drop=drop,
        timeout=timeout,
        optional=optional,
    )
    sent = self.send(data)
    return (received, sent)

sendline(data)

Sends data to the child process stdin and append a newline.

Parameters:

Name Type Description Default
data bytes

data to send.

required

Returns:

Name Type Description
int int

number of bytes sent.

Source code in libdebug/commlink/pipe_manager.py
def sendline(self: PipeManager, data: bytes) -> int:
    """Sends data to the child process stdin and append a newline.

    Args:
        data (bytes): data to send.

    Returns:
        int: number of bytes sent.
    """
    if isinstance(data, str):
        liblog.warning("The input data is a string, converting to bytes")
        data = data.encode()
    return self.send(data=data + b"\n")

sendlineafter(delims, data, occurences=1, drop=False, timeout=timeout_default, optional=False)

Sends line to the child process stdin after the delimiters are found in the stdout.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
data bytes

data to send.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stdout.

int int

number of bytes sent.

Source code in libdebug/commlink/pipe_manager.py
def sendlineafter(
    self: PipeManager,
    delims: bytes,
    data: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> tuple[bytes, int]:
    """Sends line to the child process stdin after the delimiters are found in the stdout.

    Args:
        delims (bytes): delimiters where to stop.
        data (bytes): data to send.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stdout.
        int: number of bytes sent.
    """
    received = self.recvuntil(delims=delims, occurences=occurences, drop=drop, timeout=timeout, optional=optional)
    sent = self.sendline(data)
    return (received, sent)

sendlineaftererr(delims, data, occurences=1, drop=False, timeout=timeout_default, optional=False)

Sends line to the child process stdin after the delimiters are found in the stderr.

Parameters:

Name Type Description Default
delims bytes

delimiters where to stop.

required
data bytes

data to send.

required
occurences int

number of delimiters to find. Defaults to 1.

1
drop bool

drop the delimiter. Defaults to False.

False
timeout int

timeout in seconds. Defaults to timeout_default.

timeout_default
optional bool

whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

False

Returns:

Name Type Description
bytes bytes

received data from the child process stderr.

int int

number of bytes sent.

Source code in libdebug/commlink/pipe_manager.py
def sendlineaftererr(
    self: PipeManager,
    delims: bytes,
    data: bytes,
    occurences: int = 1,
    drop: bool = False,
    timeout: int = timeout_default,
    optional: bool = False,
) -> tuple[bytes, int]:
    """Sends line to the child process stdin after the delimiters are found in the stderr.

    Args:
        delims (bytes): delimiters where to stop.
        data (bytes): data to send.
        occurences (int, optional): number of delimiters to find. Defaults to 1.
        drop (bool, optional): drop the delimiter. Defaults to False.
        timeout (int, optional): timeout in seconds. Defaults to timeout_default.
        optional (bool, optional): whether to ignore the wait for the received input if the command is executed when the process is stopped. Defaults to False.

    Returns:
        bytes: received data from the child process stderr.
        int: number of bytes sent.
    """
    received = self.recverruntil(
        delims=delims,
        occurences=occurences,
        drop=drop,
        timeout=timeout,
        optional=optional,
    )
    sent = self.sendline(data)
    return (received, sent)