Skip to content

Commit

Permalink
Merge pull request #70 from doronz88/feature/stdout-stderr
Browse files Browse the repository at this point in the history
configs: add `enable_stdout_stderr`
  • Loading branch information
doronz88 authored Jul 15, 2024
2 parents 17cf2a2 + ee64412 commit f2b1bb2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hilda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def attach(name: Optional[str], pid: Optional[int], startup_files: List[str]) ->
if name is not None:
hilda_client = create_hilda_client_using_attach_by_name(name)
elif pid is not None:
hilda_client = create_hilda_client_using_attach_by_pid(name)
hilda_client = create_hilda_client_using_attach_by_pid(pid)
else:
raise click.UsageError('You must specify a process name or pid')
hilda_client.interact(startup_files=startup_files)
Expand Down
9 changes: 8 additions & 1 deletion hilda/hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
<b>Hilda has been successfully loaded! 😎
Usage:
<span style="color: magenta">p</span> Global to access all features.
<span style="color: magenta">F1</span> UI Show.
<span style="color: magenta">F1</span> Show UI.
<span style="color: magenta">F2</span> Toggle enabling of stdout & stderr.
<span style="color: magenta">F7</span> Step Into.
<span style="color: magenta">F8</span> Step Over.
<span style="color: magenta">F9</span> Continue.
Expand Down Expand Up @@ -92,6 +93,8 @@ class Configs:
'doc': 'Whether to exclude NSObject during evaluation - reduce ipython autocomplete results.'})
objc_verbose_monitor: bool = field(default=False, metadata={
'doc': 'When set to True, using monitor() will automatically print objc methods arguments.'})
enable_stdout_stderr: bool = field(default=True, metadata={
'doc': 'When set to True, will enable process stdout and stderr.'})

def __repr__(self):
return self.__str__()
Expand Down Expand Up @@ -1089,6 +1092,10 @@ def interact(self, additional_namespace: Optional[typing.Mapping] = None,
sys.argv = ['a']
IPython.start_ipython(config=ipython_config, user_ns=namespace)

def toggle_enable_stdout_stderr(self, *args) -> None:
self.configs.enable_stdout_stderr = not self.configs.enable_stdout_stderr
self.logger.info(f'Changed stdout and stderr status to: {self.configs.enable_stdout_stderr}')

def __enter__(self) -> 'HildaClient':
return self

Expand Down
1 change: 1 addition & 0 deletions hilda/ipython_extensions/keybindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def load_ipython_extension(ipython):
def register_keybindings():
hilda = ipython.user_ns['p']
keys_mapping = {Keys.F1: hilda.ui_manager.show,
Keys.F2: hilda.toggle_enable_stdout_stderr,
Keys.F7: hilda.step_into,
Keys.F8: hilda.step_over,
Keys.F9: lambda _: (hilda.log_info('Sending continue'), hilda.cont()),
Expand Down
22 changes: 22 additions & 0 deletions hilda/launch_lldb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import sys
from abc import ABC, abstractmethod
from threading import Thread
from typing import List, Optional
Expand Down Expand Up @@ -46,6 +47,20 @@ def _check_success(self) -> None:
return
raise LLDBError(self.error.description)

def _process_stdout(self) -> None:
stdout = self.process.GetSTDOUT(1024)
while stdout:
if lldb.hilda_client is not None and lldb.hilda_client.configs.enable_stdout_stderr:
sys.stdout.write(stdout)
stdout = self.process.GetSTDOUT(1024)

def _process_stderr(self) -> None:
stderr = self.process.GetSTDERR(1024)
while stderr:
if lldb.hilda_client is not None and lldb.hilda_client.configs.enable_stdout_stderr:
sys.stderr.write(stderr)
stderr = self.process.GetSTDERR(1024)

def run(self):
event = lldb.SBEvent()
last_state = lldb.eStateStopped
Expand All @@ -54,6 +69,13 @@ def run(self):
continue
if not lldb.SBProcess.EventIsProcessEvent(event):
continue

event_type = event.GetType()
if event_type & lldb.SBProcess.eBroadcastBitSTDOUT:
self._process_stdout()
if event_type & lldb.SBProcess.eBroadcastBitSTDERR:
self._process_stderr()

state = self.process.GetStateFromEvent(event)
if state == lldb.eStateDetached:
logger.debug('Process Detached')
Expand Down

0 comments on commit f2b1bb2

Please sign in to comment.