forked from Imagelibrary/binutils-gdb
[gdb/dap] Make dap log printing thread-safe
I read that printing from different python threads is thread-unsafe, and I noticed that the dap log printing is used from different threads, but doesn't take care to make the printing thread-safe. Fix this by using a lock to access LoggingParam.log_file. Tested on aarch64-linux. Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
@@ -146,6 +146,7 @@ class LoggingParam(gdb.Parameter):
|
|||||||
set_doc = "Set the DAP logging status."
|
set_doc = "Set the DAP logging status."
|
||||||
show_doc = "Show the DAP logging status."
|
show_doc = "Show the DAP logging status."
|
||||||
|
|
||||||
|
lock = threading.Lock()
|
||||||
log_file = None
|
log_file = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -155,12 +156,13 @@ class LoggingParam(gdb.Parameter):
|
|||||||
self.value = None
|
self.value = None
|
||||||
|
|
||||||
def get_set_string(self):
|
def get_set_string(self):
|
||||||
# Close any existing log file, no matter what.
|
with dap_log.lock:
|
||||||
if self.log_file is not None:
|
# Close any existing log file, no matter what.
|
||||||
self.log_file.close()
|
if self.log_file is not None:
|
||||||
self.log_file = None
|
self.log_file.close()
|
||||||
if self.value is not None:
|
self.log_file = None
|
||||||
self.log_file = open(self.value, "w")
|
if self.value is not None:
|
||||||
|
self.log_file = open(self.value, "w")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@@ -169,16 +171,18 @@ dap_log = LoggingParam()
|
|||||||
|
|
||||||
def log(something, level=LogLevel.DEFAULT):
|
def log(something, level=LogLevel.DEFAULT):
|
||||||
"""Log SOMETHING to the log file, if logging is enabled."""
|
"""Log SOMETHING to the log file, if logging is enabled."""
|
||||||
if dap_log.log_file is not None and level <= _log_level.value:
|
with dap_log.lock:
|
||||||
print(something, file=dap_log.log_file)
|
if dap_log.log_file is not None and level <= _log_level.value:
|
||||||
dap_log.log_file.flush()
|
print(something, file=dap_log.log_file)
|
||||||
|
dap_log.log_file.flush()
|
||||||
|
|
||||||
|
|
||||||
def log_stack(level=LogLevel.DEFAULT):
|
def log_stack(level=LogLevel.DEFAULT):
|
||||||
"""Log a stack trace to the log file, if logging is enabled."""
|
"""Log a stack trace to the log file, if logging is enabled."""
|
||||||
if dap_log.log_file is not None and level <= _log_level.value:
|
with dap_log.lock:
|
||||||
traceback.print_exc(file=dap_log.log_file)
|
if dap_log.log_file is not None and level <= _log_level.value:
|
||||||
dap_log.log_file.flush()
|
traceback.print_exc(file=dap_log.log_file)
|
||||||
|
dap_log.log_file.flush()
|
||||||
|
|
||||||
|
|
||||||
@in_gdb_thread
|
@in_gdb_thread
|
||||||
|
|||||||
Reference in New Issue
Block a user