Linux Learning Notes-BASIC operations 4

Source: Internet
Author: User

1. GDB Debugging
2. Preparation of the Makefile
3. System IO Functions


1. GDB Debug:
1. Start GdB + executable file
2. View the code:
L==list
L (the first 10 lines of the main function are given by default) (the input L again will give you a press ENTER after you continue to list the following)
L line number (or function name)
L FileName: line number (or function name)
3. Set Breakpoints:
To set the current file breakpoint:
b = = Break
B line number (or function name)
To set the specified file breakpoint:
B FileName: Line number (function name)
To set a conditional breakpoint:
b If a variable ==19
To delete a breakpoint:
Delete = = Del = d
The number of the D breakpoint
Get number:
info = = I
Info b
4. View the Breakpoints Set
I==info
I b
5. Start GDB Debugging
Take one step: Start (or R==run directly or stop at the breakpoint)
Single Step continuation: N
Perform multiple steps and stop directly at the breakpoint: C
5. Single-Step commissioning
Enter inside the function body: s
Jump from inside the function body: finish
Do not enter inside the function body: n
Exit Current loop: U
6. View the value of the variable: p = = Print
P Variable Name
7. View the type of the variable: PType variable name
8. Set the value of the variable: set var variable name = Assignment
9. Set Tracking variables
Display + variable name
Cancel tracking Variable: undisplay number
Get Number: Info display
10. Exit GDB Debugging
Quit

2. Makefile writing: Project code management tool, save time to compile project, write lifetime benefit once
1. Name of makefile: Makefile (or makefile)
2. Makefile rules: (vi makefile Create files directly)
Three elements of the rule: Target (app name), dependent (. c file used), command (GCC command)
Goal: Dependent conditions
(tab Indent) command
Relationship between sub-goal and ultimate goal: generating the final goal
User-defined rules:
Clean
(-makedir \aa cannot succeed without adding-the command is paused here, plus-you can ignore the command to continue execution)
RM *.o-f (-f Force Delete, even if not)

Make clean (the command that executes the corresponding clean rule)
The rule cannot be executed if the file is in the current directory that is known as clean
The solution is to make a pseudo-target declaration on the previous line:. Phony:clean
3. Execute makefile to generate the ultimate target app:make (some GCC commands will be shown on the next line) 4. If you want to generate a target, check if the dependency condition exists in the rule, and if it does not exist, find out if there is a rule to generate the dependent file. Check that the target in the rule needs to be updated, that all its dependencies must be checked, and that any one of the dependencies is updated, the target must be updated. Dependent files are later than the destination file and need to be updated.
5. Variables such as OBJ=MAIN.O SUM.O can be declared in makefile SUB.O
Use variable: $ (obj)
6. Makefile two functions (all functions in Makefile must have a return value)
Wildcard: Finds a file of the specified type under the specified directory, one parameter
src = $ (wildcard./*.c) Find all files in the./src directory that have the suffix. C assigned to the variable src in the form of a string
PATSUBST: Match Replace, find all the. c End files from SRC and replace them with. O
obj = $ (Patsubst%.c,%.O, $ (SRC)) replaces all files with. C in the SRC variable with a. O
7. Automatic variables in the makefile
$<: The first dependency in a rule
[Email protected]: The goal in the rule
$^: All dependencies in a rule
Can only be used in commands of a rule
APP:MAIN.O SUB.O MUL.O
GCC main.o sub.o mul.o-o app
(GCC $^-o [email protected])

%.O:%.C (the first% matches the ultimate target dependency, the second% matches the first% content)
Gcc-c $<-o [email protected]
8.Makefile of self-maintained variables, are capitalized.
CC = CC (GCC)
Cppflags: Preprocessor requires options such as:-I
CFLAGS: Parameters used when compiling –wall–g-c
Ldflags: Options used by the link library –l-l
Users can modify the default values of these variables cc = GCC
3. System IO Functions
1>. Some concepts


File descriptor

Pcb

The relationship between library function and system function

2>. Open
Function Prototypes:
int open (const char *pathname, int flags);
int open (const char *pathname, int flags, mode_t mode);
Open in the way:
Mandatory option Flags:
O_rdonly Read Only
O_wronly Write only
O_rdwr Reading and writing
Available options:
O_creat Creating a new file
Third parameter: File permissions: Local has a mask: umask can view umask 022 can be temporarily modified
Actual file permissions: Given permissions & local mask (take back) = = Actual file permissions
7 7 7
111111111 (8 binary for a given permission)
111111101 (8 binary with Mask de-inverting)
---------
111111101
7 7 5
O_EXCL if O_creat is specified at the same time and the file already exists, an error is returned. <stdio.h>void perror (const char *s) is used to output the cause of an error in the previous function to a standard device (stderr)
O_trunc truncates the file to 0, and if the file already exists, truncates its length (Truncate) to 0 bytes.
O_append Append if the file already has content, the data written by this open file is appended to the end of the file.
3>. Read reads data from an open device or file.
Function prototypes: ssize_t read (int fd, void *buf, size_t count);
FD: File descriptor
BUF: Data buffer
Count: Number of bytes requested to read
return value:
1.-1 Read file failed
2.0 files are finished reading
3. Number of bytes read by >0
4>. Write writes data to an open device or file.
Function Prototypes: ssize_t write (int fd, const void *buf, size_t count);
return value:
-1---fail
>=0 the number of bytes written to the file
5>. Lseek Modify the file offset (read and write location). The extension of the file, the final need to do additional write operations.
Function prototypes: off_t lseek (int fd, off_t offset, int whence)
off_t offset---offset
int whence to offset position
Seek_set-Offset from file header backwards
Seek_cur-offset backwards from the current position
Seek_end-Offset backward from file tail
return value:
Backward offset from file start position
Allows the offset to be set beyond the end of the file, so the file is expanded.
Failed return-1
Get file Length: Lseek (FD, 0, seek_end); The return value is the length of the file
6>. Close
Parameters: Return value of the Open function
return value:
0--Normal off
-1-off error occurred

Why does the CPU use virtual address space and physical address space mapping? What kind of problems have been solved?

1. Convenient for compiler and operating system to arrange the address distribution of the program.
A program can use a series of neighboring virtual addresses to access large, non-contiguous memory buffers in physical memory.

2. Facilitate isolation between processes
Virtual addresses used by different processes are isolated from each other. The code in one process cannot change the physical memory that is being used by another process.

3. Easy to use your poor memory in the OS.
A program can use a series of virtual addresses to access memory buffers that are larger than the available physical memory. When the amount of physical memory is available for an hour,
The memory manager saves a physical memory page (typically 4 KB) to a disk file. The data or code page moves between the physical memory and the disk as needed.



There are two ways to read and write large files:
Read write-1 bytes per reading low efficiency

Getc putc--1 bytes per read high efficiency
A buffer is provided inside the standard C library function

Linux Learning Notes-BASIC operations 4

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.