aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
authorTom de Vries <tdevries@suse.de>2024-02-21 10:46:08 +0100
committerTom de Vries <tdevries@suse.de>2024-02-21 10:46:08 +0100
commitd61933410868ff2dd52a896520a2b620d35f3615 (patch)
treef4af087717b49eb60a913be55f189f8d53ce6fee /gdb/python
parent84a227694d7b97bb4d09786dcfcc3d7c0ca16d4f (diff)
downloadgdb-d61933410868ff2dd52a896520a2b620d35f3615.zip
gdb-d61933410868ff2dd52a896520a2b620d35f3615.tar.gz
gdb-d61933410868ff2dd52a896520a2b620d35f3615.tar.bz2
[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>
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/lib/gdb/dap/startup.py28
1 files changed, 16 insertions, 12 deletions
diff --git a/gdb/python/lib/gdb/dap/startup.py b/gdb/python/lib/gdb/dap/startup.py
index f20c541..6049121 100644
--- a/gdb/python/lib/gdb/dap/startup.py
+++ b/gdb/python/lib/gdb/dap/startup.py
@@ -146,6 +146,7 @@ class LoggingParam(gdb.Parameter):
set_doc = "Set the DAP logging status."
show_doc = "Show the DAP logging status."
+ lock = threading.Lock()
log_file = None
def __init__(self):
@@ -155,12 +156,13 @@ class LoggingParam(gdb.Parameter):
self.value = None
def get_set_string(self):
- # Close any existing log file, no matter what.
- if self.log_file is not None:
- self.log_file.close()
- self.log_file = None
- if self.value is not None:
- self.log_file = open(self.value, "w")
+ with dap_log.lock:
+ # Close any existing log file, no matter what.
+ if self.log_file is not None:
+ self.log_file.close()
+ self.log_file = None
+ if self.value is not None:
+ self.log_file = open(self.value, "w")
return ""
@@ -169,16 +171,18 @@ dap_log = LoggingParam()
def log(something, level=LogLevel.DEFAULT):
"""Log SOMETHING to the log file, if logging is enabled."""
- if dap_log.log_file is not None and level <= _log_level.value:
- print(something, file=dap_log.log_file)
- dap_log.log_file.flush()
+ with dap_log.lock:
+ if dap_log.log_file is not None and level <= _log_level.value:
+ print(something, file=dap_log.log_file)
+ dap_log.log_file.flush()
def log_stack(level=LogLevel.DEFAULT):
"""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:
- traceback.print_exc(file=dap_log.log_file)
- dap_log.log_file.flush()
+ with dap_log.lock:
+ if dap_log.log_file is not None and level <= _log_level.value:
+ traceback.print_exc(file=dap_log.log_file)
+ dap_log.log_file.flush()
@in_gdb_thread