Force gdb to load local shared library iso target library - c++

I'am using gdbserver and gdb on host to debug arch ARM application linked to shared library.
I have a copy of the Library on host with symbols and I want gdb to load the local library instead of the remote one.
Changing the sysroot from target: to local path cannot solve the issue. Always gdb load Library from target.
My question is how can I force gdb to use local library iso the remote one ?

Related

How to set sysroot for GDB

Scenario
Dev machine running Ubuntu 20.04 and QtCreator. Qt libs are installed to /home/mark/Qt/6.2.0/gcc_64/lib.
Target machine running Ubuntu 20.04. Qt libs are installed to /home/user/Qt/6.2.0/gcc_64/lib.
Issue
When debugging a remote application from QtCreator I get:
Could not load shared library symbols for 8 libraries, e.g. /home/user/Qt/6.2.0/gcc_64/lib/libQt6Gui.so.6.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
Setup
In Tools > Options > Debugger > GDB > Additional Startup Commands I wrote:
set sysroot /home/user/Qt/6.2.0/gcc_64/
set solib-search-path /home/user/Qt/6.2.0/gcc_64/
Questions
I read somewhere that to speed up the debugging I need to set sysroot to the local libraries (i.e. /home/mark/... instead of /home/user/...). Which is the correct path?
Is it correct to set sysroot to the parent directory of the lib folder that contains the Qt libraries?
where should I run the info sharedlibrary command?
Which is the correct path?
The one where libraries can be found during debugging, i.e. the path on the development machine.
Is it correct to set sysroot
The sysroot should be used when you have a complete set of target libraries (including all system ones).
That is, if you had the complete mirror of target libraries under e.g. /home/mark/target/, then you would use set sysroot /home/mark/target.
Since you don't have such a directory, you should not use sysroot at all.
Instead, you should tell GDB that it can find libraries in /home/mark/Qt/6.2.0/gcc_64/lib with set solib-search-path /home/mark/Qt/6.2.0/gcc_64/lib.
where should I run the info sharedlibrary command?
At the (gdb) prompt.

GDB failed to load so files and errors No such file or directory

I am loading a core file into GDB inside a docker container with alpine linux.
Initially my gdb complained about binary not found, then i followed this discussion gdb During startup program exited with code 127 and installed libc6-compat, even though I have a 64-bit linux and my interpreter for the binary is 64-bit [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
That actually worked but now i get the same error for the .so files. I can see that .so files are in the path set on solib-search-path but gdb did not load any of the libraries and complains No such file or directory.
binary runs on a production host where the libraries are in the path
I am running the binary on a development host by setting the solib-search-path
ldd binary says libraries not found which is expected since this is a development host
binary runs on a production host where the libraries are in the path I am running the binary on a development host by setting the solib-search-path
There is your problem right here.
The solib-search-path is the path where GDB will look for libraries.
In no way does that setting affect the binary itself, which uses either standard system library path, compiled-in DT_RPATH, or LD_LIBRARY_PATH environment variable to search for shared libraries.
Usually, solib-search-path should only be set when you are analyzing a core dump that came from some other host (GDB needs access to the exact libraries that were used when the core dump was generated). Setting solib-search-path while running a local binary should never be ncessary -- GDB can automatically deduce where the libraries are by asking the runtime loader where they are.
ldd binary says libraries not found which is expected since this is a development host
If ldd says that, then of course you will not be able to run the binary (inside of outside GDB).

GDB: Error while remotely loading shared libraries

I am using X-server on Linux tutnix 3.13.0-30-generic #54-Ubuntu i686 i686 GNU/Linux .
On X-server i am using schroot to change the root and after that I run my application on it.
Now the problem is I am trying to run gdbserver(version 6.7.1) on X-server(say it remote machine) and gdb(version 7.7) on Ubuntu machine(local machine or the machine where X-server is running), but its not working.
This is what I tried so far:-
executed below commands on remote machine
MY_Application &
gdbserver localhost:8888 --attach process_id
executed below commands on local machine
gdb ./MY_Application
(gdb)target remote :8888
Now on local machine gdb is showing this message
Could not load shared library symbols for 20 libraries, e.g. /usr/lib/libqt.so.3.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
As far as understand it, gdb is not able to load the shared libraries and there are 20 of them. As a result of this I am not able to find the symbols present in these shared libraries.
I tried to load the symbols from library files on local machine using symbol-file command on gdb but that leads to address mismatch
So I used the below command
(gdb)set sysroot remote:/
but it is again showing an error message
Could not open 'remote:/usr/local/davis/lib/usr/lib/libqt.so.3' as an executable file: Function not implemented
Can anyone please tell me what am I doing wrong here? If there is an alternate solution to load the symbols from shared libraries please suggest.
P.S. Both the MY_Application binaries are same.
Finally i found a solution for my problem.
The problem was in step 2 shown above. Somehow gdb was not loading all the symbols and PATH to shared libraries. Although PATH shown with ldd command was accurate. But still it was not working for me if I simply run the gdb ./MY_Application
So I executed one more command after this i.e.
file MY_Application
and also copied library file from
/srv/chroot/loc/usr/local/davis/lib/ to /usr/local/davis/lib/ after this I connect it to remote server and now it is working fine.

Is it possible to somehow provide a remote path to the libraries used in a c++ program while compiling and linking?

Usually when I use a c++ library I give the header file for the library using the -I attribute while compiling and link the library using -l attribute. But if the libraries are on another remote computer, and I do not want to copy or install it on my local host, is there some way to reference them on your local host?
Operating system:
Localhost Host: Ubuntu
Remote system: Unix Server

Static linked GDB cannot use HostName in target remote

The GDB is built on RHEL-4.7. I run the GDB on RHEL-6.3. If the GDB is dynamically linked, everything works OK. If the GDB is statically linked, I got
(gdb) target remote :2107
localhost: unknown host
:2107: No such file or directory.
When GDB is built statically, we got a warning message:
warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
I also searched with google, it seems that gethostbyname depends on the glibc library. I tried to install the compatible glibc library on RHEL-6.3. But there's no luck. Any hint?