Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run gdb with LD_PRELOAD?

Tags:

qemu

gdb

I have a program using LD_PRELOAD. The program should be run like this, "LD_PRELOAD=/path/to/libfoo.so qemu -U LD_PRELOAD a.out", if without gdb.

Here are what I did while running gdb.

(gdb) set environment LD_PRELOAD=/nfs_home/chenwj/tools/lib/libdbo.so

(gdb) file /nfs_home/chenwj/tools/bin/qemu-i386

(gdb) r -U LD_PRELOAD bzip2_base.i386-m32-gcc44-annotated input.source 1

But gdb gave me the error below

Starting program: /nfs_home/chenwj/tools/bin/qemu-i386 -U LD_PRELOAD bzip2_base.i386-m32-gcc44-annotated input.source 1

bash: open "/bin/bash" failed: Permission denied

During startup program exited with code 66.

Any sugguestion appreciated.

Regards, chenwj

like image 920
chenwj Avatar asked Jan 16 '11 04:01

chenwj


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).

What command is used to start up GDB?

Use the run command to start your program under gdb.

What is LD_ PRELOAD?

LD_PRELOAD is an optional environmental variable containing one or more paths to shared libraries, or shared objects, that the loader will load before any other shared library including the C runtime library (libc.so) This is called preloading a library.


1 Answers

GDB does not invoke your executable directly. Instead, it does

bash -c '/nfs_home/chenwj/tools/bin/qemu-i386  -U LD_PRELOAD bzip2_base.i386-m32-gcc44-annotated input.source 1'

This is done so that bash takes care of I/O redirection (which you are not using).

My guess is that /bin/bash doesn't work when LD_PRELOAD=libdbo.so is in effect, though I don't understand the exact nature of failure.

One way to work around this problem is to create a wrapper executable, implementing C equivalent of this:

export LD_PRELOAD=/nfs_home/chenwj/tools/lib/libdbo.so
exec /nfs_home/chenwj/tools/bin/qemu-i386 "$@"

and debug that executable (without setting LD_PRELOAD). You'll see an extra SIGTRAP when the wrapper execve()s the wrapped qemu-i386, which you should ignore and continue.

like image 58
Employed Russian Avatar answered Oct 21 '22 10:10

Employed Russian