The introduction of Gcc&&gdb in Oi

Source: Internet
Author: User
Tags switches win32 windows x64

Preface

This was originally written in Word, but then I changed the system so I can only use markdown migration and then wrote ...
$\qquad$ This article is mainly for those who live in windows for a long time and then find the need to use the command line to operate the confused and feel that GDB is delicious
$\qquad$ and----

$\qquad$ often blind to see the difference between i++ and J + +
$\qquad$ frequent access to a[-1] However, the compiler has no alternative (except in the case of the use of O2 the obvious access out of bounds) people
...
$\qquad$ formally, this paper introduces the application of Gcc&&gdb command in Oi.

Feed

$\qquad$ does this need a synopsis? This article is about GCC and gdb.
$\qquad$ Note This article describes only the application in Oi, some commands may have more advanced usage but will not be introduced, there are some concepts may be the author paste out can help you better understanding.
$\qquad$ This article as a tutorial in the language grammar is not very strict, such as commands and instructions are often mixed together (in fact, I do not know what the difference), but ensure that the content is available.

Description

$\qquad$ Some parts of this article are done under Windows x64, and some are done under Linux x64. It explains the game's operating system Noi Linux (in most cases the same).
Compiler under $\qquad$windows: GCC version 7.1.0 (i686-win32-dwarf-rev0, Built by Mingw-w64 Project), 7.1.0 supports the standard to c++17. The command line is Cmder (insignificant).
Compiler for $\qquad$linux: Thread model:posix
GCC version 6.4.0 20170724 (Debian 6.4.0-2), support to C++17 standard
$\qquad$noi Linux Please refer to Noi Linux for instructions.
$\qquad$ The following instruction Description section ${...} Not the part you want to enter, it represents the parameter. The parentheses behind the instruction indicate shorthand.
$\qquad$ the next relatively simple demo source file, named XiaPi.cpp.

#include <cstdio>int main(){    int ans = 0;    for(int i = 1; i <= 10; i++)        ans ++;    if(ans) std::puts("Hello World");    return 0;}
Gcc

$\qquad$ is generally referred to as GCC. However, generally compiling the gcc/g++ compiler that we use, in fact, both can "compile" C + + source code, mainly because the compile time will be similar to "call each other." The only difference is that GCC cannot link to C + + libraries, so gcc links need to be-lstdc++. Anyway g++ is omnipotent, then use g++ bar.
$\qquad$ then recommend a good thing: makefile. Because the author generally uses the compiler command is relatively long, for example:
g++ XiaPi.cpp -g -Wall -Wextra -std=c++17
$\qquad$ then you create the makefile file in the source code directory, and then the syntax reference inside:

all:XiaPi.cpp    g++ XiaPi.cpp -g -Wall -Wextra -std=c++17

$\qquad$ That means XiaPi.cpp is your source file name, followed by your build command. Note the indentation of the second line is required.

Command

$\qquad$g++ Common Command parsing in Oi:

g++ -v

$\qquad$ is to print out the information of your compiler.
$\qquad$ does not need to add a file name.
$\qquad$ first compile the file to start with g++ XiaPi.cpp.
$\qquad$ Next is a variety of switches. All switches can be swapped in order, but the file name (except-V) must be followed g++.

-g2(-g)/-g3

$\qquad$ turn on the debug switch so that the code can be debugged.
$\QQUAD$-G3 can debug macro-defined code

-O0/-O1/-O2/-Os/-O3

$\qquad$ compiler optimizations, generally do not need to add, because in O2 and above the level will lead to unpleasant debugging.
$\qquad$ However, the above optimizations such as opening O2 compile can find out some problems such as cross-border.
$\qquad$os is optimized for O2.5, that is, opening O2 without adding code length, sometimes problematic

-Wall -Wextra -Werror

$\qquad$ three commands refer to a warning to the code, where: the first is the underlying warning, the second is a verbose warning, and the third is to treat all warnings as errors.
$\qquad$ has excellent auxiliary effects for people who are less careless.

-std=${standard(c++11 / c++14 / c++17)}

$\qquad$ Specifies the C + + standard. However, generally speaking, the test will not add this option, so usually Gaga is almost
$\qquad$ is mainly to exercise Some of the ability to write high-level code.
$\qquad$ Note Not all compiler versions support a variety of standards, and the standard currently used is c++11,c++14,c++17.
$\qquad$ These standards differ in syntax, for example:
$\qquad$register ID will be prompted to disable under c++17 (in fact, many of the identities will be ignored)

