aboutsummaryrefslogtreecommitdiff
path: root/src/util/formaline.py
blob: 6656615f0d9a2e36ac0a66fefdb1fe01402fb29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# gdb extension script to extract formaline tarballs from Cactus executable
# Copyright (C) 2013 Roland Haas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
import io
import sys

# for some reason gdb passes an empty string in argv[0] if no argument is given
if(not sys.argv[0]):
  raise ValueError("needexactly one argument: gdb -P formaline.py exe/cactus_sim")

# open executable and get handle to FOrmalin'e top-level source object
gdb.execute("file %s" % sys.argv[0])
inferior = gdb.inferiors()[0]
cactus_source = gdb.parse_and_eval("cactus_source")

# replicate logic from output_source.c
# struct datainfo {
#   unsigned char const *data;
#   size_t length;
#   struct datainfo const *next;
# };
# 
# struct sourceinfo {
#   struct datainfo const *first;
#   char const *arrangement;
#   char const *thorn;
# };
count = 0
while(cactus_source[count] != 0):
  sourceinfo = cactus_source[count].dereference()
  fn = "Cactus-source-%s.tar.gz" % sourceinfo['thorn'].string()
  fh = io.open(fn, mode='wb')
  datainfo = sourceinfo['first']
  while(datainfo != 0):
    data = inferior.read_memory(datainfo['data'], datainfo['length'])
    fh.write(data)
    datainfo = datainfo['next']
  fh.close()
  count += 1