Core dump Debugging and multithreaded debugging

Source: Internet
Author: User

Debugging in the time we encountered a lot of problems, in fact, related to functional logic, there is due to some of the details caused by the fault--segmentation fault. Here is the process of our development in the test and in the process of the problem solving skills or methods to summarize, many people are already familiar with.

First we need to know a command:

ulimit Command Description :

Function Description: Controls the resources of the shell program.

Syntax: ulimit [-ahs][-c <core file Upper limit >][-d < data section size >][-f < file size >][-m < memory size >][-n < files >][-p < Buffer size >][-s < stacking size >][-t <CP time >][-u < program number >][-V < virtual memory size

Supplemental Note: Ulimit is a shell-built directive that can be used to control the resources of shell execution programs.

Parameters

-a displays the current resource constraint settings (A=all).

-c <core File upper limit > set the maximum value of core file, the unit is block.

-N < number of files > Specify the maximum number of files that can be opened at the same time.

-s < stacking size > Specifies the upper bound of the stack in kilobytes.

-S sets the flexibility limit for resources.

-U < number of programs > The maximum number of programs that users can open.

Ulimit-a is used to display current user process restrictions.

Ulimit–c can view the core file generation does not have a switch, if the result is 0, indicates that the feature is turned off, the core file will not be generated.

Linux for each user, the system limits its maximum number of processes. To improve performance, you can set the maximum number of processes for each Linux user based on device resources, and I set the maximum number of processes for a Linux user to 10,000:

Ulimit-u 10000

Ulimit-n XX modifies the number of files that can be opened per process, and the default value is 1024.

Ulimit-n 4096 increases the number of files that can be opened per process to 4096, and defaults to 1024

Some important settings for other suggestions that are set to unrestricted (unlimited) are:

Length of data segment: Ulimit-d Unlimited

Maximum memory size: Ulimit-m Unlimited

Stack size: Ulimit-s Unlimited

CPU Time: Ulimit-t Unlimited

Virtual Memory: Ulimit-v Unlimited

Sometimes it is necessary to adjust the stack size parameter of the ulimit to unlimited infinite, when using ulimit-s Unlimited can only be effective at the time of the shell, re-opened a shell is ineffective and then have to/etc/profile To add Ulimit-s Unlimited on the last side, Sudo source/etc/profile makes the modified file effective.

PS: If you encounter a similar error hint

Ulimit:max User Processes:cannot Modify limit: Operation not allowed

Ulimit:open Files:cannot Modify limit: Operations not allowed

Why the root user is OK. Ordinary users will encounter such a problem.

Take a look at/etc/security/limits.conf and you'll probably understand.

Linux has a default ulimit limit for users, and this file can configure the user's hard configuration and soft configuration, and hard configuration is the upper limit. A modification exceeding the upper limit would be an error such as "Disallowed operation".

In limits. Conf Plus

* Soft Noproc 10240

* Hard Noproc 10240

* Soft Nofile 10240

* Hard Nofile 10240

is to limit the maximum number of threads and files for any user to 10240. 、

After understanding the Ulimit command, we will use the important function of this command to generate core Dump.

L What is core Dump?

Core means memory, dump means to throw it out and heap it out. When you develop and use a UNIX program, sometimes the program is inexplicably down, without any hints (sometimes prompting the core dumped). At this point you can see if there is any shape like core. Process number of file generation, this file is the operating system when the program down when the memory content thrown out to generate, it can be used as a reference to debug the program.

Core dump is also called the central Dump, when the program runs abnormal, the program abnormal exit, the program's current memory state of the system stored in a core file, called Core dump.

L core file generation

Sometimes the program down, but the core file is not generated. The core file generation is related to your current system environment settings, you can set it up with the following statement, and then run the program to generate the core file.

Ulimit-c Unlimited or ulimit-c 10000

Core files are generated in the same location as the path where the program is run, and the filename is typically core. Process number.

But after the adjustment, opening a shell will fail. So what to do.

In the CentOS system, the default is to not generate the core dump file because there is one line in the/etc/profile file:

Ulimit-s-C 0 >/dev/null 2>&1 (0 is also prohibited from producing core files)

How to turn on core dump. The easiest way is to modify the/etc/profile and change the Ulimit line to

Ulimit-s-C Unlimited >/dev/null 2>&1

This setting allows all users to generate a core dump file without a size limit.

Currently, the PFX test machine has opened the setting, if the use of the process found no core dump file, please run at the command line terminal below the command sudo source/etc/profile.

L Core file name and build path

Core file generation Path:

Enter the same path as the executable file Run command. If the system-generated core file does not have any other extended names, it is all named Core. The new core file generation will overwrite the original core file.

1)/proc/sys/kernel/core_uses_pid can control whether the file name of the core file adds PID as an extension. The file content is 1, which means adding PID as the extension, the resulting core file format is core.xxxx, and 0 indicates that the generated core file is named Core.

This file can be modified by the following command:

echo "1″>/proc/sys/kernel/core_uses_pid

2 Proc/sys/kernel/core_pattern can control the location of the core file and file name format.
This file can be modified by the following command:
echo "/corefile/core-%e-%p-%t" >/proc/sys/kernel/core_pattern

The core file can be built uniformly into the/corefile directory, resulting in a file named core-command name-pid-timestamp, and the following is a list of parameters:
%p-insert pid into filename add PID
%u-insert present UID into filename add current UID
%g-insert present GID into filename add current GID
%s-insert signal that caused the coredump into the filename adds a signal leading to the core
%t-insert Unix time when the coredump occurred into filename to add a core file generation
%h-insert hostname where the coredump happened into filename add host name
%e-insert coredumping executable name into filename add command name