for(auto it : vector){    ...}

$\qquad$ can be used in c++11 and above

-o ${filename}

$\qquad$ can be followed by a file name, indicating the executable file name of the output. But don't write something like-o XiaPi.cpp ...

-E

$\qquad$ output preprocessor processed things, usually need to add > xiapi.e after the instruction to output to the file. It is generally useless, but you can see how much effort the predecessors have made to your code.
$\qquad$ For example, the following picture is the XiaPi.cpp after-e processing.
Pretreatment Pretreatment
$\qquad$ this has thousands of lines, however the source code is only a few lines.

-S

$\qquad$ compiles compiled code, which can sometimes be used as a tool for analysis optimization. A. s file is output, like this:

    .file   "XiaPi.cpp"    .def    ___main;    .scl    2;  .type   32; .endef    .section .rdata,"dr"LC0:    .ascii "Hello World\0"    .text    .globl  _main    .def    _main;  .scl    2;  .type   32; .endef_main:LFB44:    .cfi_startproc    pushl   %ebp    .cfi_def_cfa_offset 8    .cfi_offset 5, -8    movl    %esp, %ebp    .cfi_def_cfa_register 5    andl    $-16, %esp    subl    $32, %esp    call    ___main    movl    $0, 28(%esp)    movl    $1, 24(%esp)L3:    cmpl    $10, 24(%esp)    jg  L2    addl    $1, 28(%esp)    addl    $1, 24(%esp)    jmp L3L2:    cmpl    $0, 28(%esp)    je  L4    movl    $LC0, (%esp)    call    _putsL4:    movl    $0, %eax    leave    .cfi_restore 5    .cfi_def_cfa 4, 4    ret    .cfi_endprocLFE44:    .ident  "GCC: (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 7.1.0"    .def    _puts;  .scl    2;  .type   32; .endef
Gdb

$\qquad$ then is the GDB part. GDB's installation (if separated from the compiler) should be matched with the compiler to view your version information through GDB-V. At the same time it doesn't seem as smooth under Windows as Linux. Sometimes the inexplicable crash, for example, should wait after running the program in GDB (at least my two windows will be like this) the thread is open, which shows a similar

[New Thread 11064.0x3d2c][New Thread 11064.0x3dbc][New Thread 11064.0x1f00][New Thread 11064.0x3dc4]

To enter the data again, or it may crash.
$\qquad$ First we use the command line to gdb ${可执行文件名} enter GDB, for example, without the-O, Windows can use GdB A (can omit the suffix name), Linux can use GDB a.out access. If you use-O, it is followed by your executable file name.
$\qquad$gdb each time a command is accepted, the carriage return repeats the previous command.
$\qquad$ generally if the current line is preceded by a (gdb) user command, it may be waiting for input or waiting for the program to run.
$\qquad$ (Extended command) If a program that is not inside GDB enters a dead loop or something, and then you want to debug it quickly in the current state, you can open a terminal and enter gdb ${可执行文件名} ${进程pid} it to control it.

Concept 停止点

$\qquad$ can cause a running program to break at a certain point. There are breakpoint (corresponding operation break) and watchpoint (corresponding to Operation Watch) (there is also a catchpoint, but not in Oi). Breakpoint accepts a position and stops when it reaches this position. Watchpoint accepts an expression that pauses the program when the value of the expression changes.

查看

$\qquad$ has been monitoring a variable, corresponding to the operation display.

Numbering and Range

$\qquad$ sometimes gdb accepts a number, sometimes it accepts a number or range, the number is a number, the range is writteni-j

Command

$\qquad$ the next operation is performed within GDB.

help
help help ${function_name}

The most important command to get instant help

file
file ${command_name}

$\qquad$ This command is when you do not enter the file name after gdb or input errors can be loaded files, when the gdb Chinese pieces have changed (do not change too much, the executable file cannot be changed) can reload the file.

list
list(l)list(l) ${function_name}list(l) ${line_number}

$\qquad$ Print Code. The first one is auto-continuous display, and the second starts printing from a line. The list will have something similar to the counter, each time you print a piece, CTRL + C exit Print, enter to continue printing. For example:

#include <cstdio>int main(){    int ans = 0;    for(int i = 1; i <= 10; i++)        ans ++;    if(ans) std::puts("Hello World");    return 0;}(gdb)

$\qquad$ If you print to the end of the file, you will have:
Line number 9 out of range; XiaPi.cpp has 8 lines.
$\qquad$ at this point, if you continue, the list will always prompt. So we want list 1 to go back to the file header.

