Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2014 The ChromiumOS Authors |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Install debug symbols for specified packages. |
| 6 | |
| 7 | Only reinstall the debug symbols if they are not already installed to save time. |
| 8 | |
| 9 | The debug symbols are packaged outside of the prebuilt package in a |
| 10 | .debug.tbz2 archive when FEATURES=separatedebug is set (by default on |
| 11 | builders). On local machines, separatedebug is not set and the debug symbols |
| 12 | are part of the prebuilt package. |
| 13 | """ |
| 14 | |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 15 | import argparse |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 16 | import functools |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 17 | import logging |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 18 | import multiprocessing |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 19 | import os |
| 20 | import pickle |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 21 | import tempfile |
Mike Frysinger | e852b07 | 2021-05-21 12:39:03 -0400 | [diff] [blame] | 22 | import urllib.parse |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 23 | |
| 24 | from chromite.lib import binpkg |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 25 | from chromite.lib import build_target_lib |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 26 | from chromite.lib import cache |
| 27 | from chromite.lib import commandline |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 28 | from chromite.lib import constants |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 29 | from chromite.lib import cros_build_lib |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 30 | from chromite.lib import gs |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 31 | from chromite.lib import osutils |
Gilad Arnold | 83233ed | 2015-05-08 12:12:13 -0700 | [diff] [blame] | 32 | from chromite.lib import path_util |
Mike Frysinger | 151f5fb | 2019-10-22 20:36:25 -0400 | [diff] [blame] | 33 | from chromite.utils import outcap |
Bertrand SIMONNET | 01394c3 | 2015-02-09 17:20:25 -0800 | [diff] [blame] | 34 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 35 | |
Bertrand SIMONNET | 01394c3 | 2015-02-09 17:20:25 -0800 | [diff] [blame] | 36 | if cros_build_lib.IsInsideChroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | # pylint: disable=import-error |
| 38 | from portage import create_trees |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 39 | |
Mike Frysinger | 6a2b0f2 | 2020-02-20 13:34:07 -0500 | [diff] [blame] | 40 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | DEBUG_SYMS_EXT = ".debug.tbz2" |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 42 | |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 43 | # We cache the package indexes. When the format of what we store changes, |
| 44 | # bump the cache version to avoid problems. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | CACHE_VERSION = "1" |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 46 | |
| 47 | |
Alex Klein | 074f94f | 2023-06-22 10:32:06 -0600 | [diff] [blame^] | 48 | class DebugSymbolsInstaller: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 49 | """Container for environment objects, needed for multiprocessing.""" |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | def __init__(self, vartree, gs_context, sysroot, stdout_to_null): |
| 52 | self._vartree = vartree |
| 53 | self._gs_context = gs_context |
| 54 | self._sysroot = sysroot |
| 55 | self._stdout_to_null = stdout_to_null |
| 56 | self._capturer = outcap.OutputCapturer() |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | def __enter__(self): |
| 59 | if self._stdout_to_null: |
| 60 | self._capturer.StartCapturing() |
Mike Frysinger | 151f5fb | 2019-10-22 20:36:25 -0400 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | return self |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def __exit__(self, _exc_type, _exc_val, _exc_tb): |
| 65 | if self._stdout_to_null: |
| 66 | self._capturer.StopCapturing() |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | def Install(self, cpv, url): |
| 69 | """Install the debug symbols for |cpv|. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | This will install the debug symbols tarball in PKGDIR so that it can be |
| 72 | used later. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 75 | cpv: the cpv of the package to build. This assumes that the cpv is |
| 76 | installed in the sysroot. |
| 77 | url: url of the debug symbols archive. This could be a Google |
| 78 | Storage url or a local path. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | """ |
| 80 | archive = os.path.join( |
| 81 | self._vartree.settings["PKGDIR"], cpv + DEBUG_SYMS_EXT |
| 82 | ) |
| 83 | # GsContext does not understand file:// scheme so we need to extract the |
| 84 | # path ourselves. |
| 85 | parsed_url = urllib.parse.urlsplit(url) |
| 86 | if not parsed_url.scheme or parsed_url.scheme == "file": |
| 87 | url = parsed_url.path |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | if not os.path.isfile(archive): |
| 90 | self._gs_context.Copy(url, archive, debug_level=logging.DEBUG) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | compression = cros_build_lib.CompressionDetectType(archive) |
| 93 | compressor = cros_build_lib.FindCompressor(compression) |
Mike Frysinger | 2e169a9 | 2022-04-27 02:15:09 -0400 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | with osutils.TempDir(sudo_rm=True) as tempdir: |
| 96 | cros_build_lib.sudo_run( |
| 97 | ["tar", "-I", compressor, "-xf", archive, "-C", tempdir], |
| 98 | debug_level=logging.DEBUG, |
| 99 | capture_output=True, |
| 100 | ) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 102 | with open( |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 103 | self._vartree.getpath(cpv, filename="CONTENTS"), |
| 104 | "a", |
| 105 | encoding="utf-8", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | ) as content_file: |
| 107 | # Merge the content of the temporary dir into the sysroot. |
| 108 | # pylint: disable=protected-access |
| 109 | link = self._vartree.dbapi._dblink(cpv) |
| 110 | link.mergeme( |
| 111 | tempdir, self._sysroot, content_file, None, "", {}, None |
| 112 | ) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 113 | |
| 114 | |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 115 | def ShouldGetSymbols(cpv, vardb, remote_symbols): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | """Return True if the symbols for cpv are available and are not installed. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | We try to check if the symbols are installed before checking availability as |
| 119 | a GS request is more expensive than checking locally. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 122 | cpv: cpv of the package |
| 123 | vardb: a vartree dbapi |
| 124 | remote_symbols: a mapping from cpv to debug symbols url |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 127 | True if |cpv|'s debug symbols are not installed and are available |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | """ |
| 129 | features, contents = vardb.aux_get(cpv, ["FEATURES", "CONTENTS"]) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | return ( |
| 132 | "separatedebug" in features |
| 133 | and not "/usr/lib/debug/" in contents |
| 134 | and cpv in remote_symbols |
| 135 | ) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def RemoteSymbols(vartree, binhost_cache=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | """Get the cpv to debug symbols mapping. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | If several binhost contain debug symbols for the same cpv, keep only the |
| 142 | highest priority one. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 143 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 144 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 145 | vartree: a vartree |
| 146 | binhost_cache: a cache containing the cpv to debug symbols url for all |
| 147 | known binhosts. None if we are not caching binhosts. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 150 | a dictionary mapping the cpv to a remote debug symbols gsurl. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | """ |
| 152 | symbols_mapping = {} |
| 153 | for binhost in vartree.settings["PORTAGE_BINHOST"].split(): |
| 154 | if binhost: |
| 155 | symbols_mapping.update(ListBinhost(binhost, binhost_cache)) |
| 156 | return symbols_mapping |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 157 | |
| 158 | |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 159 | def GetPackageIndex(binhost, binhost_cache=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 160 | """Get the packages index for |binhost|. |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | If a cache is provided, use it to a cache remote packages index. |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 165 | binhost: a portage binhost, local, google storage or http. |
| 166 | binhost_cache: a cache for the remote packages index. |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 167 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 169 | A PackageIndex object. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | """ |
| 171 | key = binhost.split("://")[-1] |
| 172 | key = key.rstrip("/").split("/") |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | if binhost_cache and binhost_cache.Lookup(key).Exists(): |
| 175 | with open(binhost_cache.Lookup(key).path, "rb") as f: |
| 176 | return pickle.load(f) |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | pkgindex = binpkg.GrabRemotePackageIndex(binhost) |
| 179 | if pkgindex and binhost_cache: |
| 180 | # Only cache remote binhosts as local binhosts can change. |
| 181 | with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 182 | pickle.dump(pkgindex, temp_file) |
| 183 | temp_file.file.close() |
| 184 | binhost_cache.Lookup(key).Assign(temp_file.name) |
| 185 | elif pkgindex is None: |
| 186 | urlparts = urllib.parse.urlsplit(binhost) |
| 187 | if urlparts.scheme not in ("file", ""): |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 188 | # Don't fail the build on network errors. Print a warning message |
| 189 | # and continue. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | logging.warning("Could not get package index %s", binhost) |
| 191 | return None |
Bertrand SIMONNET | dd66ab5 | 2015-01-08 14:48:51 -0800 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | binhost = urlparts.path |
| 194 | if not os.path.isdir(binhost): |
| 195 | raise ValueError("unrecognized binhost format for %s.") |
| 196 | pkgindex = binpkg.GrabLocalPackageIndex(binhost) |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 197 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | return pkgindex |
Bertrand SIMONNET | 1e146e5 | 2014-12-11 14:11:56 -0800 | [diff] [blame] | 199 | |
| 200 | |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 201 | def ListBinhost(binhost, binhost_cache=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | """Return the cpv to debug symbols mapping for a given binhost. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | List the content of the binhost to extract the cpv to debug symbols |
| 205 | mapping. If --cachebinhost is set, we cache the result to avoid the |
| 206 | cost of gsutil every time. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 209 | binhost: a portage binhost, local or on google storage. |
| 210 | binhost_cache: a cache containing mappings cpv to debug symbols url for |
| 211 | a given binhost (None if we don't want to cache). |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 212 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 213 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 214 | A cpv to debug symbols url mapping. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | """ |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | symbols = {} |
| 218 | pkgindex = GetPackageIndex(binhost, binhost_cache) |
| 219 | if pkgindex is None: |
| 220 | return symbols |
| 221 | |
| 222 | for p in pkgindex.packages: |
| 223 | if p.get("DEBUG_SYMBOLS") == "yes": |
| 224 | path = p.get("PATH", p["CPV"] + ".tbz2") |
| 225 | base_url = pkgindex.header.get("URI", binhost) |
| 226 | symbols[p["CPV"]] = os.path.join( |
| 227 | base_url, path.replace(".tbz2", DEBUG_SYMS_EXT) |
| 228 | ) |
| 229 | |
Bertrand SIMONNET | dd66ab5 | 2015-01-08 14:48:51 -0800 | [diff] [blame] | 230 | return symbols |
| 231 | |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 232 | |
| 233 | def GetMatchingCPV(package, vardb): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 234 | """Return the cpv of the installed package matching |package|. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 235 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | Args: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 237 | package: package name |
| 238 | vardb: a vartree dbapi |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 241 | The cpv of the installed package whose name matches |package|. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | """ |
| 243 | matches = vardb.match(package) |
| 244 | if not matches: |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 245 | cros_build_lib.Die("Could not find package %s", package) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | if len(matches) != 1: |
| 247 | cros_build_lib.Die( |
| 248 | "Ambiguous package name: %s.\n" |
| 249 | "Matching: %s" % (package, " ".join(matches)) |
| 250 | ) |
| 251 | return matches[0] |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 252 | |
| 253 | |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 254 | def GetVartree(sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | """Get the portage vartree. |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 256 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | This must be called in subprocesses only. The vartree is not serializable, |
| 258 | and is implemented as a singleton in portage, so it always breaks the |
| 259 | parallel call when initialized before it is called. |
| 260 | """ |
| 261 | trees = create_trees(target_root=sysroot, config_root=sysroot) |
| 262 | return trees[sysroot]["vartree"] |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 263 | |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 264 | |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 265 | def GetBinhostCache(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 266 | """Get and optionally clear the binhost cache.""" |
| 267 | cache_dir = os.path.join( |
| 268 | path_util.FindCacheDir(), "cros_install_debug_syms-v" + CACHE_VERSION |
| 269 | ) |
| 270 | if options.clearcache: |
| 271 | osutils.RmDir(cache_dir, ignore_missing=True) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 272 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 273 | binhost_cache = None |
| 274 | if options.cachebinhost: |
| 275 | binhost_cache = cache.DiskCache(cache_dir) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 276 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | return binhost_cache |
Bertrand SIMONNET | bef192e | 2014-12-17 14:09:16 -0800 | [diff] [blame] | 278 | |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 279 | |
| 280 | def GetInstallArgs(options, sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | """Resolve the packages that are to have their debug symbols installed. |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 282 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | This function must be called in subprocesses only since it requires the |
| 284 | portage vartree. |
| 285 | See: GetVartree |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 286 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | Returns: |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 288 | list[(pkg, binhost_url)] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 289 | """ |
| 290 | vartree = GetVartree(sysroot) |
| 291 | symbols_mapping = RemoteSymbols(vartree, GetBinhostCache(options)) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | if options.all: |
| 294 | to_install = vartree.dbapi.cpv_all() |
| 295 | else: |
| 296 | to_install = [ |
| 297 | GetMatchingCPV(p, vartree.dbapi) for p in options.packages |
| 298 | ] |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 299 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | to_install = [ |
| 301 | p |
| 302 | for p in to_install |
| 303 | if ShouldGetSymbols(p, vartree.dbapi, symbols_mapping) |
| 304 | ] |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | return [(p, symbols_mapping[p]) for p in to_install] |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 307 | |
| 308 | |
| 309 | def ListInstallArgs(options, sysroot): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | """List the args for the calling process.""" |
| 311 | lines = ["%s %s" % arg for arg in GetInstallArgs(options, sysroot)] |
| 312 | print("\n".join(lines)) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 313 | |
| 314 | |
| 315 | def GetInstallArgsList(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 316 | """Get the install args from the --list reexec of the command.""" |
| 317 | # Insert the --list as the first argument to prevent parsing --list as a |
| 318 | # package when a package is given. |
| 319 | cmd = [argv[0]] + ["--list"] + argv[1:] |
| 320 | result = cros_build_lib.run(cmd, capture_output=True, encoding="utf-8") |
| 321 | lines = result.stdout.splitlines() |
| 322 | return [line.split() for line in lines if line] |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 323 | |
| 324 | |
| 325 | def _InstallOne(sysroot, debug, args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | """Parallelizable wrapper for the DebugSymbolsInstaller.Install method.""" |
| 327 | vartree = GetVartree(sysroot) |
| 328 | gs_context = gs.GSContext(boto_file=vartree.settings["BOTO_CONFIG"]) |
| 329 | with DebugSymbolsInstaller( |
| 330 | vartree, gs_context, sysroot, not debug |
| 331 | ) as installer: |
| 332 | installer.Install(*args) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 333 | |
| 334 | |
| 335 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | """Build the argument parser.""" |
| 337 | parser = commandline.ArgumentParser(description=__doc__) |
| 338 | parser.add_argument("--board", help="Board name (required).", required=True) |
| 339 | parser.add_argument( |
| 340 | "--all", |
| 341 | action="store_true", |
| 342 | default=False, |
| 343 | help="Install the debug symbols for all installed packages", |
| 344 | ) |
| 345 | parser.add_argument( |
| 346 | "packages", |
| 347 | nargs=argparse.REMAINDER, |
| 348 | help="list of packages that need the debug symbols.", |
| 349 | ) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 350 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 351 | advanced = parser.add_argument_group("Advanced options") |
| 352 | advanced.add_argument( |
| 353 | "--nocachebinhost", |
| 354 | dest="cachebinhost", |
| 355 | default=True, |
| 356 | action="store_false", |
| 357 | help="Don't cache the list of files contained in binhosts. " |
| 358 | "(Default: cache)", |
| 359 | ) |
| 360 | advanced.add_argument( |
| 361 | "--clearcache", |
| 362 | action="store_true", |
| 363 | default=False, |
| 364 | help="Clear the binhost cache.", |
| 365 | ) |
| 366 | advanced.add_argument( |
| 367 | "-j", |
| 368 | "--jobs", |
| 369 | default=multiprocessing.cpu_count(), |
| 370 | type=int, |
| 371 | help="Number of processes to run in parallel.", |
| 372 | ) |
| 373 | advanced.add_argument( |
| 374 | "--list", |
| 375 | action="store_true", |
| 376 | default=False, |
| 377 | help="List the packages whose debug symbols would be installed and " |
| 378 | "their binhost path.", |
| 379 | ) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | return parser |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 382 | |
| 383 | |
| 384 | def ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 385 | """Parse and validate arguments.""" |
| 386 | parser = GetParser() |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | options = parser.parse_args(argv) |
| 389 | if options.all and options.packages: |
| 390 | parser.error("Cannot use --all with a list of packages") |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 391 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 392 | options.Freeze() |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | return options |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 395 | |
| 396 | |
| 397 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | if not cros_build_lib.IsInsideChroot(): |
| 399 | raise commandline.ChrootRequiredError(argv) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 400 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 401 | options = ParseArgs(argv) |
Mike Frysinger | 2b1fed5 | 2022-04-27 02:15:33 -0400 | [diff] [blame] | 402 | |
Mike Frysinger | 164ec03 | 2023-03-27 16:15:14 -0400 | [diff] [blame] | 403 | cmd = [constants.CHROMITE_BIN_DIR / "cros_install_debug_syms"] + argv |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 404 | if osutils.IsNonRootUser(): |
| 405 | cros_build_lib.sudo_run(cmd) |
| 406 | return |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | # sysroot must have a trailing / as the tree dictionary produced by |
| 409 | # create_trees in indexed with a trailing /. |
| 410 | sysroot = build_target_lib.get_default_sysroot_path(options.board) + "/" |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 411 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 412 | if options.list: |
| 413 | ListInstallArgs(options, sysroot) |
| 414 | return |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 415 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 416 | args = GetInstallArgsList(cmd) |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 417 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | if not args: |
| 419 | logging.info("No packages found needing debug symbols.") |
| 420 | return |
Alex Klein | 161495b | 2019-09-26 16:59:46 -0600 | [diff] [blame] | 421 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 422 | # Partial to simplify the arguments to parallel since the first two are the |
| 423 | # same for every call. |
| 424 | partial_install = functools.partial(_InstallOne, sysroot, options.debug) |
| 425 | with multiprocessing.Pool(processes=options.jobs) as pool: |
| 426 | pool.map(partial_install, args) |
Bertrand SIMONNET | 6b6a131 | 2014-10-29 18:37:51 -0700 | [diff] [blame] | 427 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 428 | logging.debug("installation done, updating packages index file") |
| 429 | packages_dir = os.path.join(sysroot, "packages") |
| 430 | packages_file = os.path.join(packages_dir, "Packages") |
Alex Klein | 68b270c | 2023-04-14 14:42:50 -0600 | [diff] [blame] | 431 | # binpkg will set DEBUG_SYMBOLS automatically if it detects the debug |
| 432 | # symbols in the packages dir. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 433 | pkgindex = binpkg.GrabLocalPackageIndex(packages_dir) |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 434 | with open(packages_file, "w", encoding="utf-8") as p: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 435 | pkgindex.Write(p) |