arch-arm: fix GDB stub after SVE
[gem5.git] / util / cpt_upgrader.py
1 #!/usr/bin/env python2.7
2
3 # Copyright (c) 2012-2013,2015-2016 ARM Limited
4 # All rights reserved
5 #
6 # The license below extends only to copyright in the software and shall
7 # not be construed as granting a license to any other intellectual
8 # property including but not limited to intellectual property relating
9 # to a hardware implementation of the functionality of the software
10 # licensed hereunder. You may use the software subject to the license
11 # terms below provided that you ensure that this notice is replicated
12 # unmodified and in its entirety in all distributions of the software,
13 # modified or unmodified, in source code or in binary form.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions are
17 # met: redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer;
19 # redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution;
22 # neither the name of the copyright holders nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #
38 # Authors: Ali Saidi
39 # Curtis Dunham
40 #
41
42 # This python code is used to migrate checkpoints that were created in one
43 # version of the simulator to newer version. As features are added or bugs are
44 # fixed some of the state that needs to be checkpointed can change. If you have
45 # many historic checkpoints that you use, manually editing them to fix them is
46 # both time consuming and error-prone.
47
48 # This script provides a way to migrate checkpoints to the newer repository in
49 # a programmatic way. It can be imported into another script or used on the
50 # command line. From the command line the script will either migrate every
51 # checkpoint it finds recursively (-r option) or a single checkpoint. When a
52 # change is made to the gem5 repository that breaks previous checkpoints an
53 # upgrade() method should be implemented in its own .py file and placed in
54 # src/util/cpt_upgraders/. For each upgrader whose tag is not present in
55 # the checkpoint tag list, the upgrade() method will be run, passing in a
56 # ConfigParser object which contains the open file. As these operations can
57 # be isa specific the method can verify the isa and use regexes to find the
58 # correct sections that need to be updated.
59
60 # It is also possible to use this mechanism to revert prior tags. In this
61 # case, implement a downgrade() method instead. Dependencies should still
62 # work naturally - a tag depending on a tag with a downgrader means that it
63 # insists on the other tag being removed and its downgrader executed before
64 # its upgrader (or downgrader) can run. It is still the case that a tag
65 # can only be used once.
66
67 # Dependencies between tags are expressed by two variables at the top-level
68 # of the upgrader script: "depends" can be either a string naming another
69 # tag that it depends upon or a list of such strings; and "fwd_depends"
70 # accepts the same datatypes but it reverses the sense of the dependency
71 # arrow(s) -- it expresses that that tag depends upon the tag of the current
72 # upgrader. This can be especially valuable when maintaining private
73 # upgraders in private branches.
74
75
76 import ConfigParser
77 import glob, types, sys, os
78 import os.path as osp
79
80 verbose_print = False
81
82 def verboseprint(*args):
83 if not verbose_print:
84 return
85 for arg in args:
86 print arg,
87 print
88
89 class Upgrader:
90 tag_set = set()
91 untag_set = set() # tags to remove by downgrading
92 by_tag = {}
93 legacy = {}
94 def __init__(self, filename):
95 self.filename = filename
96 execfile(filename, {}, self.__dict__)
97
98 if not hasattr(self, 'tag'):
99 self.tag = osp.basename(filename)[:-3]
100 if not hasattr(self, 'depends'):
101 self.depends = []
102 elif isinstance(self.depends, str):
103 self.depends = [self.depends]
104
105 if not isinstance(self.depends, list):
106 print "Error: 'depends' for %s is the wrong type" % self.tag
107 sys.exit(1)
108
109 if hasattr(self, 'fwd_depends'):
110 if isinstance(self.fwd_depends, str):
111 self.fwd_depends = [self.fwd_depends]
112 else:
113 self.fwd_depends = []
114
115 if not isinstance(self.fwd_depends, list):
116 print "Error: 'fwd_depends' for %s is the wrong type" % self.tag
117 sys.exit(1)
118
119 if hasattr(self, 'upgrader'):
120 if not isinstance(self.upgrader, types.FunctionType):
121 print "Error: 'upgrader' for %s is %s, not function" \
122 % (self.tag, type(self))
123 sys.exit(1)
124 Upgrader.tag_set.add(self.tag)
125 elif hasattr(self, 'downgrader'):
126 if not isinstance(self.downgrader, types.FunctionType):
127 print "Error: 'downgrader' for %s is %s, not function" \
128 % (self.tag, type(self))
129 sys.exit(1)
130 Upgrader.untag_set.add(self.tag)
131 else:
132 print "Error: no upgrader or downgrader method for", self.tag
133 sys.exit(1)
134
135 if hasattr(self, 'legacy_version'):
136 Upgrader.legacy[self.legacy_version] = self
137
138 Upgrader.by_tag[self.tag] = self
139
140 def ready(self, tags):
141 for dep in self.depends:
142 if dep not in tags:
143 return False
144 return True
145
146 def update(self, cpt, tags):
147 if hasattr(self, 'upgrader'):
148 self.upgrader(cpt)
149 tags.add(self.tag)
150 verboseprint("applied upgrade for", self.tag)
151 else:
152 self.downgrader(cpt)
153 tags.remove(self.tag)
154 verboseprint("applied downgrade for", self.tag)
155
156 @staticmethod
157 def get(tag):
158 return Upgrader.by_tag[tag]
159
160 @staticmethod
161 def load_all():
162 util_dir = osp.dirname(osp.abspath(__file__))
163
164 for py in glob.glob(util_dir + '/cpt_upgraders/*.py'):
165 Upgrader(py)
166
167 # make linear dependences for legacy versions
168 i = 3
169 while i in Upgrader.legacy:
170 Upgrader.legacy[i].depends = [Upgrader.legacy[i-1].tag]
171 i = i + 1
172
173 # resolve forward dependencies and audit normal dependencies
174 for tag, upg in Upgrader.by_tag.items():
175 for fd in upg.fwd_depends:
176 if fd not in Upgrader.by_tag:
177 print "Error: '%s' cannot (forward) depend on "\
178 "nonexistent tag '%s'" % (fd, tag)
179 sys.exit(1)
180 Upgrader.by_tag[fd].depends.append(tag)
181 for dep in upg.depends:
182 if dep not in Upgrader.by_tag:
183 print "Error: '%s' cannot depend on "\
184 "nonexistent tag '%s'" % (tag, dep)
185 sys.exit(1)
186
187 def process_file(path, **kwargs):
188 if not osp.isfile(path):
189 import errno
190 raise IOError(ennro.ENOENT, "No such file", path)
191
192 verboseprint("Processing file %s...." % path)
193
194 if kwargs.get('backup', True):
195 import shutil
196 shutil.copyfile(path, path + '.bak')
197
198 cpt = ConfigParser.SafeConfigParser()
199
200 # gem5 is case sensitive with paramaters
201 cpt.optionxform = str
202
203 # Read the current data
204 cpt_file = file(path, 'r')
205 cpt.readfp(cpt_file)
206 cpt_file.close()
207
208 change = False
209
210 # Make sure we know what we're starting from
211 if cpt.has_option('root','cpt_ver'):
212 cpt_ver = cpt.getint('root','cpt_ver')
213
214 # Legacy linear checkpoint version
215 # convert to list of tags before proceeding
216 tags = set([])
217 for i in xrange(2, cpt_ver+1):
218 tags.add(Upgrader.legacy[i].tag)
219 verboseprint("performed legacy version -> tags conversion")
220 change = True
221
222 cpt.remove_option('root', 'cpt_ver')
223 elif cpt.has_option('Globals','version_tags'):
224 tags = set((''.join(cpt.get('Globals','version_tags'))).split())
225 else:
226 print "fatal: no version information in checkpoint"
227 exit(1)
228
229 verboseprint("has tags", ' '.join(tags))
230 # If the current checkpoint has a tag we don't know about, we have
231 # a divergence that (in general) must be addressed by (e.g.) merging
232 # simulator support for its changes.
233 unknown_tags = tags - (Upgrader.tag_set | Upgrader.untag_set)
234 if unknown_tags:
235 print "warning: upgrade script does not recognize the following "\
236 "tags in this checkpoint:", ' '.join(unknown_tags)
237
238 # Apply migrations for tags not in checkpoint and tags present for which
239 # downgraders are present, respecting dependences
240 to_apply = (Upgrader.tag_set - tags) | (Upgrader.untag_set & tags)
241 while to_apply:
242 ready = set([ t for t in to_apply if Upgrader.get(t).ready(tags) ])
243 if not ready:
244 print "could not apply these upgrades:", ' '.join(to_apply)
245 print "update dependences impossible to resolve; aborting"
246 exit(1)
247
248 for tag in ready:
249 Upgrader.get(tag).update(cpt, tags)
250 change = True
251
252 to_apply -= ready
253
254 if not change:
255 verboseprint("...nothing to do")
256 return
257
258 cpt.set('Globals', 'version_tags', ' '.join(tags))
259
260 # Write the old data back
261 verboseprint("...completed")
262 cpt.write(file(path, 'w'))
263
264 if __name__ == '__main__':
265 from optparse import OptionParser, SUPPRESS_HELP
266 parser = OptionParser("usage: %prog [options] <filename or directory>")
267 parser.add_option("-r", "--recurse", action="store_true",
268 help="Recurse through all subdirectories modifying "\
269 "each checkpoint that is found")
270 parser.add_option("-N", "--no-backup", action="store_false",
271 dest="backup", default=True,
272 help="Do no backup each checkpoint before modifying it")
273 parser.add_option("-v", "--verbose", action="store_true",
274 help="Print out debugging information as")
275 parser.add_option("--get-cc-file", action="store_true",
276 # used during build; generate src/sim/tags.cc and exit
277 help=SUPPRESS_HELP)
278
279 (options, args) = parser.parse_args()
280 verbose_print = options.verbose
281
282 Upgrader.load_all()
283
284 if options.get_cc_file:
285 print "// this file is auto-generated by util/cpt_upgrader.py"
286 print "#include <string>"
287 print "#include <set>"
288 print
289 print "std::set<std::string> version_tags = {"
290 for tag in Upgrader.tag_set:
291 print " \"%s\"," % tag
292 print "};"
293 exit(0)
294 elif len(args) != 1:
295 parser.error("You must specify a checkpoint file to modify or a "\
296 "directory of checkpoints to recursively update")
297
298 # Deal with shell variables and ~
299 path = osp.expandvars(osp.expanduser(args[0]))
300
301 # Process a single file if we have it
302 if osp.isfile(path):
303 process_file(path, **vars(options))
304 # Process an entire directory
305 elif osp.isdir(path):
306 cpt_file = osp.join(path, 'm5.cpt')
307 if options.recurse:
308 # Visit very file and see if it matches
309 for root,dirs,files in os.walk(path):
310 for name in files:
311 if name == 'm5.cpt':
312 process_file(osp.join(root,name), **vars(options))
313 for dir in dirs:
314 pass
315 # Maybe someone passed a cpt.XXXXXXX directory and not m5.cpt
316 elif osp.isfile(cpt_file):
317 process_file(cpt_file, **vars(options))
318 else:
319 print "Error: checkpoint file not found at in %s " % path,
320 print "and recurse not specified"
321 sys.exit(1)
322 sys.exit(0)
323