How to Use ulimit and core files

Source: Internet
Author: User

Ulimint-A is used to display various current user process restrictions.
Linux limits the maximum number of processes per user. To improve performance,
Set the maximum number of processes for Linux users. For some processes, set them to unrestricted:
Data Segment Length: ulimit-D Unlimited
Maximum memory size: ulimit-M Unlimited
Stack size: ulimit-s Unlimited

We use this command to generate core files, that is, files with incorrect program running release segments:

Ulimit-C unlimited to generate a Core File

Ulimit-C unlimited --> CC-G hello. c -->./A. Out --> gdb a. out core

View limits ulimit-

 

The following information is displayed:

 

Core File size (blocks,-C) 0
Data seg size (Kbytes,-d) Unlimited
File size (blocks,-f) Unlimited
Pending signals (-I) 1024
Max locked memory (Kbytes,-l) 32
Max memory size (Kbytes,-m) Unlimited
Open File (-N) 1024
Pipe size (512 bytes,-p) 8
POSIX message queues (bytes,-q) 819200
Stack size (Kbytes,-S) 10240
CPU time (seconds,-T) Unlimited
Max user processes (-u) 4096
Virtual Memory (Kbytes,-v) Unlimited
File locks (-x) Unlimited

 

What we need to modify is the value of open files (-N) 1024.

 

The command is limit-N 2048 (set as needed)

 

Bytes -----------------------------------------------------------------------------------

 

 

 

Function Description:Controls shell program resources.

Syntax:Ulimit [-ahs] [-C <Core File limit>] [-D <Data Partition size>] [-F <file size>] [-M <memory size>] [-n <number of files>] [-P <buffer size>] [-S <stack size>] [-T <CPU time>] [-u <program count>] [-v <virtual memory size>]

Note:Ulimit is a built-in shell command that can be used to control shell execution program resources.

Parameters:
-A: displays the current resource limit settings.
-C <Core File limit> sets the maximum value of the core file, in blocks.
-D <Data Partition size> maximum value of the Program Data Partition, in KB.
-F <file size> the maximum file that can be created by shell. The unit is block.
-H: Set the hard limit of resources, that is, the limit set by the Administrator.
-M <memory size> specifies the maximum memory available, in KB.
-N <file quantity> specifies the maximum number of files that can be opened at the same time.
-P <buffer size> specifies the size of the MPs queue buffer, in 512 bytes.
-S <stack size> specifies the maximum number of stacks, in KB.
-S: Set the elastic limit of resources.
-T <CPU time> specifies the upper limit of CPU usage time, in seconds.
-U <program count> the maximum number of programs that can be opened by users.
-V <virtual memory size> specifies the upper limit of virtual memory available, in KB.

 

------------------

 

 

 

1, note:
Ulimit is used for resources occupied by Shell startup processes.

2. Category:
Shell built-in commands

3. Syntax format:
Ulimit [-acdfhlmnpsstvw] [size]

4. Parameter introduction:

 

Quote:

 

-H: Set hardware resource limits.
-S: Set software resource limits.
-A: displays all current resource limits.
-C size: sets the maximum value of the core file. Unit: Blocks
-D size: sets the maximum value of the data segment. Unit: Kbytes
-F size: sets the maximum value of the file to be created. Unit: Blocks
-L size: sets the maximum value of the locked process in the memory. Unit: Kbytes
-M size: sets the maximum value of resident memory that can be used. Unit: Kbytes
-N size: sets the maximum value of the file descriptor that the kernel can open at the same time. Unit: N
-P size: sets the maximum value of the MPs queue buffer. Unit: Kbytes
-S size: sets the maximum value of the stack. Unit: Kbytes
-T size: sets the maximum CPU usage time. Unit: seconds
-V Size: sets the maximum value of virtual memory. Unit: Kbytes

 

5. Simple Example:

1] In the rh8 environment file/etc/profile, we can see how the system configures ulimit:

 

Code: # grep ulimit/etc/profile
Ulimit-s-c 0>/dev/null 2> & 1

 

This statement sets the software resources and core file size.

2] If we want to limit the file size created by shell, for example:

 

Code: # ll H
-RW-r -- 1 Lee 150062 July 22 02:39 H
# Ulimit-F 100 # set the maximum size of the file to be created (one block = 512 bytes)
# Cat h> newh
File size limit exceeded
# Ll newh
-RW-r -- 1 Lee 51200 November 8 11:47 newh

 

The file H size is 150062 bytes, and the size of the file we set to create is 512 bytes X 51200 bytes = bytes
Of course, the system will generate a 51200-byte newh file based on your settings.

3] Put the ulimit you want to set in the environment file/etc/profile, just like instance 1.

 

 

 

Bytes ------------------------------------------------------------------------------------------------------------------

 

