File: cs_debug_symbol.py

package info (click to toggle)
code-saturne 7.0.2%2Brepack-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,868 kB
  • sloc: ansic: 395,271; f90: 100,755; python: 86,746; cpp: 6,227; makefile: 4,247; xml: 2,389; sh: 1,091; javascript: 69
file content (252 lines) | stat: -rw-r--r-- 7,404 bytes parent folder | download
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2021 EDF S.A.
#
# 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

"""
This modules describes the script used to translate runtime symbols
obtained from a stack into the corresponding file and line.
"""

#===============================================================================
# Import required Python modules
#===============================================================================

import sys
from optparse import OptionParser

from code_saturne.cs_exec_environment import get_command_output

#-------------------------------------------------------------------------------
# Print a help page.
#-------------------------------------------------------------------------------

def print_help(pkg):
    """
    Print a help page.
    """

    help_string = \
"""
This is a debug symbol to file/line utility. Usage:

%s symbol2line -s symbol1 [-s symbol2, .. -s symbolN] [optional arguments]
Example:
If the stack in the listing/error.log file contains the following line :
1: 0x55834c6102d7 <cs_function+0x17>             (cs_solver)

then the command to launch is : '%s symbol2line -s cs_function+0x17 [optional arguments]'
"""

    print(help_string % (pkg.name, pkg.name))

#-------------------------------------------------------------------------------
# Process command line arguments
#-------------------------------------------------------------------------------

def process_cmd_line(argv, pkg):
    """
    Processes the passed command line arguments.
    """

    for arg in argv:
        if arg == "-h" or arg == "--help":
            print_help(pkg)

    if sys.argv[0][-3:] == '.py':
        usage = "usage: %prog [options]"
    else:
        usage = "usage: %prog compile [options]"

    parser = OptionParser(usage=usage)

    parser.add_option('-s', '--symbol', dest='symbols', type='string',
                      action='append',
                      help="Symbols to translate into file:line")

    parser.add_option('-p', '--path', dest='path', type='string',
                      help="Optional path to executable")

    parser.add_option('-e', '--executable', dest='solver', type='string',
                      help="Optional name of executable.")

    parser.set_defaults(path=None)
    parser.set_defaults(solver=None)
    parser.set_defaults(symbols=[])

    (options, args) = parser.parse_args(argv)

    if len(args) > 0:
        parser.print_help()
        sys.exit(1)

    return options


#===============================================================================
# Main class
#===============================================================================

class cs_debug_symbol_translator:
    """
    Class with utility functions to translate debug symbols into
    file:line form using addr2line.
    """

    #---------------------------------------------------------------------------

    def __init__(self, pkg, path=None, solver=None):
        """
        Constructor
        """

        self.pkg    = pkg
        self.path   = path
        self.solver = solver
        if self.solver == None:
            self.solver = pkg.solver

    #---------------------------------------------------------------------------

    def split_symbol(self, symbol):
        """
        Split stack symbol into name and offset
        """

        name, offset = symbol.split("+")

        return name, offset

    #---------------------------------------------------------------------------

    def get_runtime_adresse(self, symbol_name):
        """
        Get runtime address of a symbol
        """

        solver_path = "."
        if self.path:
            solver_path = self.path

        solver_path = "/".join([solver_path, self.solver])


        cmd = "nm -D %s | grep %s" % (solver_path, symbol_name)

        out = get_command_output(cmd)

        if out == "":
            sys.exit(1)

        run_time_addr = "0x"+out.split(" ")[0]


        return run_time_addr

    #---------------------------------------------------------------------------

    def get_line_from_address(self, addr, offset):
        """
        Get file and line using the runtime addresse and offset of symbol
        """

        solver_path = "."
        if self.path:
            solver_path = self.path

        solver_path = "/".join([solver_path, self.solver])

        addr_int = int(addr, 0)
        off_int  = int(offset, 0)
        hex_addr = hex(addr_int + off_int)

        cmd = "addr2line -e %s %s" % (solver_path, hex_addr)

        out = get_command_output(cmd)

        if out == None or out == "":
            sys.exit(1)

        return out

    def symbol2line(self, sym_offset):

        sym, offset  = self.split_symbol(sym_offset)
        runtime_addr = self.get_runtime_adresse(sym)
        sym_line     = self.get_line_from_address(runtime_addr, offset)

        return sym_line


#===============================================================================
# Get symbols' lines
#===============================================================================

def symbols_to_lines(argv, pkg):
    """
    For each debug symbol, get the corresponding file and line.
    """

    if not pkg:
        from code_saturne.cs_package import package
        pkg = package()

    options = process_cmd_line(argv, pkg)

    translator = cs_debug_symbol_translator(pkg,
                                            path=options.path,
                                            solver=options.solver)

    for sym in options.symbols:
        file_line = translator.symbol2line(sym)

        if "??:?" in file_line:
            sys.stdout.write("Symbol '%s' yielded no file/line.\n" % sym)
            sys.stdout.write("Please rerun case with an instance compiled with --debug option.\n")

        else:
            res = "%s -> %s" % (sym, file_line)
            sys.stdout.write(res)

    return 0

#===============================================================================
# Main function
#===============================================================================

def main(argv, pkg):
    """
    Main function
    """
    return symbols_to_lines(argv, pkg)

#-------------------------------------------------------------------------------

if __name__ == "__main__":

    retval = main(argv=sys.argv[1:])

    sys.exit(retval)

#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------