Make the debug tests aware of multicore.
[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 Hart(object):
9 # XLEN of the hart. May be overridden with --32 or --64 command line
10 # options.
11 xlen = 0
12
13 # Will be autodetected (by running ExamineTarget) if left unset. Set to
14 # save a little time.
15 misa = None
16
17 # Path to linker script relative to the .py file where the target is
18 # defined. Defaults to <name>.lds.
19 link_script_path = None
20
21 # Implements dmode in tdata1 as described in the spec. Harts that need
22 # this value set to False are not compliant with the spec (but still usable
23 # as long as running code doesn't try to mess with triggers set by an
24 # external debugger).
25 honors_tdata1_hmode = True
26
27 # Address where a r/w/x block of RAM starts, together with its size.
28 ram = None
29 ram_size = None
30
31 # Number of instruction triggers the hart supports.
32 instruction_hardware_breakpoint_count = 0
33
34 # Defaults to target-<index>
35 name = None
36
37 def __init__(self):
38 self.temporary_binary = None
39
40 def compile(self, *sources):
41 binary_name = "%s_%s-%d" % (
42 self.name,
43 os.path.basename(os.path.splitext(sources[0])[0]),
44 self.xlen)
45 if Target.isolate:
46 self.temporary_binary = tempfile.NamedTemporaryFile(
47 prefix=binary_name + "_")
48 binary_name = self.temporary_binary.name
49 Target.temporary_files.append(self.temporary_binary)
50 march = "rv%dima" % self.xlen
51 for letter in "fdc":
52 if self.extensionSupported(letter):
53 march += letter
54 testlib.compile(sources +
55 ("programs/entry.S", "programs/init.c",
56 "-I", "../env",
57 "-march=%s" % march,
58 "-T", self.link_script_path,
59 "-nostartfiles",
60 "-mcmodel=medany",
61 "-DXLEN=%d" % self.xlen,
62 "-o", binary_name),
63 xlen=self.xlen)
64 return binary_name
65
66 def extensionSupported(self, letter):
67 # target.misa is set by testlib.ExamineTarget
68 if self.misa:
69 return self.misa & (1 << (ord(letter.upper()) - ord('A')))
70 else:
71 return False
72
73 class Target(object):
74 # pylint: disable=too-many-instance-attributes
75
76 # List of Hart object instances, one for each hart in the target.
77 harts = []
78
79 # Name of the target. Defaults to the name of the class.
80 name = None
81
82 # GDB remotetimeout setting.
83 timeout_sec = 2
84
85 # Timeout waiting for the server to start up. This is different than the
86 # GDB timeout, which is how long GDB waits for commands to execute.
87 # The server_timeout is how long this script waits for the Server to be
88 # ready for GDB connections.
89 server_timeout_sec = 60
90
91 # Path to OpenOCD configuration file relative to the .py file where the
92 # target is defined. Defaults to <name>.cfg.
93 openocd_config_path = None
94
95 # List of commands that should be executed in gdb after connecting but
96 # before starting the test.
97 gdb_setup = []
98
99 # Internal variables:
100 directory = None
101 temporary_files = []
102
103 def __init__(self, path, parsed):
104 # Path to module.
105 self.path = path
106 self.directory = os.path.dirname(path)
107 self.server_cmd = parsed.server_cmd
108 self.sim_cmd = parsed.sim_cmd
109 Target.isolate = parsed.isolate
110 if not self.name:
111 self.name = type(self).__name__
112 # Default OpenOCD config file to <name>.cfg
113 if not self.openocd_config_path:
114 self.openocd_config_path = "%s.cfg" % self.name
115 self.openocd_config_path = os.path.join(self.directory,
116 self.openocd_config_path)
117 for i, hart in enumerate(self.harts):
118 hart.index = i
119 if not hart.name:
120 hart.name = "%s-%d" % (self.name, i)
121 # Default link script to <name>.lds
122 if not hart.link_script_path:
123 hart.link_script_path = "%s.lds" % self.name
124 hart.link_script_path = os.path.join(self.directory,
125 hart.link_script_path)
126
127 def create(self):
128 """Create the target out of thin air, eg. start a simulator."""
129 pass
130
131 def server(self):
132 """Start the debug server that gdb connects to, eg. OpenOCD."""
133 return testlib.Openocd(server_cmd=self.server_cmd,
134 config=self.openocd_config_path)
135
136 def add_target_options(parser):
137 parser.add_argument("target", help=".py file that contains definition for "
138 "the target to test with.")
139 parser.add_argument("--sim_cmd",
140 help="The command to use to start the actual target (e.g. "
141 "simulation)", default="spike")
142 parser.add_argument("--server_cmd",
143 help="The command to use to start the debug server (e.g. OpenOCD)")
144
145 xlen_group = parser.add_mutually_exclusive_group()
146 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
147 help="Force the target to be 32-bit.")
148 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
149 help="Force the target to be 64-bit.")
150
151 parser.add_argument("--isolate", action="store_true",
152 help="Try to run in such a way that multiple instances can run at "
153 "the same time. This may make it harder to debug a failure if it "
154 "does occur.")
155
156 def target(parsed):
157 directory = os.path.dirname(parsed.target)
158 filename = os.path.basename(parsed.target)
159 module_name = os.path.splitext(filename)[0]
160
161 sys.path.append(directory)
162 module = importlib.import_module(module_name)
163 found = []
164 for name in dir(module):
165 definition = getattr(module, name)
166 if type(definition) == type and issubclass(definition, Target):
167 found.append(definition)
168 assert len(found) == 1, "%s does not define exactly one subclass of " \
169 "targets.Target" % parsed.target
170
171 t = found[0](parsed.target, parsed)
172 assert t.harts, "%s doesn't have any harts defined!" % t.name
173
174 return t