When some programs in the system encounter some errors and crash, the system will automatically generate core files to record the crash time system information, including memory and register information, which can be used by programmers in daily debugging. These errors include segment errors, invalid commands, bus errors, or user-generated exit information. Generally, core files are stored in the current folder.

 

The core file may not appear in your current folder when an error occurs. There are two reasons for this: one is that the current terminal is set to not be able to pop up the core file; the other is that the path of the core file is specified.

 

For the former, we can useUlimitCommand to set the core file size. By default, the core file size is set to 0, so that the system will not dump the core file. In this case, run the following command:Ulimit-C UnlimitedYou can set the size of the core file to an infinitely large value. You can also use numbers to replace unlimited to set the upper limit of the Core File more accurately.

 

In addition to setting the core file size, you can also specify the core file name. This setting modifies the/proc/sys/kernel/core_pattern and/proc/sys/kernel/core_uses_pid files. The methods for modifying these two files are as follows:

 

Echo <pattern>/proc/sys/kernel/core_pattern

 

Echo <"0"/"1">/proc/sys/kernel/core_uses_pid

 

Note that only the Super User can modify the two tables.

 

Core_pattern accepts the pattern of the core file name. It contains any string and uses % as the transfer symbol to generate some identifiers, adding special meanings to the core file name. The defined identifiers are as follows:

 

%: Equivalent to %

 

% P: equivalent to <pid>

 

% U: equivalent to <uid>

 

% G: equivalent to <GID>

 

% S: number equivalent to the signal that causes dump

 

% T: equivalent to dump time

 

% E: equivalent to the name of the execution File

 

% H: equivalent to hostname

 

In addition to the above signs, it also stipulates:

 

1. The single % at the end can be directly removed;

 

2. Add % to any character other than the above, and the % and this character will be removed;

 

3. All other characters are added as common characters;

 

4. The maximum name of the core file is 64 bytes (including '\ 0 ');

 

5. The default pattern in core_pattern is core;

 

6. To maintain compatibility, you can add % P to the end of the core file by setting core_uses_pid;

 

7. pattern can contain path information.

1. You can use ulimit-a to view the stack size.
In kernel 2.6.20, the stack size is 8192 Kbytes.
If there is no limit, the size of the stack is limited by the memory. 2 GB is the upper limit.

2. Core File
* Enable or disable the generation of core files
Ulimit-C can check whether this option is enabled. If it is 0, it is disabled;
Ulimit-C 0 can be manually disabled
Ulimit-C 1000 sets the core file size to 1000 kb

Ulimit-C unlimited: Set the core file size to unlimited.

 

Many systems disable generating core files by default. This command can be added to your profile.

3. Set the core dump file directory and naming rules for core dump.

 

By default, many system core files are generated in the directory where you run the program, or the directory after chdir in the program, then a PID is added to the back of the core file. In actual work, this may cause many directories to generate core files, which is not easy to manage. In fact, in 2.6, the location where the core file is generated and the name of the file name can be configured.

 

/Proc/sys/kernel/core_uses_pid can be used to control whether the PID is added as an extension to the file name of the generated core file. If it is added, the file content is 1; otherwise, it is 0.
Proc/sys/kernel/core_pattern can be used to set the location or file name of the formatted core file. For example, the original file content is core-% E
You can modify it as follows:
Echo "/tmp/Core-% E-% P"> core_pattern
The generated core file is stored in the/corefile directory, and the generated file name is core-command name-PID-timestamp.
The following is a list of parameters:
% P-insert PID into filename add PID
% U-insert current uid into filename add current uid
% G-insert current GID into filename add current GID
% S-insert signal that caused the coredump into the filename added to generate core Signal
% T-insert UNIX time that the coredump occurred into filename UNIX time when the core file is generated
% H-insert hostname where the coredump happened into filename Add the Host Name
% E-insert coredumping executable name into filename add command name

Of course, you can do this in the following ways:
Sysctl-W kernel. core_pattern =/tmp/Core-% E-% P

 

These operations will be lost once the computer restarts. If you want to persist these operations, you can add the following in the/etc/sysctl. conf file:
Kernel. core_pattern =/tmp/Core % P

 

If you want to check the effect without restarting, run the following command:
Sysctl-P/etc/sysctl. conf

Solved problems:
A program P has been running on the server for a long time, and is often killed once every day (segment error ).

Current solution:
Use securecrt to open a terminal, set ulimit-C nulimited on the service, and then start the program p. The query result using the ulimite-a command is as follows:

Core File size (blocks,-C) Unlimited
Data seg size (Kbytes,-d) Unlimited
File size (blocks,-f) Unlimited
Pending signals (-I) 1024
Max locked memory (Kbytes,-l) 32
............
Indicates that the core file can be generated.

And test the core file using the kill-6 PID.

Current difficulties:

