一垄青竹映陋室,半枝桃花衬碧窗.

Thursday, December 06, 2007

Some GDB features

1.Core dump

多线程如果dump,多为段错误,一般都涉及内存非法读写。可以这样处理,使用下面的命令打开系统开关,让其可以在死掉的时候生成core文件。
ulimit -c unlimited
这样的话死掉的时候就可以在当前目录看到core.pid(pid为进程号)的文件。接着使用gdb:
gdb ./JM ./core.pid
进去后,使用bt查看死掉时栈的情况,在使用frame命令。

===========================================

#include

int main()
{
char *temp = "Paras";

int i;
i=0;

temp[3]='F';

for (i =0 ; i < 5 ; i++ )
printf("%c\n", temp[i]);

return 0;
}

/*

[root@localhost core-dump]# ulimit -c unlimited

[root@localhost core-dump]# g++ -g -o testit testit.c
[root@localhost core-dump]# ./testit
段错误 (core dumped)
[root@localhost core-dump]# ./testit
段错误 (core dumped)
[root@localhost core-dump]# gdb testit core.3051
GNU gdb Red Hat Linux (6.5-15.fc6rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".


warning: Can't read pathname for load map: 输入/输出错误.
Reading symbols from /usr/lib/libstdc++.so.6...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Failed to read a valid object file image from memory.
Core was generated by `./testit'.
Program terminated with signal 11, Segmentation fault.
#0 0x08048489 in main () at testit.c:10
10 temp[3]='F';
(gdb) q
[root@localhost core-dump]#
*/

2."Program received signal SIG32, Real-time event 32."

Problem ===============================================

gdb) target remote 10.136.7.252:2345
Remote debugging using 10.136.7.252:2345
_start () at ../sysdeps/arm/elf/start.S:71
71 ../sysdeps/arm/elf/start.S: No such file or directory.
in ../sysdeps/arm/elf/start.S
Current language: auto; currently asm
(gdb) c
Continuing.

Program received signal SIG32, Real-time event 32.
0x0023ca84 in __pthread_sigsuspend (set=0xfffffffc) at ../linuxthreads/sysdeps/unix/sysv/linux/pt-sigsuspend.c:54
54 ../linuxthreads/sysdeps/unix/sysv/linux/pt-sigsuspend.c: No such file or directory.
in ../linuxthreads/sysdeps/unix/sysv/linux/pt-sigsuspend.c
Current language: auto; currently c
(gdb) c
Continuing.

Program received signal SIG32, Real-time event 32.
0x0023ca84 in __pthread_sigsuspend (set=0xfffffffc) at ../linuxthreads/sysdeps/unix/sysv/linux/pt-sigsuspend.c:54
54 in ../linuxthreads/sysdeps/unix/sysv/linux/pt-sigsuspend.c
(gdb) c
Continuing.

Solution==============================================

On Sun, 19 Sep 2004 11:08:42 -0700, Chris Germano <netslayer at hotmail.com> wrote:
> Does this GDB output look normal? I hear I need to recompile gcc and glibc
> with multithread support or something?
>
...
> warning: Unable to find dynamic linker breakpoint function.
> GDB will be unable to debug shared library initializers << ?
> and track explicitly loaded dynamic code. << ?

Perhaps the executable was statically compiled?

> Program received signal SIG32, Real-time event 32. << ?

I reckon SIG32 is the signal used by the threadslib to jump into the
scheduler every timeslice.
You can try the following in GDB, before starting the program:

handle SIG32 nostop noprint pass

That will prevent GDB from stopping when it receives the signal, it
won't print any occurrances
either, and the signal will be passed to program which is being debugged.

Robert

3.ulimit

ulimit is a shell builtin command used to show and set various restrictions on resource usage for a shell. Among the limitations that can be set you find, maximum file size, maximum core file size, maximum size of resident memory. Though the restrictions are shell-independent, the exact syntax depends on what shell you are running.

It's a good practice to set some of these limitations to prevent for instance a faulty shell script to start unlimited copies of it self or to prevent users on the system to start processes that run forever.

Warning:Typing ulimit may result in the output "unlimited". That is misleading, you may have limits in place as you can find out with ulimit -a

