Add back code to clean up triggers in entry.S
[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 # Implements dmode in tdata1 as described in the spec. Targets that need
38 # this value set to False are not compliant with the spec (but still usable
39 # as long as running code doesn't try to mess with triggers set by an
40 # external debugger).
41 honors_tdata1_hmode = True
42
43 # Internal variables:
44 directory = None
45 temporary_files = []
46 temporary_binary = None
47
48 def __init__(self, path, parsed):
49 # Path to module.
50 self.path = path
51 self.directory = os.path.dirname(path)
52 self.server_cmd = parsed.server_cmd
53 self.sim_cmd = parsed.sim_cmd
54 self.isolate = parsed.isolate
55 if not self.name:
56 self.name = type(self).__name__
57 # Default OpenOCD config file to <name>.cfg
58 if not self.openocd_config_path:
59 self.openocd_config_path = "%s.cfg" % self.name
60 self.openocd_config_path = os.path.join(self.directory,
61 self.openocd_config_path)
62 # Default link script to <name>.lds
63 if not self.link_script_path:
64 self.link_script_path = "%s.lds" % self.name
65 self.link_script_path = os.path.join(self.directory,
66 self.link_script_path)
67
68 def create(self):
69 """Create the target out of thin air, eg. start a simulator."""
70 pass
71
72 def server(self):
73 """Start the debug server that gdb connects to, eg. OpenOCD."""
74 return testlib.Openocd(server_cmd=self.server_cmd,
75 config=self.openocd_config_path)
76
77 def compile(self, *sources):
78 binary_name = "%s_%s-%d" % (
79 self.name,
80 os.path.basename(os.path.splitext(sources[0])[0]),
81 self.xlen)
82 if self.isolate:
83 self.temporary_binary = tempfile.NamedTemporaryFile(
84 prefix=binary_name + "_")
85 binary_name = self.temporary_binary.name
86 Target.temporary_files.append(self.temporary_binary)
87 march = "rv%dima" % self.xlen
88 for letter in "fdc":
89 if self.extensionSupported(letter):
90 march += letter
91 testlib.compile(sources +
92 ("programs/entry.S", "programs/init.c",
93 "-I", "../env",
94 "-march=%s" % march,
95 "-T", self.link_script_path,
96 "-nostartfiles",
97 "-mcmodel=medany",
98 "-DXLEN=%d" % self.xlen,
99 "-o", binary_name),
100 xlen=self.xlen)
101 return binary_name
102
103 def extensionSupported(self, letter):
104 # target.misa is set by testlib.ExamineTarget
105 if self.misa:
106 return self.misa & (1 << (ord(letter.upper()) - ord('A')))
107 else:
108 return False
109
110 def add_target_options(parser):
111 parser.add_argument("target", help=".py file that contains definition for "
112 "the target to test with.")
113 parser.add_argument("--sim_cmd",
114 help="The command to use to start the actual target (e.g. "
115 "simulation)", default="spike")
116 parser.add_argument("--server_cmd",
117 help="The command to use to start the debug server (e.g. OpenOCD)")
118
119 xlen_group = parser.add_mutually_exclusive_group()
120 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
121 help="Force the target to be 32-bit.")
122 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
123 help="Force the target to be 64-bit.")
124
125 parser.add_argument("--isolate", action="store_true",
126 help="Try to run in such a way that multiple instances can run at "
127 "the same time. This may make it harder to debug a failure if it "
128 "does occur.")
129
130 def target(parsed):
131 directory = os.path.dirname(parsed.target)
132 filename = os.path.basename(parsed.target)
133 module_name = os.path.splitext(filename)[0]
134
135 sys.path.append(directory)
136 module = importlib.import_module(module_name)
137 found = []
138 for name in dir(module):
139 definition = getattr(module, name)
140 if type(definition) == type and issubclass(definition, Target):
141 found.append(definition)
142 assert len(found) == 1, "%s does not define exactly one subclass of " \
143 "targets.Target" % parsed.target
144
145 return found[0](parsed.target, parsed)