When you run the ulimit-C nulimited terminal (and the terminal starts program P to the background./P &) to close it, the program P does not generate a core file after it dies.
After the test, it is found that the ulimit command is related to the terminal.

Expert advice:
How to configure the ability to generate core files is not related to the terminal
That is, the program starts and closes the terminal. When the program dies (segment error), the core file can be generated.

In
/Etc/security/limits. conf (the RedHat derivative is set in Linux)
Or
In/etc/profile:
# No core files by default
Ulimit-s-c 0>/dev/null 2> & 1

Comment out the above line.

Other UNIX operating systems also have their own configuration files.

Bytes ------------------------------------------------------------------------------------------------------------------

GDB core multithreading debugging in Linux environments is not as convenient as. net. These days I 've been struggling to find a deadlock bug for a long time. Let's introduce the methods I 've used.

Multi-thread dump is often a segment error, which generally involves illegal memory read/write. You can use the following command to open the system switch so that it can generate a core file when it dies.
Ulimit-C Unlimited
In this case, you can see the core. PID (PID is the process number) file in the current directory. Next, use GDB:
GDB./bin./CORE. PID
After entering, use BT to view the stack situation when it is dead, and use the frame command.

There is also a thread in it that stops and does not die. This is generally a deadlock or timeout problem involving message acceptance (I have never encountered it, as I have heard of it ). In this case, you can use:
Gcore PID(PID Number of the debugging process)
Manually generate a core file and view the stack by using pstack (which does not seem to work properly in Linux. If you cannot see it, check the code carefully to see if it is in the IF, return, break, and continue statements. You need to analyze the nested locks.

1, note:
Ulimit is used for resources occupied by Shell startup processes.
2. Category:
Shell built-in commands
3. Syntax format:
Ulimit [-acdfhlmnpsstvw] [size]
4. Parameter introduction:
-H: Set hardware resource limits.
-S: Set software resource limits.
-A: displays all current resource limits.
-C size: sets the maximum value of the core file. Unit: Blocks
-D size: sets the maximum value of the data segment. Unit: Kbytes
-F size: sets the maximum value of the file to be created. Unit: Blocks
-L size: sets the maximum value of the locked process in the memory. Unit: Kbytes
-M size: sets the maximum value of resident memory that can be used. Unit: Kbytes
-N size: sets the maximum value of the file descriptor that the kernel can open at the same time. Unit: N
-P size: sets the maximum value of the MPs queue buffer. Unit: Kbytes
-S size: sets the maximum value of the stack. Unit: Kbytes
-T size: sets the maximum CPU usage time. Unit: seconds
-V Size: sets the maximum value of virtual memory. Unit: Kbytes 5, simple instance:
5. Example
When writing a program in Linux, if the program is large, you will often encounter problems such as "segment error" (segmentation fault, this is mainly because the initial stack size (stack size) of the Linux system is too small, generally 10 m. I usually set the stack size to 256 m, so there is no segment error! Command:
Ulimit-s 262140
If you want the system to automatically remember this configuration, edit the/etc/profile file, under the "ulimit-s-c 0>/dev/null 2> & 1" line, add "ulimit-s 262140" and save and restart the system!
1] In the rh8 environment file/etc/profile, we can see how the system configures ulimit:
# Grep ulimit/etc/profile
Ulimit-s-c 0>/dev/null 2> & 1
This statement sets the software resources and core file size.
2] If we want to limit the file size created by shell, for example:
# Ll H
-RW-r -- 1 Lee 150062 July 22 02:39 H
# Ulimit-F 100 # set the maximum size of the file to be created (one block = 512 bytes)
# Cat h> newh
File size limit exceeded
# Ll newh
-RW-r -- 1 Lee 51200 November 8 11:47 newh
The file H size is 150062 bytes, and the size of the file we set to create is 512 bytes X 51200 bytes = bytes
Of course, the system will generate a 51200-byte newh file based on your settings.
3] Put the ulimit you want to set in the environment file/etc/profile, just like instance 1.
Purpose
Set or report user resource limits.
Syntax
Ulimit [-H] [-S] [-A] [-C] [-D] [-F] [-M] [-N] [-S] [-T] [limit]
Description
The ulimit command sets or reports the resource limits of user processes, as defined in the/etc/security/limits file. The file contains the following default limits:
Fsize = 2097151
Core = 2097151
CPU =-1
Data = 262144
RSS = 65536
Stacks = 65536
No files = 2000
These values are used as default values when new users are added to the system. When a user is added to the system, the preceding values are set using the mkuser command or the chuser command.
The limit can be soft or hard. With the ulimit command, you can change the soft limit to the maximum value of the hard limit. To change the hard limit of a resource, you must have the root user permission.
Many systems do not include one or more limits. The limit of a specific resource is set when the limit parameter is specified. The value of the limit parameter can be a number in a specified unit in each resource or an unlimited value. To set a specific ulimit to unlimited, you can use the word unlimited.
Note: In the/etc/security/limits file, setting the default limit is to set the system Width limit, not just the limit required by the user when creating the user.
When the limit parameter is omitted, the current resource limit is printed. The soft limit is printed unless the-H flag is specified. When you specify more than one resource, the limit name and unit are printed before the value. If no option is provided, it is assumed that the-F flag is included.
Because the ulimit command affects the current shell environment, it will be provided as a general built-in shell command. If you call this command in an independent command execution environment, the file size limit of the caller environment is not affected. This is the case in the following example:
Nohup ulimit-F 10000
Env ulimit 10000
Once the hard limit is reduced through the process, it cannot be increased without the root privilege, even if it is returned to the original value.
For more information about user and system resource limits, see getrlimit, setrlimit, or vlimit subroutines in Aix 5l Version 5.3 Technical Reference: base operating system and extensions volume 1.
Flag
-A lists the limits of all current resources.
-C uses 512 bytes to specify the core dump size.
-D specifies the size of the data area in K bytes.
-F sets the file size limit (in blocks) when the limit parameter is used, or reports the file size limit when no parameter is specified. The default value is the-F flag.
-H specifies the hard limit of a given resource. If you have root user permissions, you can increase the hard limit. Any user can reduce the hard limit.
-M specifies the size of the physical memory in K bytes.
-N specifies the maximum number of file descriptors that a process can possess.
-S specifies the size of the stack in K bytes.
-S specifies the soft limit for the given resource. The soft limit can be increased to the hard limit value. If the-H and-s signs are not specified, the limit applies to the above two.
-T specifies the number of seconds used by each process.
Exit status
The following exit values are returned:
0.
> 0: a request to a higher limit is rejected or an error occurs.
Example
To set the file size limit to 51,200 bytes, enter:
Ulimit-F 100

