Skip to content

Commit

Permalink
Add debug feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr-yudai committed Oct 17, 2023
1 parent 6368f32 commit 30526b9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions ptrlib/binary/encoding/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .bitconv import *
from .byteconv import *
from .dump import *
from .locale import *
from .table import *
30 changes: 30 additions & 0 deletions ptrlib/binary/encoding/dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from .byteconv import str2bytes

def hexdump(data, prefix='', postfix=''):
if isinstance(data, str):
data = str2bytes(data)
prev_display = True
for offset in range(0, len(data), 0x10):
output = prefix
output += f"{offset:08x} "
for i, c in enumerate(data[offset:offset+0x10]):
if i == 8: output += " "
output += f"{c:02x} "
if len(data[offset:]) < 0x10:
output += " " * (0x10 - len(data[offset:]))
if len(data[offset:]) < 9:
output += " "
output += " |"
for c in data[offset:offset+0x10]:
if 0x20 <= c <= 0x7e:
output += chr(c)
else:
output += "."
output += postfix
if offset > 0x10 and data[offset-0x10:offset] == data[offset:offset+0x10]:
if prev_display:
print(prefix + "*" + postfix)
prev_display = False
else:
print(output)
prev_display = True
2 changes: 1 addition & 1 deletion ptrlib/connection/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> by
self._poll() # poll after received all data
return data

def send(self, data: Union[str, bytes]):
def _send(self, data: Union[str, bytes]):
"""Send raw data
Send raw data through the socket
Expand Down
2 changes: 1 addition & 1 deletion ptrlib/connection/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> by

return data

def send(self, data: Union[str, bytes]):
def _send(self, data: Union[str, bytes]):
"""Send raw data
Send raw data through the socket
Expand Down
12 changes: 11 additions & 1 deletion ptrlib/connection/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class Tube(metaclass=ABCMeta):
def __init__(self):
self.buf = b''
self.debug = False

@abstractmethod
def _settimeout(self, timeout: Optional[Union[int, float]]):
Expand Down Expand Up @@ -70,6 +71,9 @@ def recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None) -> byt
self._settimeout(timeout)
data = self._recv(size, timeout=-1)
self.buf += data
if self.debug:
logger.info(f"Received {hex(len(data))} ({len(data)}) bytes:")
hexdump(data, prefix=" " + Color.CYAN, postfix=Color.END)

# We don't check size > len(self.buf) because Python handles it
data, self.buf = self.buf[:size], self.buf[size:]
Expand Down Expand Up @@ -236,9 +240,15 @@ def recvregex(self,
return group, data[:pos]

@abstractmethod
def send(self, data: bytes):
def _send(self, data: bytes):
pass

def send(self, data: bytes):
self._send(data)
if self.debug:
logger.info(f"Sent {hex(len(data))} ({len(data)}) bytes:")
hexdump(data, prefix=Color.YELLOW, postfix=Color.END)

@abstractmethod
def _socket(self) -> Optional[Any]:
pass
Expand Down
2 changes: 1 addition & 1 deletion ptrlib/connection/winproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def recv(self, size: int=4096, timeout: Optional[Union[int, float]]=None):

return self._recv(min(self.size, size))

def send(self, data: bytes):
def _send(self, data: bytes):
"""Send raw data
Send raw data through the socket
Expand Down
2 changes: 1 addition & 1 deletion ptrlib/console/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Color:
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
INVISIBLE = '\033[08m'
REVERCE = '\033[07m'
REVERSE = '\033[07m'

class ColoredFormatter(Formatter):
def format(self, record: LogRecord):
Expand Down

0 comments on commit 30526b9

Please sign in to comment.