From 02bce71cab92f5d95734599d65837cb2fbcfa373 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Mon, 25 Oct 2021 00:53:12 +0200 Subject: [PATCH] mtrace: init at 2.33-50 `mtrace(1)` is a small Perl script that interprets and provides human-readable output for `malloc(3)` traces. Even though this is actually part of `glibc` itself I decided to place this into its own package. The main reason for this is that this script has a runtime dependency on Perl which would complicate `stdenv` bootstrapping since we'd have to compile another Perl that doesn't depend on the bootstrap tools that is used as runtime dependency for the stage2 glibc. Since this is only a dev/debugging tool, splitting this up seemed like a reasonable choice to me. On a leaking C program, this can be used like this: $ env MALLOC_TRACE=$(pwd)/trace ./a.out $ ./result/bin/mtrace ./trace Memory not freed: ----------------- Address Size Caller 0x0000000001875690 0x4 at 0x401151 Closes #141924 --- pkgs/development/libraries/glibc/common.nix | 2 +- pkgs/development/libraries/glibc/mtrace.nix | 38 +++++++++++++++++++++ pkgs/top-level/all-packages.nix | 2 ++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 pkgs/development/libraries/glibc/mtrace.nix diff --git a/pkgs/development/libraries/glibc/common.nix b/pkgs/development/libraries/glibc/common.nix index a715ba752ec1..580f989c53cc 100644 --- a/pkgs/development/libraries/glibc/common.nix +++ b/pkgs/development/libraries/glibc/common.nix @@ -198,7 +198,7 @@ stdenv.mkDerivation ({ BASH_SHELL = "/bin/sh"; # Used by libgcc, elf-header, and others to determine ABI - passthru = { inherit version; }; + passthru = { inherit version; minorRelease = version; }; } // (removeAttrs args [ "withLinuxHeaders" "withGd" ]) // diff --git a/pkgs/development/libraries/glibc/mtrace.nix b/pkgs/development/libraries/glibc/mtrace.nix new file mode 100644 index 000000000000..fed24aff2796 --- /dev/null +++ b/pkgs/development/libraries/glibc/mtrace.nix @@ -0,0 +1,38 @@ +{ glibc, perl }: + +# Small wrapper which only exposes `mtrace(3)` from `glibc`. This can't be placed +# into `glibc` itself because it depends on Perl which would mean that the final +# `glibc` inside a stdenv bootstrap has a dependency `glibc -> perl -> bootstrap tools`, +# so this is now in its own package that isn't used for bootstrapping. +# +# `glibc` needs to be overridden here because it's still needed to `./configure` the source in order +# to have a build environment where we can call the needed make target. + +glibc.overrideAttrs ({ meta ? {}, ... }: { + pname = "glibc-mtrace"; + + buildPhase = '' + runHook preBuild + + mkdir malloc + make -C ../glibc-${glibc.minorRelease}/malloc objdir=`pwd` `pwd`/malloc/mtrace; + + runHook postBuild + ''; + + installPhase = '' + mkdir -p $out/bin + mv malloc/mtrace $out/bin/ + ''; + + # Perl interpreter used for `mtrace`. + buildInputs = [ perl ]; + + # Reset a few things declared by `pkgs.glibc`. + outputs = [ "out" ]; + separateDebugInfo = false; + + meta = meta // { + description = "Perl script used to interpret and provide human readable output of the trace log contained in the file mtracedata, whose contents were produced by mtrace(3)."; + }; +}) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index a79aa339c190..bf5250a77824 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -16248,6 +16248,8 @@ with pkgs; stdenv = gccStdenv; # doesn't compile without gcc }; + mtrace = callPackage ../development/libraries/glibc/mtrace.nix { }; + # Provided by libc on Operating Systems that use the Extensible Linker Format. elf-header = if stdenv.hostPlatform.parsed.kernel.execFormat.name == "elf"