Issue Observed while using GDB - c

I am trying to debug my application which use one static builded library.
I want to set break points in my library so i tried to set it using below command :
break TS.cpp:600(FIle name:line no)
but it says
No source file named TS.cpp.
Make breakpoint pending on future shared library load?(y or [n])
so I presses y here (I came to know after browsing internet) but after pressing y gdb is not stopping at my break point and it completed executing program.
Why GDB is not stopped at my break point??
Any input is highly appreciated.

No source file named TS.cpp
This means one of two things:
either the file TS.cpp was not compiled with -g (or equivalently TS.o has been stripped), or
the file TS.o was not linked into the application.
Since you are seeing prints from that source, it's a safe bet that #1 is the actual root cause.
info sources command shows only my application.c and not the files of my library
That is another confirmation that #1 is the root cause.

The problem in your case is with source mapping. It normally happens when application is compiled at some other machine and you are debugging it on some other machine where source location is different.
You can specify source path using directory command of gdb. e.g. if your sources are in /home/taimoor/testApp/src, you can do following:
(gdb) directory /home/taimoor/testApp/src

Related

GDB attach problems with /sysdeps/unix/sysv/linux/

Hi I had a little question.
I've been trying for hours to find a solution but I just do not get on.
I would like to use the gdb.attach function via pwntools.
But whenever I call this function also opens the gdb with the executable and also waits pwntools then on the gdb gives a feedback.
But in the GDB window goes then below only:
0x00007fbf014828be in __GI___libc_read (fd=0x0, buf=0x56139a7ee370, nbytes=0x1) at ../sysdeps/unix/sysv/linux/read.c:26
26 ../sysdeps/unix/sysv/linux/read.c: File or directory not found.
Breakpoint 1 at 0x7fbf0145f140: file ../sysdeps/unix/syscall-template.S, line 120.
When I use the gdb.debug function I have no problems but unfortunately I also need the gdb.attach function.
This is the test code I use:
from pwn import*
p = process('bash')
gdb.attach(p,
'''
directory /home/pentester/Downloads/glibc-2.32/
set follow-fork-mode child
break execve
continue
''')
p.interactiv()
I have already downloaded the glibc sources but I don't know where to put them exactly.
I had them in the sources in the gdbinit reingepackt but unfortunately the problem is still there :(
Does one of you maybe have an idea how I can get the whole thing running?
Edit:
What I forgot to say is that the debugger seems to hang and pwntools searches in vain for the gdb.

GDB - read.c: No such file or directory [duplicate]

I wanted to debug printf function, so when I step inside the printf function (gdb debugger) it showed me this:
__printf (format=0x80484d0 " my name is Adam") at printf.c:28
28 printf.c: No such file or directory.
What is the meaning of this?
And when I again started step then there are a lot more statements like this.
Please help me to understand this.
I think it's pretty clear. There is a place where the gdb expects the source code to be, so download glibc's source code and put it there. I think the error message contains the full path.
If it's a linux distro it's fairly simple in fact because usually source packages are shipped too. Otherwise you need to find the source code yourself, note that it MUST be exactly the same that was used to compile the c library components, not just the same version because distributors often make changes to the sources.
Well, for the debugger to show you the code that was compiled into the binaries you're using, you need the original code somewhere.
You don't seem to have that, so your debugger can't find it.
Notice that you usually do not want to debug the source code of your std library functions, but only the way they are being called. For that, the usual "debug symbol" packages of your operating systems are optimal.
As others have answered, GDB was unable to find the source file.
For the C runtime libraries, Linux distributions may provide a debuginfo RPM that you can install, which may allow GDB to view the files. For example:
$ yum search glibc-debuginfo
...
glibc-debuginfo.x86_64 : Debug information for package glibc
glibc-debuginfo-common.x86_64 : Debug information for package glibc
...
The glibc package and the glibc-debuginfo are a matched pair. There is no explicit dependency, but glibc-debuginfo package won't work unless it is matched with the same version of glibc.
If you have the sources unpacked somewhere, but not where GDB is expecting them to be, you can attempt to use either the directory or the set substitute-path command to let GDB know where the sources are.
The directory command tells GDB to prepend a prefix ahead of any source file path it is attempting to find. For example, if the source tree is actually located under the /tmp, you could use:
(gdb) directory /tmp
The set substitute-path command is used to tell GDB to replace a matching prefix in a source file path with a different path prefix. For example, if the compiled source file was in /build/path/source.c, but in debugging the source file is actually in /usr/home/alice/release-1.1/source.c, then you could use:
(gdb) set substitute-path /build/path /usr/home/alice/release-1.1
The command assumes that you are only specifying a complete path names, so it won't perform the substitution on /build/pathological/source.c.

How to debug standard c library functions like printf?

I wanted to debug printf function, so when I step inside the printf function (gdb debugger) it showed me this:
__printf (format=0x80484d0 " my name is Adam") at printf.c:28
28 printf.c: No such file or directory.
What is the meaning of this?
And when I again started step then there are a lot more statements like this.
Please help me to understand this.
I think it's pretty clear. There is a place where the gdb expects the source code to be, so download glibc's source code and put it there. I think the error message contains the full path.
If it's a linux distro it's fairly simple in fact because usually source packages are shipped too. Otherwise you need to find the source code yourself, note that it MUST be exactly the same that was used to compile the c library components, not just the same version because distributors often make changes to the sources.
Well, for the debugger to show you the code that was compiled into the binaries you're using, you need the original code somewhere.
You don't seem to have that, so your debugger can't find it.
Notice that you usually do not want to debug the source code of your std library functions, but only the way they are being called. For that, the usual "debug symbol" packages of your operating systems are optimal.
As others have answered, GDB was unable to find the source file.
For the C runtime libraries, Linux distributions may provide a debuginfo RPM that you can install, which may allow GDB to view the files. For example:
$ yum search glibc-debuginfo
...
glibc-debuginfo.x86_64 : Debug information for package glibc
glibc-debuginfo-common.x86_64 : Debug information for package glibc
...
The glibc package and the glibc-debuginfo are a matched pair. There is no explicit dependency, but glibc-debuginfo package won't work unless it is matched with the same version of glibc.
If you have the sources unpacked somewhere, but not where GDB is expecting them to be, you can attempt to use either the directory or the set substitute-path command to let GDB know where the sources are.
The directory command tells GDB to prepend a prefix ahead of any source file path it is attempting to find. For example, if the source tree is actually located under the /tmp, you could use:
(gdb) directory /tmp
The set substitute-path command is used to tell GDB to replace a matching prefix in a source file path with a different path prefix. For example, if the compiled source file was in /build/path/source.c, but in debugging the source file is actually in /usr/home/alice/release-1.1/source.c, then you could use:
(gdb) set substitute-path /build/path /usr/home/alice/release-1.1
The command assumes that you are only specifying a complete path names, so it won't perform the substitution on /build/pathological/source.c.

Finding virtual memory address of variable on osx

Consider the following code in mono/domain.c:
static MonoDomain *mono_root_domain = NULL;
...
MonoDomain* mono_get_root_domain (void)
{
return mono_root_domain;
}
My task is to read the struct data pointed by the mono_root_domain pointer in runtime from another process. (Attaching, reading, locating dylibs, etc. from this other process is solved already)
Looking into the generated libmono dylib I can find the corresponding symbol:
This symbol points to the address of 0x2621A8 which in the local relocation section (__DATA, __bss):
This points to the address of 0x1A7690 (__TEXT, __symbol_stub):
The target is
so 0x1A7DF8 (__TEXT, __stub_helper):
At this point I am completely lost of how to retrieve the actual pointer to the MonoDomain struct. Any help is appreciated.
For security reasons and to prevent buffer overflow attacks and other exploits, you can't know that, because of a security measure called PIE or ASLR (address space layout randomization). However, this can be disabled for debugging purposes. LLDB and GDB do/did it in order to debug executables. The way this can be done with a CLI app is as follows:
Copy or download this python script from GitHub
https://github.com/thlorenz/chromium-build/blob/master/mac/change_mach_o_flags.py
Save the python script, for example, next to your executable
If so, open Terminal and cd to where your executable is
enter chmod +x ./change_mach_o_flags.py to make the script executable
enter ./change_mach_o_flags.py --no-pie ./YourExecutable
Now the addresses of your executable should not be randomized anymore. Because of that, to calculate the addresses of your static / global variables is possible. To do that, do the following in Terminal (I am assuming you are using a 64-bit machine):
otool -v -l ./YourExecutable | open -f(this will generate a file text with the commands inside your executable of how to layout DATA, TEXT, etc. in memory)
Look for the section you are interested in. Look at the addr field. If it contains let's say 0x0000000100001020 then the variable will be placed exactly there with ASLR disabled.
I am not sure if this works with dylibs but you can try it. Now I ran out of time, but I can try at home and see if this is doable with dylibs.

Merge C program and VHDL bitstream via "make" (i.e. using a Makefile)

I am trying to test the VHDL AVR8 soft processor found on Gadget Factory on a Digilent Nexys II (Spartan 3E) Development board. The project includes a Makefile for compiling a C (or other) software program and merging it with the FPGA bitstream so there is no need to resynthesize the HDL with every iteration of the software.
When I execute 'make' I get the following error associated with data2mem:
Merging Verilog Mem file with Xilinx bitstream: main.bit
data2mem -bm bin/top_avr_core_v8_bd.bmm -bt bin/top_avr_core_v8.bit -bd main.mem -o b main.bit
process_begin: CreateProcess(NULL, data2mem -bm bin/top_avr_core_v8_bd.bmm -bt bin/top_avr_core_v8.bit -bd main.mem -o b main.bit, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [main.bit] Error 2
I am executing 'make' in the same directory containing the VHDL project files, and I even have a blank 'main.bit' file in the directory.
Does anyone have any ideas about what's going on here? Does my blank 'main.bit' file need to be formatted a certain way or placed in a different location?
The following is a link to my Makefile:
Makefile
Other information to note: I'm new to using Makefiles in general, let alone for the specific purpose of merging software with an FPGA bitstream file. Also, I am attempting this on a Windows 7 machine in command prompt.
Thanks in advance.
Edit: Here's a link to the AVR8 soft processor on Gadget Factory, and here's the AVR8 source.
Offhand I'd say make cannot invoke the data2mem program. Do you have such a program on your system? Is the directory containing it in your PATH variable? Does it run properly? For example, can you type in that command line yourself at the command prompt and have it work properly?
When you say a "blank" main.bit, I assume you mean a "fully populated except for the memory that I want to put the program into" main.bit... otherwise nothings going to work!
It sounds like you do not have data2mem on your path - are you sure you are running your makefile from a command window/shell which includes the xilinx paths?
On Windows, there is a specific icon for this. Alternatively you can open any old command prompt and run the settings32.bat (or settings64.bat) file from within the Xilinx install folder to get set up. Or on linux, you can source the appropriate .sh/.csh file in your shell.

Resources