Note: The executable file to be debugged, you need to add a-g,core file at compile time to display error messages normally.

gdb View Core file

When the core file is obtained, the command gdb can be used to locate the row that is causing the core dump in the file. Parameter one is the name of the application, and parameter two is the core file. GDB [execfile] [core file]

such as: GDB./test core.3224

Then enter BT or where to find the location where the error occurred and the corresponding stack information. You know the function call relationship when an error occurs, and then you can use up or down to view the previous and next details. This can be a general positioning of the problem, and then look at the source code, for analysis. Where the command is BT, the debugger locates the code location where the segment error occurred and is accurate to the line number.

L causes the program core dump

1 memory access out of bounds

A) array access is out of bounds due to the use of incorrect subscript

b When searching a string, the string terminator is used to determine whether the string ends, but the string does not use the end character properly

c) Use string manipulation functions such as strcpy, Strcat, sprintf, strcmp,strcasecmp to read/write the target string to the burst. Functions such as strncpy, strlcpy, Strncat, strlcat,snprintf, strncmp, and strncasecmp should be used to prevent reading and writing from crossing boundaries.

The 2 multithreaded application uses a thread-unsafe function.

3 read-write data for multiple threads is not protected by lock.

For global data that will be accessed by multiple threads at the same time, you should pay attention to lock protection, otherwise it can easily cause core dump

4 illegal pointers

A) using null pointers

b free use of pointer conversions. A pointer to a memory that is not converted to this structure or type unless it is determined that the memory was originally assigned to a struct or type, or an array of such structures or types

, and you should copy this memory into one of these structures or types, and then access the structure or type. This is because if the beginning address of this memory is not aligned according to this structure or type, it is easy to access it with the core dump because of bus error.

5 Stack Overflow

Do not use large local variables (because local variables are allocated on the stack), which can easily cause stack overflow, damage the system stack and heap structure, resulting in inexplicable errors.

L a small way to test the production of core files

[Root@ys hello]# ulimit-c 0

[Root@ys hello]# Ulimit-a

Core file size (blocks,-c) 0

Data seg Size (Kbytes,-D) Unlimited

File size (blocks,-f) Unlimited

Max locked Memory (Kbytes,-L) Unlimited

Max memory Size (Kbytes, M) Unlimited

Open files (-N) 1024

Pipe Size (bytes, p) 8

Stack size (Kbytes,-s) 8192

CPU time (seconds,-t) unlimited

MAX User Processes (-u) 4096

Virtual Memory (Kbytes,-V) Unlimited

[Root@ys hello]# ulimit-c 1024

[Root@ys hello]# Ulimit-a

Core file size (blocks,-c) 1024

Data seg Size (Kbytes,-D) Unlimited

File size (blocks,-f) Unlimited

Max locked Memory (Kbytes,-L) Unlimited

Max memory Size (Kbytes, M) Unlimited

Open files (-N) 1024

Pipe Size (bytes, p) 8

Stack size (Kbytes,-s) 8192

CPU time (seconds,-t) unlimited

MAX User Processes (-u) 4096

Virtual Memory (Kbytes,-V) Unlimited

[Root@ys hello]# Cat test.c

#include <stdio.h>

const char *STR = "Test";

void Core_test () {

Str[1] = ' T ';

}

int main () {

Core_test ();

return 0;

}

[Root@ys hello]# gcc-g test.c-o test

test.c:in function ' core_test ':

Test.c:4: Warning:assignment of Read-only location

[Root@ys hello]#./test

Segment error (Core dumped)

[Root@ys hello]# ls

core.2795 Test test.c

[Root@ys hello]# gdb Test core.2795

GNU gdb Red Hat Linux (5.3POST-0.20021129.18RH)

Copyright 2003 Free Software Foundation, Inc.

The GDB is free software, covered by the GNU general public License, and your are

Welcome to change it and/or distribute copies of it under certain conditions.

Type ' show copying ' to the conditions.

There is absolutely no warranty for GDB. Type ' show warranty ' for details.

This is GDB was configured as "I386-redhat-linux-gnu" ...

Core is generated by './test '.

Program terminated with signal, segmentation fault.

Reading symbols From/lib/tls/libc.so.6...done.

Loaded symbols for/lib/tls/libc.so.6

Reading symbols From/lib/ld-linux.so.2...done.

Loaded symbols for/lib/ld-linux.so.2

#0 0x080482fd in Core_test () at Test.c:4

4 Str[1] = ' T ';

(GDB) where

#0 0x080482fd in Core_test () at Test.c:4

#1 0x08048317 in Main () Test.c:8

#2 0x42015574 in __libc_start_main () from/lib/tls/libc.so.6

(GDB) Q

Multi-process debugging

The multithreaded mode must know the process number of the executing program, which can be obtained with the following command:

PS S | grep program Name

L 1: Run the program first, this time there will be the process of the program test running.

L 2: Query GUI process Current process number: PS S | grep test

n If the process number is 11325

L 3:attach Process

n GDB Attach 11325

L 4: Set breakpoints

n We know that test has function start.

N b Start (int& param)

L 5: Do the appropriate operation

L 6: If we do a record operation, then the program stops at the start method of test, and can then view the parameters or other actions.

Multi-process debugging There are other methods, attach is only one of them, but also the easiest one, the other process debugging methods please check yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.