Hello Nixers!

When debugging packaged apps, I often find myself looking for debugging
variables used by the application that would allow me to explain some
behavior or to tell the application about a path or something.

Unfortunately, strace(1) doesn't help to look at getenv(3) calls, and
gdb isn't very helpful in the absence of glibc debugging symbols.

So I've come up with the ultimate tool (attached).  Just run:

  $ LD_PRELOAD=./getenv.so the-program

And it will print the list of getenv(3) calls and their results.

How great!

:-)

Ludo'.

/* Display looked up environment variables.

   Compile as shown at the bottom of this file.

   Run with:

     $ LD_PRELOAD=./getenv.so some-program

   Written by Ludovic Courtès <l...@gnu.org>.  Hereby placed under the
   LGPLv3+.
 */

#include <stdio.h>
#include <gnu/lib-names.h>
#include <dlfcn.h>

static void *glibc;
static char * (*glibc_getenv) (const char *);

static void load_glibc (void) __attribute__ ((__constructor__));

static void
load_glibc (void)
{
  glibc = dlopen (LIBC_SO, RTLD_LAZY);
  glibc_getenv = dlsym (glibc, "getenv");
}

char *
getenv (const char *name)
{
  char *result;

  result = glibc_getenv (name);

  printf ("getenv (\"%s\") = %s%s%s\n", name,
          result ? "\"" : "",
          result ? result : "NULL",
          result ? "\"" : "");

  return result;
}

/*
   Local Variables:
   compile-command: "gcc -Wall -shared -fPIC -o getenv.so getenv.c"
   End:
 */

Attachment: pgpD625aT0FX2.pgp
Description: PGP signature

_______________________________________________
nix-dev mailing list
nix-dev@cs.uu.nl
https://mail.cs.uu.nl/mailman/listinfo/nix-dev

Reply via email to