From c8f57dd4e2342a2694597be5184b6dfe20d6ff76 Mon Sep 17 00:00:00 2001 From: Tobias Ribizel Date: Fri, 9 Sep 2022 09:00:42 +0200 Subject: [PATCH] Add `gdb` pretty-printers for simple types (#11499) This adds `gdb` pretty printers for `rmm::device_uvector`, `thrust::*_vector`, `thrust::device_reference` and `cudf::*_span`. The implementation is based on https://github.com/NVIDIA/thrust/pull/1631. I will probably backport the thrust-specific changes to there as well, but since the location of the thrust source is not fixed, I'd prefer having all types in a self-contained file. Authors: - Tobias Ribizel (https://github.com/upsj) Approvers: - Bradley Dice (https://github.com/bdice) - Karthikeyan (https://github.com/karthikeyann) URL: https://github.com/rapidsai/cudf/pull/11499 --- cpp/CMakeLists.txt | 5 ++ cpp/scripts/gdb-pretty-printers.py | 84 +++++++++++++++++++++++++++++ cpp/scripts/load-pretty-printers.in | 3 ++ 3 files changed, 92 insertions(+) create mode 100644 cpp/scripts/gdb-pretty-printers.py create mode 100644 cpp/scripts/load-pretty-printers.in diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 09a60836fef..69394a34624 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -749,6 +749,11 @@ if(CUDF_BUILD_BENCHMARKS) add_subdirectory(benchmarks) endif() +# build pretty-printer load script +if(Thrust_SOURCE_DIR AND rmm_SOURCE_DIR) + configure_file(scripts/load-pretty-printers.in load-pretty-printers @ONLY) +endif() + # ################################################################################################## # * install targets ------------------------------------------------------------------------------- rapids_cmake_install_lib_dir(lib_dir) diff --git a/cpp/scripts/gdb-pretty-printers.py b/cpp/scripts/gdb-pretty-printers.py new file mode 100644 index 00000000000..ebb56a8c9e6 --- /dev/null +++ b/cpp/scripts/gdb-pretty-printers.py @@ -0,0 +1,84 @@ +# Copyright (c) 2022, NVIDIA CORPORATION. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import gdb + +global_locals = locals() +if not all( + name in global_locals + for name in ( + "HostIterator", + "DeviceIterator", + "is_template_type_not_alias", + "template_match", + ) +): + raise NameError( + "This file expects the RMM pretty-printers to be loaded already. " + "Either load them manually, or use the generated load-pretty-printers " + "script in the build directory" + ) + + +class CudfHostSpanPrinter(gdb.printing.PrettyPrinter): + """Print a cudf::host_span""" + + def __init__(self, val): + self.val = val + self.pointer = val["_data"] + self.size = int(val["_size"]) + + def children(self): + return HostIterator(self.pointer, self.size) + + def to_string(self): + return f"{self.val.type} of length {self.size} at {hex(self.pointer)}" + + def display_hint(self): + return "array" + + +class CudfDeviceSpanPrinter(gdb.printing.PrettyPrinter): + """Print a cudf::device_span""" + + def __init__(self, val): + self.val = val + self.pointer = val["_data"] + self.size = int(val["_size"]) + + def children(self): + return DeviceIterator(self.pointer, self.size) + + def to_string(self): + return f"{self.val.type} of length {self.size} at {hex(self.pointer)}" + + def display_hint(self): + return "array" + + +def lookup_cudf_type(val): + if not str(val.type.unqualified()).startswith("cudf::"): + return None + suffix = str(val.type.unqualified())[6:] + if not is_template_type_not_alias(suffix): + return None + if template_match(suffix, "host_span"): + return CudfHostSpanPrinter(val) + if template_match(suffix, "device_span"): + return CudfDeviceSpanPrinter(val) + return None + + +gdb.pretty_printers.append(lookup_cudf_type) diff --git a/cpp/scripts/load-pretty-printers.in b/cpp/scripts/load-pretty-printers.in new file mode 100644 index 00000000000..4c00384c878 --- /dev/null +++ b/cpp/scripts/load-pretty-printers.in @@ -0,0 +1,3 @@ +source @Thrust_SOURCE_DIR@/scripts/gdb-pretty-printers.py +source @rmm_SOURCE_DIR@/scripts/gdb-pretty-printers.py +source @PROJECT_SOURCE_DIR@/scripts/gdb-pretty-printers.py