Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gdb with LD_PRELOAD

Tags:

c

linux

gcc

x86-64

gdb

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?

like image 374
MetallicPriest Avatar asked May 04 '12 11:05

MetallicPriest


People also ask

How do I run a command in GDB?

Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).

How does GDB work in Linux?

GDB allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line. GDB uses a simple command line interface.

What command is used to start up GDB?

Use the run command to start your program under gdb.


2 Answers

Do the following.

gdb your_program  (gdb) set environment LD_PRELOAD ./yourso.so (gdb) start 
like image 157
MetallicPriest Avatar answered Sep 19 '22 13:09

MetallicPriest


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 
like image 33
Alexey Romanov Avatar answered Sep 19 '22 13:09

Alexey Romanov