run(r)
run(r)

$\qquad$ This command runs the program until an error is encountered or a breakpoint is encountered. Before running this command, make sure you have set breakpoints unless you use this to check your program for errors.
$\qquad$ Notice that our breakpoint is set on a few lines (for example, line fifth), the program pauses on line fifth, but notice that the program is not running line fifth at this time, for example:

Thread 1 hit Breakpoint 1, main () at XiaPi.cpp:55           for(int i = 1; i <= 10; i++) ans ++;

$\qquad$ indicates that the program did not run to line fifth. That is, when you see the line number N at a later time, the program has not run nth row .

next(n)
next(n)nexti(ni)

$\qquad$ a step down (one line), but does not enter the function
The one behind $\qquad$ is in the middle of the assembly.

step(s)
step(s)stepi(si)

$\qquad$ a step down (one line), and if a function goes in, the inline or macro definition does not go in.
The $\qquad$ back is in the middle of the Assembly, and the call

skip
skip ${function_name}skip ${file_name}skip delete\disable\enable

$\qquad$ skip this function or file at step
$\qquad$delete\disable\enable can refer to the meaning of other commands

finish
finish

$\qquad$ run until you exit this function

until
until

$\qquad$ run until the current loop ends

continue(c)
continue(c)

$\qquad$ continue to run until the next breakpoint

jump
jump ${line_number}

$\qquad$ jumps directly to a line, if a logic error occurs with the function stack, such as when the function is not finished and then transferred directly to another function, but the command does not change the contents of the stack, there are some unexpected errors

break(b)
break(b)break(b) ${function_name}break(b) (${file_name}::) ${line_number}break(b) ${line_number} ${expression}

$\qquad$ A breakpoint operation, the first is to view all breakpoints, noting that breakpoints are numbered.
$\qquad$ the second is to set a breakpoint at the beginning of a function.
$\qquad$ third causes a breakpoint to be set on a line, but the program does not run the line.
$\qquad$ for example
Breakpoint 1 at 0x401611: file XiaPi.cpp, line 6.
Represents the code that ran to line 6th in XiaPi.cpp, but did not execute line sixth
$\qquad$ What if you encounter multiple files (such as interactive questions)? You can use break ${file_name}::${line_number} to set breakpoints for the specified file
$\qquad$ (extended usage) The last one is a high-level usage: When expression satisfies the breakpoint to take effect, the $ data structure is more troublesome than the thing special effects $

condition
condition ${break_number} ${expression}

$\qquad$ a subordinate command of the above command to modify the judgment expression on a breakpoint

commands
commands ${break_number}${commands}end

$\qquad$ executes a command when a breakpoint is encountered and is stopped (commands within GDB, such as printing a variable)
$\qquad$ with if can be well used in a complex program to extract critical information
$\qquad$ For example a use case:

(gdb) commands 1Type commands for breakpoint(s) 1, one per line.End with a line saying just "end".>print ans>end
ignore
ignore ${break_number} ${times}

$\qquad$ ignoring a breakpoint's condition several times

watch
watch ${expression}rwatch ${expression}awatch ${expression}

$\qquad$ set the Observer to interrupt the program when an expression (variable) occurs for a time
$\qquad$watch: The expression value changes, Rwatch: expression is read, awatch:b expression is read or written (the following two are generally for expressions), where awatch comparison is useful for some of the things you think a variable can be changed by metaphysics.

clear
clearclear ${line_number}clear ${function_name}

$\qquad$ Delete stop point (not only breakpoint)
$\qquad$ is followed by a specified number of rows or function names

delete(d)
delete(d)delete(d) ${break_number}

$\qquad$ Delete Breakpoint, support range (this means that the command can replace the number into a range)

disable\enable
disable ${break_number}enable ${break_number}enable ${break_number} onceenable ${break_number} delete

$\qquad$ temporarily disables a breakpoint, enables a breakpoint, once means that once after disable,delete is used once, the delete
$\qquad$ Support Scope

checkpoint
checkpoint

$\qquad$ set a checkpoint at the current location so that you can quickly restore the state when you re-debug the program

restart
restart ${check_number}

$\qquad$ Restart the program in the first few checkpoint

print
printprint (${file_name}/${function_name}::)${expression}print {array_pointer}@${length}printf ${format},${expression}...

