1
1
import os
2
2
import re
3
3
import shlex
4
+ import shutil
4
5
import subprocess
5
6
import sys
6
7
import sysconfig
7
8
import unittest
8
9
from test import support
9
10
10
11
12
+ GDB_PROGRAM = shutil .which ('gdb' ) or 'gdb'
13
+
11
14
# Location of custom hooks file in a repository checkout.
12
15
CHECKOUT_HOOK_PATH = os .path .join (os .path .dirname (sys .executable ),
13
16
'python-gdb.py' )
@@ -27,7 +30,7 @@ def clean_environment():
27
30
# Temporary value until it's initialized by get_gdb_version() below
28
31
GDB_VERSION = (0 , 0 )
29
32
30
- def run_gdb (* args , exitcode = 0 , ** env_vars ):
33
+ def run_gdb (* args , exitcode = 0 , check = True , ** env_vars ):
31
34
"""Runs gdb in --batch mode with the additional arguments given by *args.
32
35
33
36
Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
@@ -36,7 +39,7 @@ def run_gdb(*args, exitcode=0, **env_vars):
36
39
if env_vars :
37
40
env .update (env_vars )
38
41
39
- cmd = ['gdb' ,
42
+ cmd = [GDB_PROGRAM ,
40
43
# Batch mode: Exit after processing all the command files
41
44
# specified with -x/--command
42
45
'--batch' ,
@@ -59,7 +62,7 @@ def run_gdb(*args, exitcode=0, **env_vars):
59
62
60
63
stdout = proc .stdout
61
64
stderr = proc .stderr
62
- if proc .returncode != exitcode :
65
+ if check and proc .returncode != exitcode :
63
66
cmd_text = shlex .join (cmd )
64
67
raise Exception (f"{ cmd_text } failed with exit code { proc .returncode } , "
65
68
f"expected exit code { exitcode } :\n "
@@ -72,10 +75,10 @@ def run_gdb(*args, exitcode=0, **env_vars):
72
75
def get_gdb_version ():
73
76
try :
74
77
stdout , stderr = run_gdb ('--version' )
75
- except OSError :
78
+ except OSError as exc :
76
79
# This is what "no gdb" looks like. There may, however, be other
77
80
# errors that manifest this way too.
78
- raise unittest .SkipTest ("Couldn't find gdb program on the path" )
81
+ raise unittest .SkipTest (f "Couldn't find gdb program on the path: { exc } " )
79
82
80
83
# Regex to parse:
81
84
# 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7
@@ -106,7 +109,8 @@ def check_usable_gdb():
106
109
# disallow this without a customized .gdbinit.
107
110
stdout , stderr = run_gdb (
108
111
'--eval-command=python import sys; print(sys.version_info)' ,
109
- '--args' , sys .executable )
112
+ '--args' , sys .executable ,
113
+ check = False )
110
114
111
115
if "auto-loading has been declined" in stderr :
112
116
raise unittest .SkipTest (
@@ -144,6 +148,7 @@ def setup_module():
144
148
print (f"gdb version { GDB_VERSION [0 ]} .{ GDB_VERSION [1 ]} :" )
145
149
for line in GDB_VERSION_TEXT .splitlines ():
146
150
print (" " * 4 + line )
151
+ print (f" path: { GDB_PROGRAM } " )
147
152
print ()
148
153
149
154
0 commit comments