Linux you need to know basic programming (IV)

  1. gdb debugger
    GDB (GNU Debugger) is a GCC debugging tools. Functions are as follows:

     1. 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序
     2. 可让被调试的程序在你所指定的调试的断点出停住。(断点可以是条件表达式)
     3. 当程序被停住的时候,可以检查此时你的程序中所发生的事
     4. 动态的改变你程序的运行环境
    
    1. Adjustable generated when the gdb file in gcc compile time to add parameters -g
    2. Start GDB
      1. Start only perform step
      2. n (next) ---- next step
      3. s (step) ---- into the internal function of the body
      4. c (continue) ---- parked directly in the breakpoint
    3. See Code
      1. L (List)
      2. L + line number
      3. l + file name +: + function name
      4. l + file name +: + line number
    4. Set a breakpoint
      1. Sets the current file breakpoint
        1. break + row number (BREAK can be abbreviated as b)
        2. break + function name
      2. Set a breakpoint specified file
        1. break + filename + line number
        2. break + filename + function name
      3. Set conditional breakpoints
        b + line number + conditions (if I == 15)
      4. Remove breakpoints
        del + breakpoint corresponding number
    5. See breakpoint set
      info + break (abbreviation: I b)
    6. Started gdb debugging (start)
      1. Execute step operation
      2. n (next) ---- next step
      3. s (step) ---- function can body into the interior
      4. c (continue) ---- stops directly at the breakpoint
    7. Stepping
      1. Entering the functions that: s
      2. From the inside out function: finish (premise is to remove the breakpoint function body)
      3. Exit the current cycle: u
    8. View the current value of the variable
      p (print) + variable name
    9. Check variable type
      ptype + variable name
    10. Set the value of the variable
      set var + variable name + "=" + value (avoid waiting)
    11. Set the tracking variable
      1. display + variable name
      2. info + display (display trace variables)
      3. undislay + tracking number of variables
    12. Quit gdb debugger
      quit
  2. Write a makefile

    1. when the make command, you need a makefile to tell make what commands need to compile and link the program.
      makefile tells the make command how to compile and link these documents, the rule is:

      1. If this project is not compiled, then all the c file must be compiled and linked
      2. A few C files are modified if this works, then we only modified C compiler, and link with the target program
      3. If the project header file is changed, then we only need to build the C file is referenced several header files and link target program
      4. As long as the makefile to write a good, all this only can be done with a make command, make command can automatically determine which files need to be recompiled based on the current file modification, so to compile the required documents and link target program
    2. makefile nomenclature
      MakeFile some who makefile

    3. makefile rule (the core content of the makefile)

       1. Target…:prerequisites…
       		Command
       		…
       		…
       2. target也就是一个目标文件,可以是object File,也可以是执行文件,还可以是一个标签(Label)
       prerequisites就是,要生成那个target所需要的文件或是目标
       command就是make需要执行的命令。(任意的Shell命令)
       3. 这是一个文件的依赖关系,target这一个或多个的目标文件依赖于prerequisittes中的文件,其生成规则定义在command中。说白一点,prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。
       4. 在定义好依赖关系以后,后续的那一行定义了如何生成目标文件的操作系统命令,一定要以一个Tab键作为开头。make并不管命令是如何工作的,他只管执行所定义的命令。make会比较targets文件和prerequisites文件的修改日期,如果prerequisites文件的日期要比targets文件的日期要新,或者target不存在的话,那么make就会执行后面的命令
      
    4. One example
      . 1) test directory:
      Here Insert Picture Description
      2) content of the makefile:
      Here Insert Picture Description
      3) make the input in the test directory command command executes the command, and generates an executable main
      . 4) enter the command: ./ main
      Here Insert Picture Description

    5. make work
      in the default mode, that is, we just type make, perform the following steps:

      1. make'll find the name "Makefile" or "makefile" file in the current directory
      2. If found, he would find the file in the first target file (target), and the ultimate goal of this document as file
      3. Modified If the target file does not exist, or the object file depends .o file object files than the new, then he will execute the command later defined to generate an object file
      4. So if the target file to the .o files exist, make will look for the target file in the current file is dependent .o file, if found then generates .o file (which looks like a stack process) in accordance with a rule that
        Here Insert Picture Description
      5. C files and H files exist, so make generates a .o file, and then use the .o file generation tasks make the ultimate, which is the target file
    6. Use the makefile variables

      1. Ordinary variables
        1. When the makefile becomes complicated, easy to maintain in order makefile in the makefile we can use variables, makefile variable that is a string
          Here Insert Picture Description
        2. By a number of variables makefile maintenance format is usually capitalized.
          1. Some variables have a default value (CC: The default value cc cc == gcc)
          2. Some variables no default
            1. CPPFLAGS: pre-need options such as: -I
            2. CFLAGS: compile time using the -c parameter -wall -g
            3. LDFLAGS: Option -L-link library used by -l
        3. The user can also modify the values ​​of these variables (CC = gcc)
      2. Automatic variables
        1. Variables:
          A) $ @ - >> target rule
          b) $ <- >> first rule dependencies
          c) $ ^ - all dependent files >> rules
        2. Mode rules:
          A)% target using rules defined in
          b) in% used in the rule dependencies
          Here Insert Picture Description
    7. make automatic derivation of
      GNU make is very powerful, he can automatically derive command files and dependent relationships behind, they see a .o file, he would add the .c file to the dependency, and later will be deduced come out
      Here Insert Picture Description

    8. makefile contents inside:
      makefile which contains five main things: display rules, obscure rules, variable definitions, instructions and file comments

      1. Show rule: Displays rule explains how to generate one or more object files. This is clearly indicated by the makefile writer, it relies on to generate files, generate commands
      2. Obscure rule: Since we make the automatic derivation function, so obscure rule allows us to rough writing makefile, which is supported by the make of
      3. The definition of variables: in the makefile we have to define a set of variables, which are generally strings, when makefile is executed, in which the variables will be extended to the corresponding reference position
      4. File indicates: three parts:
        1. Another cited a makefile makefile (like C language include)
        2. In some cases the active portion designated according to the makefile (like C language as #if precompiled)
        3. The definition of a multi-line command
      5. Notes: Only the makefile line comments, that comment is "#" character, if you want to use the "#" character to be escaped with a backslash, such as: "/ #"
        command in the makefile, we must start with the tab key
    9. makefile two functions of
      the makefile Each function has a return value

      1. Gets the specified directory .c file
        src = $ (wildcard ./ .c)
        function called wildcard
        directory ./
        .c (all current .c files in the directory)
        $ for the value of the returned value acquisition
      2. Alternatively .o .c files into the file
        obj = $ (patsubst ./%.c, ./%.o , $ (src))
        function called patsubst
        ./%.c file is c
        ./%.o is o file
        $ (src) stored inside .c file
        Here Insert Picture Description
    10. Delete * .o files and object files (temporary files Clear middle) (Enter the command in the terminal: make clean will execute the makefile in rm $ (obj) $ (target ) -f)
      Here Insert Picture Description
      the role of the -f parameter: is mandatory the implementation of the previous command

    11. Pseudotarget

      1. The reason
        the command make clean does not generate a file in the current directory, so clean as the pseudo-objective, if you create a clean file in the current document, in the implementation of make clean command will prompt " 'clean' is up to date " , will not be executed "rm $ (obj) $ ( target) -f" this command
      2. Solution:
        In a previously declared clean, clean pseudo target
        Here Insert Picture Description
  3. Io function of the system
    format to view the file: file + file name

    1. IO C library functions workflow
      Here Insert Picture Description

    2. pcb and file descriptors
      Here Insert Picture Description

    3. Virtual address space
      Here Insert Picture Description

    4. Why cpu To use the virtual address space and physical address space mapping? What solved the problem?

      1. Convenient compilers and operating systems Scheduler addresses distribution.
        Program can use a series of adjacent virtual address to access non-contiguous physical memory in large memory buffer
      2. Isolation between easy process
        virtual address different processes used in isolation from each other, a process can not change the code in physical memory being used by another process
      3. Easy to use operating system memory
        can use a series of virtual addresses to access the memory buffer larger than the available physical memory. When the supply of physical memory becomes smaller, the memory manager will be physical memory pages (usually size is 4KB) saved to a disk file
    5. The relationship between library functions and system functions
      Here Insert Picture Description
      f. Function (open, read, write, lseek , close)

       查man文档(man 2 + 函数名称)
       	errno:
       		1) 是一个全局变量(任何标准C库函数都能对其进行修改(Linux系统函数更可以))
       		2) 错误信息定义的位置:
       			a) 第1-34个错误定义:
       			/usr/include/asm-gengric/errno-base.h
       			b) 地35-133个错误定义:
       			/usr/include/asm-generic/errno.h
       		3) 是记录系统的最后一次错误代码,代码是一个int型的值
       			a) 每个errno值对应着以字符串表示的错误类型
       			b) 当调用“某些”函数出错时,该函数会重新设置errno的值
       		4) perror
       			a) 头文件:stdio.h
       			b) 函数定义:void perror(const char * s)
       			c) 函数说明:
       				i) 用来将上一个函数发生错误的原因输出到标准设备(stderr)
       				ii) 参数s所指的字符串会先打印出,后面在加上错误原因字符串
       				iii) 此错误原因依照全局变量errno的值来决定要输出的字符串
       	1. open
       		1) 头文件:
       			a) #include<sys/types.h>
       			b) #include<sys/stat.h>
       			c) #include<fcntl.h>
       		2) 函数定义
       			a) int open(const char * pathname,int flags);
       			b) int open(const char * pathname,int flags,mode_t mode)
       		3) 函数说明(用来打开一个存在或者打开一个不存在的文件)
       		4) 参数说明:
       			a) pathname:打开文件的路径(绝对路径或者是相对路径)
       			b) flags:文件的打开方式
       				i) the argument flags must include one of the following access modes:O_RDONLY,O_WRONLY,or O_RDRW.This request opening the file read-only,write-only,or read/write, respectively
       				ii) in addition,zero or more file creation flags and file status flags can be bitwise-or'd in flags. The file creation flags are O_CLOEXEC,O_CREAT,O_DIRECTORY,O_EXCL,O_NOCTTY,O_NOFOLLOW,O_TEPFILE, and O_TRUNC. The file status flags are all of the remaining flags listed below. The distinction between these two groups id flags is that the file status flags can be retricved and (in some cases) modified.
       			c) mode:指文件创建后的权限
       				注意:文件的实际权限 = 创建文件是给定的权限 & 本地掩码取反
       				查看掩码umask
       				修改掩码umask + 三位的八进制数
       		5) 函数返回值:
       			open() return the new file descriptor, or -1 if an error occurred (in which case,errno is set appropriately).
       	2. read
       		1) 头文件:
       			a) #include<unistd.h>
       		2) 函数定义
       			a) ssize_t read(int fd,void *buf,size_t count)
       				i) size_t是无符号的整数
       				ii) ssize_t是有符号的整数
       		3) 函数说明
       			a) read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf
       		4) 参数说明
       			a) fd:文件描述符
       			b) buf:缓冲区
       			c) count:缓冲区的大小
       		5) 函数返回值
       			a) on success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal
       			b) on error, -1 is returned, and errno is set appropriately. In this case, it is left unspecified whether the file position (if any) changes
       	3. write
       		1) 头文件:
       			a) #include<unistd.h>
       		2) 函数定义:
       			a) Ssize_t write(int fd, const void * buf, size_t count)
       				i) Size_t是无符号的整数
       				ii) Ssize_t是有符号的整数
       		3) 函数说明:
       			a) write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd
       		4) 参数说明:
       			a) fd:文件描述符
       			b) buf:缓冲区
       			c) count:写的位置
       		5) 函数返回值:
       			a) on success, the number of bytes written is returned (zero indicates nothing was written). It is not an error if this number is smaller than the number of bytes requested; this may happen for example because the disk device was filled.
       			b) on error, -1 is returned,and errno is set appropriately
       	4. lseek(获取文件大小、移动文件指针、文件拓展)
       		1) 头文件:
       			a) #include<sys/types.h>
       			b) #include<unistd.h>
       		2) 函数定义:
       			a) off_t lseek(int fd,off_t offset,int whence)
       		3) 函数说明:以指定的位置打开文件
       		4) 参数说明:
       			a) fd:文件描述符
       			b) offset:文件偏移量
       			c) whence:
       				i) SEEK_SET(the offset is set to offset bytes)
       				ii) SEEK_CUR(the offset is set to its current location plus bytes)
       				iii) SEEK_END(the offset is set to the size of the file plus offset byte)
       		5) 函数返回值:
       			a) upon successful completion,lseek() returns the resulting offset location as measured in bytes from the beginning of the file.
       			b)  on error, the value (off_t) -1 is returned and errno is set to indicate the error
       	5. close
       		1) 头文件:
       			a) #include<unistd.h>
       		2) 函数定义
       			a) int close(int fd);
       		3) 函数说明:关闭一个已经打开的文件
       		4) 参数说明:
       			a) fd:文件描述符
       		5) 函数返回值:
       			close() returns zero on success. On error, -1 is returned, and errno is set appropriately
       		6) 错误类型:
       			a) EBADF: fd isn't a valid open file descriptor
       			b) EINTR: The close() call was interrupted by a signal
       			c) EIO:An I/O error occurred
      

The following is the author of the micro-channel public number, welcome attention, will continue to update c ++, python, tensorflow, machine learning, deep learning, computer vision and other articles, public No. contains 300+ this pdf e-books, certainly you need one, you can receive public attention No. Oh.
Here Insert Picture Description
If you are interested in JAVA aspects, can focus on the following JAVAERS public number, accompany you learn together, grow together, sharing poetry and distant JAVA road together. In which public numbers are JAVA friend of this world, the public will have number-technical articles every day, by dry surface, but also the advanced architecture of e-books, such as Spring combat, SpringBoot practical, high-performance MySQL, in-depth understanding of JVM, RabbitMQ combat , Redis design and implementation of a number of high-quality books and so on, you can receive public attention No. Oh.
Here Insert Picture Description

Published 43 original articles · won praise 129 · views 10000 +

Guess you like

Origin blog.csdn.net/Xiao_Jie123/article/details/105252336