ls3523:~ # ulimit unlimited ls3523:~ # ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited pending signals (-i) 4091 max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 4091 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited

In general, the command ulimit -$ (where $ is some letter) will return the current value of the variable linked with $, while ulimit -$ NUMBER (again, where $ is some letter, and NUMBER is generally an integer, or "unlimited") will set that variable. Of special note is the -a option, which will display all limits for the system.

The -H and -S options are also special. -H will affect the "hard" limit, which is an absolute limit on a resource. -S will affect the "soft" limit. The major difference between the two is that hard limits can only be increased by root. Since the default for options is to affect both hard and soft limits, adding -S to a command will allow returning the variable to the previous value without root access.

Finally, all limits only apply to the thread in which they are set. For systemwide setting of limits, try /etc/security/limits.conf

Examples

  • ulimit -S -c 0 (Set the maximum filesize for core files to 0, as a soft limit. This will prevent core dumps from forming, while allowing returning to the previous limit.)
  • ulimit -f (Return the maximum file size.)
  • ulimit -u unlimited (Remove all limits on the number of simultaneous user processes.

6. Advanced gdb Features

  1. How do I examine memory?
  2. How do I see what is in the processor registers?
  3. How do I debug with a core file?
  4. How do I step through my code at the instruction level?
  5. How do I see the assembly code my program is running?

6.1 How do I examine memory? [top] [toc]

Use the x command to examine memory. The syntax for the x command is x/FMT ADDRESS. The FMT field is a count followed by a format letter and a size letter. There are many options here, use the help command 'help x' to see them all. The ADDRESS argument can either be a symbol name, such as a variable, or a memory address.

If we have char *s = "Hello World\n", some uses of the x command could be:

Examine the variable as a string:

(gdb) x/s s
0x8048434 <_io_stdin_used+4>: "Hello World\n"

Examine the variable as a character:

(gdb) x/c s
0x8048434 <_io_stdin_used+4>: 72 'H'

Examine the variable as 4 characters:

(gdb) x/4c s
0x8048434 <_io_stdin_used+4>: 72 'H' 101 'e' 108 'l' 108 'l'

Examine the first 32 bits of the variable:

(gdb) x/t s
0x8048434 <_io_stdin_used+4>: 01101100011011000110010101001000

Examine the first 24 bytes of the variable in hex:

(gdb) x/3x s
0x8048434 <_io_stdin_used+4>: 0x6c6c6548 0x6f57206f 0x0a646c72

6.2 How do I see what is in the processor registers? [top] [toc]

Use the info registers command. The output of this command depends on the hardware architecture. The following is part of the output on an intel machine:

(gdb) info registers
eax 0x40123460 1074934880
ecx 0x1 1
edx 0x80483c0 134513600
ebx 0x40124bf4 1074940916
esp 0xbffffa74 0xbffffa74
ebp 0xbffffa8c 0xbffffa8c
esi 0x400165e4 1073833444
...

6.3 How do I debug with a core file? [top] [toc]

When your program segfaults and leaves a core dump file, you can use gdb to look at the program state when it crashed. Use the core command to load a core file. The argument to the core command is the filename of the core dump file, which is usually "core", making the full command core core.

prompt > myprogram
Segmentation fault (core dumped)
prompt > gdb myprogram
...
(gdb) core core
...

6.4 How do I step through my code at the instruction level? [top] [toc]

There are two commands, nexti and stepi, that work similar to next and step. See the usage of those commands for an idea of how to use these two.


6.5 How do I see the assembly code my program is running? [top] [toc]

Use the disassemble command. The argument to this command is a memory address. Here is an example of the disassembly for the main function of a simple program on an intel machine:

(gdb) disassemble main
Dump of assembler code for function main:
0x80483c0
: push %ebp
0x80483c1 : mov %esp,%ebp
0x80483c3 : sub $0x18,%esp
0x80483c6 : movl $0x0,0xfffffffc(%ebp)
0x80483cd : mov 0xfffffffc(%ebp),%eax
0x80483d0 : movb $0x7,(%eax)
0x80483d3 : xor %eax,%eax
0x80483d5 : jmp 0x80483d7
0x80483d7 : leave
0x80483d8 : ret
End of assembler dump.



0 Comments:

Post a Comment

<< Home