How to use gdb with LD_PRELOAD

CLinuxGccGdbX86 64

C Problem Overview


I run a program with LD_PRELOADing a specific library. Like this.

LD_PRELOAD=./my.so ./my_program

How do I run this program with gdb?

C Solutions


Solution 1 - C

Do the following.

gdb your_program

(gdb) set environment LD_PRELOAD ./yourso.so
(gdb) start

Solution 2 - C

Posting because we ran into a case where set environment didn't work:

From GDB documentation:

> set exec-wrapper wrapper show exec-wrapper unset exec-wrapper

> When ‘exec-wrapper’ is set, the specified wrapper is used to launch programs for debugging. gdb starts your program with a shell command of the form exec wrapper program. Quoting is added to program and its arguments, but not to wrapper, so you should add quotes if appropriate for your shell. The wrapper runs until it executes your program, and then gdb takes control.

> You can use any program that eventually calls execve with its arguments as a wrapper. Several standard Unix utilities do this, e.g. env and nohup. Any Unix shell script ending with exec "$@" will also work.

> For example, you can use env to pass an environment variable to the debugged program, without setting the variable in your shell's environment:

> (gdb) set exec-wrapper env 'LD_PRELOAD=libtest.so' > (gdb) run

Solution 3 - C

Here is a way to run everything (with arguments and environment) as one command:

Example:

gdb --args env LD_PRELOAD=/usr/local/lib/libstderred.so ls -l

The keen observer will notice that env serves here as an exec wrapper (like Alexey Romanov's answer).

Solution 4 - C

You can supply env as an exec-wrapper on the command line using the -iex flag:

gdb -iex "set exec-wrapper env LD_PRELOAD=./my.so" ./my_program

Solution 5 - C

I am using gdbserver with VS Code, the simplest way is launching your program wrapped in a shell:

gdbserver :8888 sh -c 'LD_PRELOAD=/libtest.so your_prog'

Solution 6 - C

You can basically do it the same way, just add gdb before the program name:

LD_PRELOAD=./my.so gdb ./my.program

You can check the environment variables using:

(gdb) show environment LD_PRELOAD

In the rare case you actually need to change it inside gdb, e.g. when debugging a dlopen(), you ca do that:

(gdb) set environment LD_PRELOAD ./my.so

Oh, wait, it doesn't work for me with gdb 7.6.2! The library doesn't get loaded, that means none of the answer here are entirely correct, at least with current tools.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMetallicPriestView Question on Stackoverflow
Solution 1 - CMetallicPriestView Answer on Stackoverflow
Solution 2 - CAlexey RomanovView Answer on Stackoverflow
Solution 3 - Cuser2394284View Answer on Stackoverflow
Solution 4 - CecatmurView Answer on Stackoverflow
Solution 5 - CBubbleQuoteView Answer on Stackoverflow
Solution 6 - CPavel ŠimerdaView Answer on Stackoverflow