summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/gdb_index.py
blob: 5850b8560d3915c8f8e730f7f6ea2304279b4abc (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
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
# Copyright 2020 MongoDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

import SCons


def _update_builder(env, builder):

    verbose = '' if env.Verbose() else None

    base_action = builder.action
    if not isinstance(base_action, SCons.Action.ListAction):
        base_action = SCons.Action.ListAction([base_action])

    # There are cases were a gdb-index file is NOT generated from gdb 'save gdb-index' command,
    # mostly shim libraries where there is no code in the library, and the following Actions would
    # then fail. The files are created make sure there is always a file to operate on, if its an
    # empty file then the following actions are basically NOOP, and its cleaner then writing
    # conditions into each action.

    # Because this is all taking under one task, the list action will always run all actions if
    # the target is out of date. So the gdb-index files would always be regenerated, and there is
    # no value in keeping them around, it will just waste disk space. Therefore they should be
    # removed as if they never existed from the task. The build system doesn't need to know about
    # them.
    if env.get('DWARF_VERSION') <= 4:
        base_action.list.extend([
            SCons.Action.Action(
                'touch ${TARGET}.gdb-index',
                verbose,
            ),
            SCons.Action.Action(
                '$GDB --batch-silent --quiet --nx --eval-command "save gdb-index ${TARGET.dir}" $TARGET',
                "$GDB_INDEX_GEN_INDEX_STR",
            ),
            SCons.Action.Action(
                '$OBJCOPY --add-section .gdb_index=${TARGET}.gdb-index --set-section-flags .gdb_index=readonly ${TARGET} ${TARGET}',
                "$GDB_INDEX_ADD_SECTION_STR",
            ),
            SCons.Action.Action(
                'rm -f ${TARGET}.gdb-index',
                verbose,
            ),
        ])
    else:
        base_action.list.extend([
            SCons.Action.Action(
                'touch ${TARGET}.debug_names ${TARGET}.debug_str',
                verbose,
            ),
            SCons.Action.Action(
                '$GDB --batch-silent --quiet --nx --eval-command "save gdb-index -dwarf-5 ${TARGET.dir}" $TARGET',
                "$GDB_INDEX_GEN_INDEX_STR",
            ),
            SCons.Action.Action(
                '$OBJCOPY --dump-section .debug_str=${TARGET}.debug_str.new $TARGET',
                verbose,
            ),
            SCons.Action.Action(
                'cat ${TARGET}.debug_str >>${TARGET}.debug_str.new',
                verbose,
            ),
            SCons.Action.Action(
                '$OBJCOPY --add-section .debug_names=${TARGET}.debug_names --set-section-flags .debug_names=readonly --update-section .debug_str=${TARGET}.debug_str.new ${TARGET} ${TARGET}',
                "$GDB_INDEX_ADD_SECTION_STR",
            ),
            SCons.Action.Action(
                'rm -f ${TARGET}.debug_names ${TARGET}.debug_str.new ${TARGET}.debug_str',
                verbose,
            ),
        ])

    builder.action = base_action


def generate(env):
    if env.get("OBJCOPY", None) is None:
        env["OBJCOPY"] = env.WhereIs("objcopy")
    if env.get("GDB", None) is None:
        env["GDB"] = env.WhereIs("gdb")

    if not env.Verbose():
        env.Append(
            GDB_INDEX_GEN_INDEX_STR="Using $GDB to generate index for $TARGET",
            GDB_INDEX_ADD_SECTION_STR="Adding index sections into $TARGET",
        )

    for builder in ["Program", "SharedLibrary", "LoadableModule"]:
        _update_builder(env, env["BUILDERS"][builder])


def exists(env):
    result = False
    if env.TargetOSIs("posix"):
        objcopy = env.get("OBJCOPY", None) or env.WhereIs("objcopy")
        gdb = env.get("GDB", None) or env.WhereIs("gdb")
        try:
            dwarf_version = int(env.get('DWARF_VERSION'))
        except ValueError:
            dwarf_version = None

        unset_vars = []
        if not objcopy:
            unset_vars += ['OBJCOPY']
        if not gdb:
            unset_vars += ['GDB']
        if not dwarf_version:
            unset_vars += ['DWARF_VERSION']

        if not unset_vars:
            print("Enabled generation of gdb index into binaries.")
            result = True
        else:
            print(f"Disabled generation gdb index because {', '.join(unset_vars)} were not set.")
    return result