What is core dump?

Core means memory, dump means throwing it out and heap it out.

When developing and using UNIX programs, sometimes the program is inexplicably down, but there is no prompt (sometimes the prompt is core dumped ). at this time, you can check whether it is like core. process number file generation. This file is generated by the operating system throwing out the memory content when the program is down. It can be used as a reference for debugging programs.

Core dump is also called core dump. When an exception occurs during the program running and the program exits abnormally, the operating system stores the current memory status of the program in a core file, which is called core dump.

How to use core files?

GDB-C core file path [Application Path]

Enter where and press enter to display the row in which the program is deleted and in which function.

Why is no core file generated?

Sometimes the program is down, but the core file is not generated. the generation of the core file is related to the environment settings of the current system. You can use the following statement to set the file and then run the program to generate the core file.

Ulimit-C Unlimited

The core file is generally generated in the same path as the running program, and the file name is generally the core. Process number.

4. Use GDB to view the core file:

Here we can dump the core when an error occurs due to the message number.

After a core dump occurs, use GDB to view the content of the core file to locate the line that causes the core dump in the file.

GDB [exec file] [core file]

For example:

GDB./test. Core

After entering GDB, run the BT command to view backtrace to check where the program is running and locate the core dump file-> line.

 

 

What is core?
Before using semiconductors as the memory material, humans use coils as the memory material (the inventor is Wang An). The coils are called core, and the memories made by coils are called core memory. Today, the semiconductor industry is booming and no one has used core memory. However, in many cases, memory is still called core.
What is core dump?
When developing (or using) a program, what we fear most is that the program can be ignored. Although the system is okay, we may encounter the same problem next time. Then, the operating system will dump out the memory content when the program is down (usually written in a file called core). Let's use it as a reference or debugger. This action is called core dump.
Why does core dump occur?
As mentioned above, an error occurs when the program fails. In C/C ++, the most common error occurs when the needle is faulty. You can use the core file and debugger to locate errors (how to use the core file in the debugger? MAN: GDB !).
How to Use GDB to call core?

When the program written in C shows that the core is, you can use GDB programname core to view the core. If the core file cannot be found, run the following command to view and modify it.

Ulimit-A // view the size of the core file

Ulimit-C // modify the size of the core file

If GDB-C Core encounters the following situation:

#0 0x08048373 in ?? ()

#1 0xbfffd8f8 in ?? ()

#2 0x0804839e in ?? ()

#3 0xb74cc6b3 in ?? ()

#4 0x00000000 in ?? ()

Use the following method to solve the problem: GDB./A. out core and then BT.

In addition to BT, the following commands are available: Where, frame, up, down, and print.

[How to generate core files in cygwin]

In cygwin, the program core dump generates only one *. stackdump file. to generate the core dump file, you need to set the environment variable.

Export cygwin = "$ cygwin error_start1_dumper.exe-D % 1% 2"

 

 

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.