Coder Perfect

What is the best way to utilize gdb with LD PRELOAD?


I’m running a program that uses LD PRELOAD to load a certain library. This is something I like.

LD_PRELOAD=./ ./my_program

How can I use gdb to run this program?

Asked by MetallicPriest

Solution #1

Do the following.

gdb your_program

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

Answered by MetallicPriest

Solution #2

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

From GDB documentation:

Answered by Alexey Romanov

Solution #3

Here’s how to run everything in one command (with parameters and environment):


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

The astute observer will see that, like Alexey Romanov’s answer, env is used as an exec wrapper here.

Answered by user2394284

Solution #4

Using the -iex switch, you can pass env as an exec-wrapper on the command line:

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

Answered by ecatmur

Solution #5

I’m using gdbserver with VS Code, and the simplest way to get started is to wrap your program in a shell:

gdbserver :8888 sh -c 'LD_PRELOAD=/ your_prog'

Answered by BubbleQuote

Post is based on