dod-cyber-crime-center / pyhidra

Pyhidra is a Python library that provides direct access to the Ghidra API within a native CPython interpreter using jpype.
Other
176 stars 15 forks source link

Logging error: [Errno 9] Bad file descriptor #21

Closed gipi closed 8 months ago

gipi commented 1 year ago

Using the logging module inside pyhidra generates exceptions: with a simple test script like the following (named logme.py)

import logging

Log_Format = "%(levelname)s %(asctime)s - %(message)s"

logging.basicConfig(filename="/tmp/ghidra_abstract.log", filemode="a", format=Log_Format, level=logging.DEBUG)
logger = logging.getLogger(__name__)

def something():
    logger.info("miao")

and calling something() inside the ghidra's pyhidra window

>>> import logme
>>> logme.something()
--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.10/logging/__init__.py", line 1103, in emit
    stream.write(msg + self.terminator)
OSError: [Errno 9] Bad file descriptor
Call stack:
  File "/usr/lib/python3.10/threading.py", line 973, in _bootstrap
    self._bootstrap_inner()
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.10/code.py", line 232, in interact
    more = self.push(line)
  File "/usr/lib/python3.10/code.py", line 258, in push
    more = self.runsource(source, self.filename)
  File "/usr/lib/python3.10/code.py", line 74, in runsource
    self.runcode(code)
  File "/opt/symbolic/.venv/lib/python3.10/site-packages/pyhidra/java/plugin/plugin.py", line 163, in runcode
    super().runcode(code)
  File "/usr/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/opt/symbolic/logme.py", line 10, in something
    logger.info("miao")
Message: 'miao'
Arguments: ()

where in a normal execution inside a normal python intepreter you have

In [1]: import logme

In [2]: logme.something()

In [3]:

and then

$ cat /tmp/ghidra_abstract.log 
INFO 2022-12-24 12:51:37,032 - miao

This is probably caused by a closed stdout: still inside pyhidra terminal you can generate the same error

>>> os.write(1, b"miao")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
dc3-tsd commented 1 year ago

Thanks for letting us know about this issue. For the time being when you use logging.basicConfig in your script setting force=True should allow working around this issue.