$\qquad$ The first one is the output history viewed variables
The $\qquad$ is then the value of the print expression (even if the-G3 is open and cannot be defined with a macro), you can specify a file or function (which can span functions), a function is allowed within an expression, and you can use a=1 an expression like that to assign a value, and so on.
$\qquad$ then is the number of values to look at the beginning of an array pointer, and if it is a two-dimensional array, you can print *a[10]@100 view
The one behind $\qquad$ is printing variables in printf format.
$\qquad$ Note that some STL function or struct function can not be viewed, this also has no way to summarize, according to experience it

display(disp)
display(disp) ${expression}undisplay(undisp) ${display_number}delete display ${display_number}disable ${display_number}enable ${display_number}

$\qquad$ set up to view, always print variables, as long as there is a program to stop or input commands will be displayed, be careful not to make too much too complicated, otherwise it will be very card
$\qquad$ back two are all deleted view
$\qquad$, back two, same as the front one.

call
call ${expression}

$\qquad$ executes an expression (function) and prints the return value, unlike print, where call does not print a value when the return value is void

set
set ${var_name}=${number}set var ${var_name}=${number}set args ${args}...

$\qquad$ assignment, if the assignment object is a noun of a keyword within GDB, then you need to declare it with VAR
$\qquad$ the last one can set the parameters of the program

ptype
ptype ${struct/union/class/expression}

$\qquad$ can view the specific type of a struct (specific to union/int[2]), you can examine some implicit conversions to prevent the card from floating-point

whatis
whatis ${struct/union/class/expression}

$\qquad$ can view the specific type of a struct (specific to the const number)
Examples of differences between $\qquad$ and previous commands:

(gdb) ptype dtype = const union {    int i[2];    double d;}(gdb) whatis dtype = const number

D is something defined within a library.

info
info 查看所有info可用参数info address ${var_name} -- 打印指定变量在内存中的位置info all-register -- 打印所有寄存器状态info args -- 打印当前函数栈的参数info breakpoints (${break_number}_ -- 答应断点信息info checkpoints (${check_number}) -- 打印checkpoint信息info display (${display_number}) -- 打印display的信息 info functions -- 打印函数信息info handle -- 打印处理signal的信息info line (${line_number}) -- 打印行信息,默认当前行info program -- 打印程序状态info registers (${register_name}) -- 打印寄存器状态,不要"$",例如`info register eax`info signals -- 打印信号信息info skip -- 查看skipinfo source -- 查看源码信息info sources -- 查看源码以及依赖库信息info stack -- 查看栈信息info symbol -- 查看某个地址上的变量info types -- 打印所有定义过的类型info variables -- 打印所有变量info watchpoints -- 打印观察点信息

It's on top.

breacktrace(bt)
breacktrace(bt)breacktrace(bt) ${number}

$\qquad$ View stack information, followed by numbers to view a few layers, positive numbers starting from the top of the stack, otherwise starting from the bottom of the stack (once from the top/bottom of the stack)

frame
frame ${stack_numbwe}down/up ${number}

$\qquad$ jumps to the tier stack (does not affect the stack)
$\qquad$ move the current stack up/down

handle
handle ${signal_name} ${keyword}

$\qquad$ to process a signal
$\qquad$signal_name has

SIGABRT -- 进程停止运行SIGALRM -- 警告钟    SIGFPE -- 算述运算例外SIGHUP -- 系统挂断SIGILL -- 非法指令SIGINT -- 终端中断SIGKILL -- 停止进程(此信号不能被忽略或捕获)SIGPIPE -- 向没有读的管道写入数据SIGSEGV -- 无效内存段访问SIGQOUT -- 终端退出SIGTERM -- 终止SIGUSR1 -- 用户定义信号SIGUSR2 -- 用户定义信号SIGCHLD -- 子进程已经停止或退出SIGCONT -- 如果被停止则继续执行SIGSTOP -- 停止执行SIGTSTP -- 终端停止信号SIGTOUT -- 后台进程请求进行写操作SIGTTIN -- 后台进程请求进行读操作

$\qquad$keyword has

nostop 发出信息,不停止程序stop 发出信息病停止程序print 仅发出信息noprint 不发出信息pass/noignore 程序处理信号nopass/ignore 不让程序处理信号

$\qquad$ sometimes want to temporarily ignore a certain signal and continue the program can be used

Artifice and Small commands print ${var_name}@${length}

$\qquad$ View the value behind a variable in memory. You can use this to detect if your error is related to the subscript crossing.

search

$\qquad$ just find the text of the source file.

End

Maybe it's over.

The introduction of Gcc&&gdb in Oi

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.