Add gdb_setup to target for arbitrary gdb commands
[riscv-tests.git] / debug / targets.py
1 import importlib
2 import os.path
3 import sys
4 import tempfile
5
6 import testlib
7
8 class Target(object):
9 # pylint: disable=too-many-instance-attributes
10
11 # Name of the target. Defaults to the name of the class.
12 name = None
13
14 # XLEN of the target. May be overridden with --32 or --64 command line
15 # options.
16 xlen = 0
17
18 # GDB remotetimeout setting.
19 timeout_sec = 2
20
21 # Path to OpenOCD configuration file relative to the .py file where the
22 # target is defined. Defaults to <name>.cfg.
23 openocd_config_path = None
24
25 # Path to linker script relative to the .py file where the target is
26 # defined. Defaults to <name>.lds.
27 link_script_path = None
28
29 # Will be autodetected (by running ExamineTarget) if left unset. Set to
30 # save a little time.
31 misa = None
32
33 # List of commands that should be executed in gdb after connecting but
34 # before starting the test.
35 gdb_setup = []
36
37 # Internal variables:
38 directory = None
39 temporary_files = []
40 temporary_binary = None
41
42 def __init__(self, path, parsed):
43 # Path to module.
44 self.path = path
45 self.directory = os.path.dirname(path)
46 self.server_cmd = parsed.server_cmd
47 self.sim_cmd = parsed.sim_cmd
48 self.isolate = parsed.isolate
49 if not self.name:
50 self.name = type(self).__name__
51 # Default OpenOCD config file to <name>.cfg
52 if not self.openocd_config_path:
53 self.openocd_config_path = "%s.cfg" % self.name
54 self.openocd_config_path = os.path.join(self.directory,
55 self.openocd_config_path)
56 # Default link script to <name>.lds
57 if not self.link_script_path:
58 self.link_script_path = "%s.lds" % self.name
59 self.link_script_path = os.path.join(self.directory,
60 self.link_script_path)
61
62 def create(self):
63 """Create the target out of thin air, eg. start a simulator."""
64 pass
65
66 def server(self):
67 """Start the debug server that gdb connects to, eg. OpenOCD."""
68 return testlib.Openocd(server_cmd=self.server_cmd,
69 config=self.openocd_config_path)
70
71 def compile(self, *sources):
72 binary_name = "%s_%s-%d" % (
73 self.name,
74 os.path.basename(os.path.splitext(sources[0])[0]),
75 self.xlen)
76 if self.isolate:
77 self.temporary_binary = tempfile.NamedTemporaryFile(
78 prefix=binary_name + "_")
79 binary_name = self.temporary_binary.name
80 Target.temporary_files.append(self.temporary_binary)
81 march = "rv%dima" % self.xlen
82 for letter in "fdc":
83 if self.extensionSupported(letter):
84 march += letter
85 testlib.compile(sources +
86 ("programs/entry.S", "programs/init.c",
87 "-I", "../env",
88 "-march=%s" % march,
89 "-T", self.link_script_path,
90 "-nostartfiles",
91 "-mcmodel=medany",
92 "-DXLEN=%d" % self.xlen,
93 "-o", binary_name),
94 xlen=self.xlen)
95 return binary_name
96
97 def extensionSupported(self, letter):
98 # target.misa is set by testlib.ExamineTarget
99 if self.misa:
100 return self.misa & (1 << (ord(letter.upper()) - ord('A')))
101 else:
102 return False
103
104 def add_target_options(parser):
105 parser.add_argument("target", help=".py file that contains definition for "
106 "the target to test with.")
107 parser.add_argument("--sim_cmd",
108 help="The command to use to start the actual target (e.g. "
109 "simulation)", default="spike")
110 parser.add_argument("--server_cmd",
111 help="The command to use to start the debug server (e.g. OpenOCD)")
112
113 xlen_group = parser.add_mutually_exclusive_group()
114 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
115 help="Force the target to be 32-bit.")
116 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
117 help="Force the target to be 64-bit.")
118
119 parser.add_argument("--isolate", action="store_true",
120 help="Try to run in such a way that multiple instances can run at "
121 "the same time. This may make it harder to debug a failure if it "
122 "does occur.")
123
124 def target(parsed):
125 directory = os.path.dirname(parsed.target)
126 filename = os.path.basename(parsed.target)
127 module_name = os.path.splitext(filename)[0]
128
129 sys.path.append(directory)
130 module = importlib.import_module(module_name)
131 found = []
132 for name in dir(module):
133 definition = getattr(module, name)
134 if type(definition) == type and issubclass(definition, Target):
135 found.append(definition)
136 assert len(found) == 1, "%s does not define exactly one subclass of " \
137 "targets.Target" % parsed.target
138
139 return found[0](parsed.target, parsed)