当前位置: 首页 > news >正文

基础开发工具(下)

文章目录

    • 6. 版本控制器Git
    • 7. 调试器 - gdb/cgdb使用

6. 版本控制器Git

版本控制器Git

安装命令:

sudo yum install -y git

[gsm@VM-4-3-centos lesson8]$ git clone https://gitee.com/waves_123/linux.git
Cloning into 'linux'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (5/5), done.
[gsm@VM-4-3-centos lesson8]$ ll
total 4
drwxrwxr-x 3 gsm gsm 4096 Oct 15 22:55 linux
[gsm@VM-4-3-centos lesson8]$ cd linux/
[gsm@VM-4-3-centos linux]$ ls -al
total 24
drwxrwxr-x 3 gsm gsm 4096 Oct 15 22:55 .
drwxrwxr-x 3 gsm gsm 4096 Oct 15 22:55 ..
drwxrwxr-x 8 gsm gsm 4096 Oct 15 22:55 .git
-rw-rw-r-- 1 gsm gsm  270 Oct 15 22:55 .gitignore
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
[gsm@VM-4-3-centos linux]$ pwd
/home/gsm/112/lesson8/linux
[gsm@VM-4-3-centos linux]$ tree .git
.git
|-- branches
|-- config
|-- description
|-- HEAD
|-- hooks
|   |-- applypatch-msg.sample
|   |-- commit-msg.sample
|   |-- post-update.sample
|   |-- pre-applypatch.sample
|   |-- pre-commit.sample
|   |-- prepare-commit-msg.sample
|   |-- pre-push.sample
|   |-- pre-rebase.sample
|   `-- update.sample
|-- index
|-- info
|   `-- exclude
|-- logs
|   |-- HEAD
|   `-- refs
|       |-- heads
|       |   `-- master
|       `-- remotes
|           `-- origin
|               `-- HEAD
|-- objects
|   |-- 25
|   |   `-- 9148fa18f9fb7ef58563f4ff15fc7b172339fb
|   |-- 57
|   |   `-- 48bfe6b21665c8ee67f6efdbfb41e21fe1d67e
|   |-- 72
|   |   `-- bb7b2e0d90167921aeeb94ed970c25686ca03b
|   |-- c0
|   |   `-- d91e5cb7f3b25b9c30acda112d29f357809e56
|   |-- ea
|   |   `-- b4b77d39a0bb01a836188ab9a3a09e7f3f1845
|   |-- info
|   `-- pack
|-- packed-refs
`-- refs|-- heads|   `-- master|-- remotes|   `-- origin|       `-- HEAD`-- tags21 directories, 25 files
[gsm@VM-4-3-centos linux]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
[gsm@VM-4-3-centos linux]$ touch test.c
[gsm@VM-4-3-centos linux]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 15 23:04 test.c
[gsm@VM-4-3-centos linux]$ vim test.c 
[gsm@VM-4-3-centos linux]$ cat test.c 
#include <stdio.h>int main()
{printf("hello git!\n");return 0;
}
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ pwd
/home/gsm/112/lesson8/linux
[gsm@VM-4-3-centos linux]$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	test.c
nothing added to commit but untracked files present (use "git add" to track)
[gsm@VM-4-3-centos linux]$ git add test.c
[gsm@VM-4-3-centos linux]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   test.c
#
[gsm@VM-4-3-centos linux]$ git commit -m "添加第一个源代码test.c"
[master 517b039] 添加第一个源代码test.c1 file changed, 7 insertions(+)create mode 100644 test.c
[gsm@VM-4-3-centos linux]$ tree .git
.git
|-- branches
|-- COMMIT_EDITMSG
|-- config
|-- description
|-- HEAD
|-- hooks
|   |-- applypatch-msg.sample
|   |-- commit-msg.sample
|   |-- post-update.sample
|   |-- pre-applypatch.sample
|   |-- pre-commit.sample
|   |-- prepare-commit-msg.sample
|   |-- pre-push.sample
|   |-- pre-rebase.sample
|   `-- update.sample
|-- index
|-- info
|   `-- exclude
|-- logs
|   |-- HEAD
|   `-- refs
|       |-- heads
|       |   `-- master
|       `-- remotes
|           `-- origin
|               `-- HEAD
|-- objects
|   |-- 25
|   |   `-- 9148fa18f9fb7ef58563f4ff15fc7b172339fb
|   |-- 2a
|   |   `-- 5cff60a2b4e1a50152c627c72d9870b0dee309
|   |-- 51
|   |   `-- 7b039195309afb3adb5ed71683d5d785f8f026
|   |-- 57
|   |   `-- 48bfe6b21665c8ee67f6efdbfb41e21fe1d67e
|   |-- 72
|   |   `-- bb7b2e0d90167921aeeb94ed970c25686ca03b
|   |-- c0
|   |   `-- d91e5cb7f3b25b9c30acda112d29f357809e56
|   |-- d9
|   |   `-- 723cbf8480902a6cc3b4568caecb84a765c51d
|   |-- ea
|   |   `-- b4b77d39a0bb01a836188ab9a3a09e7f3f1845
|   |-- info
|   `-- pack
|-- packed-refs
`-- refs|-- heads|   `-- master|-- remotes|   `-- origin|       `-- HEAD`-- tags24 directories, 29 files
[gsm@VM-4-3-centos linux]$ cd .git
[gsm@VM-4-3-centos .git]$ ll
total 48
drwxrwxr-x  2 gsm gsm 4096 Oct 15 22:55 branches
-rw-rw-r--  1 gsm gsm   31 Oct 15 23:13 COMMIT_EDITMSG
-rw-rw-r--  1 gsm gsm  262 Oct 15 22:55 config
-rw-rw-r--  1 gsm gsm   73 Oct 15 22:55 description
-rw-rw-r--  1 gsm gsm   23 Oct 15 22:55 HEAD
drwxrwxr-x  2 gsm gsm 4096 Oct 15 22:55 hooks
-rw-rw-r--  1 gsm gsm  336 Oct 15 23:08 index
drwxrwxr-x  2 gsm gsm 4096 Oct 15 22:55 info
drwxrwxr-x  3 gsm gsm 4096 Oct 15 22:55 logs
drwxrwxr-x 12 gsm gsm 4096 Oct 15 23:13 objects
-rw-rw-r--  1 gsm gsm  107 Oct 15 22:55 packed-refs
drwxrwxr-x  5 gsm gsm 4096 Oct 15 22:55 refs
[gsm@VM-4-3-centos .git]$ cd objects/
[gsm@VM-4-3-centos objects]$ ll
total 40
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 25
drwxrwxr-x 2 gsm gsm 4096 Oct 15 23:13 2a
drwxrwxr-x 2 gsm gsm 4096 Oct 15 23:13 51
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 57
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 72
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 c0
drwxrwxr-x 2 gsm gsm 4096 Oct 15 23:08 d9
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 ea
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 info
drwxrwxr-x 2 gsm gsm 4096 Oct 15 22:55 pack
[gsm@VM-4-3-centos objects]$ cd ..
[gsm@VM-4-3-centos .git]$ cd ..
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 370 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 7e4b4085
To https://gitee.com/waves_123/linux.giteab4b77..517b039  master -> master
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ pwd
/home/gsm/112/lesson8/linux
[gsm@VM-4-3-centos linux]$ git log
commit 517b039195309afb3adb5ed71683d5d785f8f026
Author: waves浪游 <1600665451@qq.com>
Date:   Wed Oct 15 23:13:38 2025 +0800添加第一个源代码test.ccommit eab4b77d39a0bb01a836188ab9a3a09e7f3f1845
Author: waves浪游 <1600665451@qq.com>
Date:   Wed Oct 15 14:48:59 2025 +0000Initial commit
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ ls -al
total 28
drwxrwxr-x 3 gsm gsm 4096 Oct 15 23:04 .
drwxrwxr-x 3 gsm gsm 4096 Oct 15 22:55 ..
drwxrwxr-x 8 gsm gsm 4096 Oct 15 23:27 .git
-rw-rw-r-- 1 gsm gsm  270 Oct 15 22:55 .gitignore
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ cat .gitignore 
# Prerequisites
*.d# Compiled Object files
*.slo
*.lo
*.o
*.obj# Precompiled Headers
*.gch
*.pch# Compiled Dynamic libraries
*.so
*.dylib
*.dll# Fortran module files
*.mod
*.smod# Compiled Static libraries
*.lai
*.la
*.a
*.lib# Executables
*.exe
*.out
*.app
[gsm@VM-4-3-centos linux]$ vim .gitignore 
[gsm@VM-4-3-centos linux]$ cat .gitignore 
*.txt
# Prerequisites
*.d# Compiled Object files
*.slo
*.lo
*.o
*.obj# Precompiled Headers
*.gch
*.pch# Compiled Dynamic libraries
*.so
*.dylib
*.dll# Fortran module files
*.mod
*.smod# Compiled Static libraries
*.lai
*.la
*.a
*.lib# Executables
*.exe
*.out
*.app
[gsm@VM-4-3-centos linux]$ touch test1.c
[gsm@VM-4-3-centos linux]$ touch test2.c
[gsm@VM-4-3-centos linux]$ touch test3.txt
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ git add .
[gsm@VM-4-3-centos linux]$ git commit -m "ignore test"
[master 3b709c0] ignore test3 files changed, 1 insertion(+)create mode 100644 test1.ccreate mode 100644 test2.c
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 415 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 8d46c9b7
To https://gitee.com/waves_123/linux.git517b039..3b709c0  master -> master
[gsm@VM-4-3-centos linux]$ git status
# On branch master
nothing to commit, working directory clean
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ cp ../../lesson7//processbar/ . -rf
[gsm@VM-4-3-centos linux]$ ll
total 16
drwxrwxr-x 2 gsm gsm 4096 Oct 16 14:52 processbar
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
[gsm@VM-4-3-centos linux]$ tree processbar/
processbar/
|-- main.c
|-- Makefile
|-- process-backup20251013_02.c
|-- process-backup20251013.c
|-- process.c
`-- process.h0 directories, 6 files
[gsm@VM-4-3-centos linux]$ git add .
[gsm@VM-4-3-centos linux]$ git commit -m "进度条代码"
[master 8d2b679] 进度条代码6 files changed, 220 insertions(+)create mode 100644 processbar/Makefilecreate mode 100644 processbar/main.ccreate mode 100644 processbar/process-backup20251013.ccreate mode 100644 processbar/process-backup20251013_02.ccreate mode 100644 processbar/process.ccreate mode 100644 processbar/process.h
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
To https://gitee.com/waves_123/linux.git! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/waves_123/linux.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[gsm@VM-4-3-centos linux]$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), done.
From https://gitee.com/waves_123/linuxdb997bb..5b33cf3  master     -> origin/master
Error detected while processing /home/gsm/.vimrc:
line    5:
E492: Not an editor command: Plug 'Valloric/YouCompleteMe'
line    6:
E492: Not an editor command: Plug 'bling/vim-airline'
line    7:
E492: Not an editor command: Plug 'vim-airline/vim-airline-themes'
line    8:
E492: Not an editor command: Plug 'morhetz/gruvbox'
line    9:
E492: Not an editor command: Plug 'octol/vim-cpp-enhanced-highlight'
line   10:
E492: Not an editor command: Plug 'SirVer/ultisnips'
line   11:
E492: Not an editor command: Plug 'tpope/vim-surround'
line   12:
E492: Not an editor command: Plug 'flazz/vim-colorschemes'
line   13:
E492: Not an editor command: Plug 'scrooloose/nerdtree'
line   14:
E492: Not an editor command: Plug 'python-mode/python-mode'
line   15:
E492: Not an editor command: Plug 'scrooloose/nerdcommenter'
line   16:
E492: Not an editor command: Plug 'Yggdroot/LeaderF'
line   17:
E492: Not an editor command: Plug 'cpiger/NeoDebug'
line   18:
E492: Not an editor command: Plug 'ryanoasis/vim-devicons'
line   22:
E492: Not an editor command: Plug 'autozimu/LanguageClient-neovim', { 'branch': 'next', 'do': 'bash install.sh', }
line   23:
E492: Not an editor command: Plug 'fatih/vim-go'
line   30:
E185: Cannot find color scheme 'luna-term'
line   59:
E518: Unknown option: foldmethod=marker
line  142:
E518: Unknown option: foldenable
line  150:
E518: Unknown option: foldmethod=indent
line  151:
E518: Unknown option: foldlevel=99
line  295:
E31: No such mapping
line  296:
E31: No such mapping
line  297:
E31: No such mapping
line  298:
E31: No such mapping
line  299:
E31: No such mapping
line  300:
E31: No such mapping
line  301:
E31: No such mapping
line  302:
E31: No such mapping
line  307:
E492: Not an editor command: ^Iterminal
Press ENTER or type command to continue
Merge made by the 'recursive' strategy.windowscode/hello_windows.txt | 2 ++1 file changed, 2 insertions(+)create mode 100644 windowscode/hello_windows.txt
[gsm@VM-4-3-centos linux]$ ll
total 20
drwxrwxr-x 2 gsm gsm 4096 Oct 16 14:52 processbar
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
drwxrwxr-x 2 gsm gsm 4096 Oct 16 14:56 windowscode
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
Counting objects: 13, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 2.68 KiB | 0 bytes/s, done.
Total 11 (delta 3), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag a3445193
To https://gitee.com/waves_123/linux.git5b33cf3..aeda6ce  master -> master
[gsm@VM-4-3-centos linux]$ vim processbar/main.c 
[gsm@VM-4-3-centos linux]$ ll
total 20
drwxrwxr-x 2 gsm gsm 4096 Oct 16 14:52 processbar
-rw-rw-r-- 1 gsm gsm  949 Oct 15 22:55 README.en.md
-rw-rw-r-- 1 gsm gsm 1310 Oct 15 22:55 README.md
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test1.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test2.c
-rw-rw-r-- 1 gsm gsm    0 Oct 16 14:28 test3.txt
-rw-rw-r-- 1 gsm gsm   77 Oct 15 23:05 test.c
drwxrwxr-x 2 gsm gsm 4096 Oct 16 14:56 windowscode
[gsm@VM-4-3-centos linux]$ git add .
[gsm@VM-4-3-centos linux]$ git commit -m "Linux mod"
[master 51dec78] Linux mod1 file changed, 9 insertions(+)
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
To https://gitee.com/waves_123/linux.git! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/waves_123/linux.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[gsm@VM-4-3-centos linux]$ git pull
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), done.
From https://gitee.com/waves_123/linuxaeda6ce..49783b3  master     -> origin/master
Auto-merging processbar/main.c
CONFLICT (content): Merge conflict in processbar/main.c
Automatic merge failed; fix conflicts and then commit the result.
[gsm@VM-4-3-centos linux]$ cat processbar/main.c 
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "process.h"typedef void (*flush_t)(double total, double current);// 这是一个刷新的函数指针类型const int base = 100;
double total = 2048.0; // 2048MB
double once = 0.1;// 进度条的调用方式
void download(flush_t f)
{double current = 0.0;while (current < total){// 模拟下载行为int r = rand() % base + 1;double speed = r * once;current += speed;if (current >= total){current = total;}usleep(10000);// 更新出了本次新的下载量// 根据真实的应用场景,进行动态刷新//Process(total, 1.0);f(total, current);//printf("test:%.1lf/%.1lf\r", current, total);//fflush(stdout);}printf("\n");
}int main()
{srand(time(NULL));download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);<<<<<<< HEAD// 下面是linux的修改download(FlushProcess);download(FlushProcess);
=======//下面的代码是新增的
>>>>>>> 49783b3d2c3ea8af97c50dcc0db3e6edd1b4f6d3download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);return 0;
}
[gsm@VM-4-3-centos linux]$ vim processbar/main.c 
[gsm@VM-4-3-centos linux]$ cat processbar/main.c 
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "process.h"typedef void (*flush_t)(double total, double current);// 这是一个刷新的函数指针类型const int base = 100;
double total = 2048.0; // 2048MB
double once = 0.1;// 进度条的调用方式
void download(flush_t f)
{double current = 0.0;while (current < total){// 模拟下载行为int r = rand() % base + 1;double speed = r * once;current += speed;if (current >= total){current = total;}usleep(10000);// 更新出了本次新的下载量// 根据真实的应用场景,进行动态刷新//Process(total, 1.0);f(total, current);//printf("test:%.1lf/%.1lf\r", current, total);//fflush(stdout);}printf("\n");
}int main()
{srand(time(NULL));download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);// 下面是linux的修改download(FlushProcess);download(FlushProcess);//下面的代码是新增的download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);download(FlushProcess);return 0;
}
[gsm@VM-4-3-centos linux]$ git add .
[gsm@VM-4-3-centos linux]$ git commit -m "fix conflict"
[master 8f91b54] fix conflict
[gsm@VM-4-3-centos linux]$ git push
Username for 'https://gitee.com': waves_123
Password for 'https://waves_123@gitee.com': 
Counting objects: 14, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 764 bytes | 0 bytes/s, done.
Total 8 (delta 6), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 1b423ab3
To https://gitee.com/waves_123/linux.git49783b3..8f91b54  master -> master

7. 调试器 - gdb/cgdb使用

调试器 - gdb/cgdb使用

[gsm@VM-4-3-centos lesson9]$ touch code.c
[gsm@VM-4-3-centos lesson9]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct 16 22:02 code.c
[gsm@VM-4-3-centos lesson9]$ vim code.c 
[gsm@VM-4-3-centos lesson9]$ cat code.c
#include <stdio.h>int Sum(int s, int e)
{int sum = 0;int i = s;for (; i <= e; i++){sum += i;}return sum;
}int main()
{printf("process is running\n");int start = 1;int end = 100;int result = Sum(start, end);printf("process is done, result:%d\n", result);return 0;
}
[gsm@VM-4-3-centos lesson9]$ touch Makefile
[gsm@VM-4-3-centos lesson9]$ vim Makefile 
[gsm@VM-4-3-centos lesson9]$ cat Makefile 
myexe:code.cgcc -o $@ $^
.PHONY:clean
clean:rm -f myexe
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe code.c
[gsm@VM-4-3-centos lesson9]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  345 Oct 16 22:18 code.c
-rw-rw-r-- 1 gsm gsm   60 Oct 16 22:33 Makefile
-rwxrwxr-x 1 gsm gsm 8440 Oct 16 22:47 myexe
[gsm@VM-4-3-centos lesson9]$ ./myexe 
process is running
process is done, result:5050
[gsm@VM-4-3-centos lesson9]$ vim Makefile 
[gsm@VM-4-3-centos lesson9]$ cat Makefile 
myexe-debug:code.cgcc -o $@ $^ -g
.PHONY:clean
clean:rm -f myexe
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe-debug code.c -g
[gsm@VM-4-3-centos lesson9]$ ll
total 32
-rw-rw-r-- 1 gsm gsm  345 Oct 16 22:18 code.c
-rw-rw-r-- 1 gsm gsm   69 Oct 16 22:51 Makefile
-rwxrwxr-x 1 gsm gsm 8440 Oct 16 22:47 myexe
-rwxrwxr-x 1 gsm gsm 9688 Oct 16 22:51 myexe-debug
[gsm@VM-4-3-centos lesson9]$ readelf -S myexe | grep -i debug
[gsm@VM-4-3-centos lesson9]$ readelf -S myexe-debug | grep -i debug[27] .debug_aranges    PROGBITS         0000000000000000  00001069[28] .debug_info       PROGBITS         0000000000000000  00001099[29] .debug_abbrev     PROGBITS         0000000000000000  000011a9[30] .debug_line       PROGBITS         0000000000000000  00001234[31] .debug_str        PROGBITS         0000000000000000  0000128c
[gsm@VM-4-3-centos lesson9]$ vim Makefile 
[gsm@VM-4-3-centos lesson9]$ cat Makefile 
myexe:code.cgcc -o $@ $^ -g
.PHONY:clean
clean:rm -f myexe
[gsm@VM-4-3-centos lesson9]$ make clean
rm -f myexe
[gsm@VM-4-3-centos lesson9]$ rm myexe-debug 
[gsm@VM-4-3-centos lesson9]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 345 Oct 16 22:18 code.c
-rw-rw-r-- 1 gsm gsm  63 Oct 16 23:00 Makefile
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe code.c -g
[gsm@VM-4-3-centos lesson9]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  345 Oct 16 22:18 code.c
-rw-rw-r-- 1 gsm gsm   63 Oct 16 23:00 Makefile
-rwxrwxr-x 1 gsm gsm 9688 Oct 16 23:01 myexe
[gsm@VM-4-3-centos lesson9]$ gdb myexe 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) l 15
10	        sum += i;
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
(gdb) l Sum
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) l main
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
21	    int end = 100;
(gdb) l code.c:5
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) l code.c:12
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
(gdb) quit
[gsm@VM-4-3-centos lesson9]$ gdb myexe 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) 
Line number 28 out of range; code.c has 27 lines.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is running
process is done, result:5050
[Inferior 1 (process 3892) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.3.x86_64
(gdb) b 20
Breakpoint 1 at 0x4005bf: file code.c, line 20.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005bf in main at code.c:20
(gdb) b 21
Breakpoint 2 at 0x4005c6: file code.c, line 21.
(gdb) b 22
Breakpoint 3 at 0x4005cd: file code.c, line 22.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005bf in main at code.c:20
2       breakpoint     keep y   0x00000000004005c6 in main at code.c:21
3       breakpoint     keep y   0x00000000004005cd in main at code.c:22
(gdb) d 3
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005bf in main at code.c:20
2       breakpoint     keep y   0x00000000004005c6 in main at code.c:21
(gdb) d 2
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005bf in main at code.c:20
(gdb) d 1
(gdb) info b
No breakpoints or watchpoints.
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) 
Line number 28 out of range; code.c has 27 lines.
(gdb) b code.c:20
Breakpoint 4 at 0x4005bf: file code.c, line 20.
(gdb) info b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x00000000004005bf in main at code.c:20
(gdb) b code.c:main
Breakpoint 5 at 0x4005b5: file code.c, line 18.
(gdb) info b
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x00000000004005bf in main at code.c:20
5       breakpoint     keep y   0x00000000004005b5 in main at code.c:18
(gdb) quit
[gsm@VM-4-3-centos lesson9]$ gdb myexe 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) info b
No breakpoints or watchpoints.
(gdb) b 20
Breakpoint 1 at 0x4005bf: file code.c, line 20.
(gdb) b 21
Breakpoint 2 at 0x4005c6: file code.c, line 21.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005bf in main at code.c:20
2       breakpoint     keep y   0x00000000004005c6 in main at code.c:21
(gdb) disable 1
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x00000000004005bf in main at code.c:20
2       breakpoint     keep y   0x00000000004005c6 in main at code.c:21
(gdb) disable 2
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x00000000004005bf in main at code.c:20
2       breakpoint     keep n   0x00000000004005c6 in main at code.c:21
(gdb) enable 2
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x00000000004005bf in main at code.c:20
2       breakpoint     keep y   0x00000000004005c6 in main at code.c:21
(gdb) quit
[gsm@VM-4-3-centos lesson9]$ gdb myexe 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is running
process is done, result:5050
[Inferior 1 (process 15497) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.3.x86_64
(gdb) b 18
Breakpoint 1 at 0x4005b5: file code.c, line 18.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b5 in main at code.c:18
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe Breakpoint 1, main () at code.c:18
18	    printf("process is running\n");
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b5 in main at code.c:18breakpoint already hit 1 time
(gdb) n
process is running
20	    int start = 1;
(gdb) 
21	    int end = 100;
(gdb) 
22	    int result = Sum(start, end);
(gdb) n
24	    printf("process is done, result:%d\n", result);
(gdb) 
process is done, result:5050
26	    return 0;
(gdb) 
27	}
(gdb) 
0x00007ffff7a2f555 in __libc_start_main () from /lib64/libc.so.6
(gdb) 
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 15639) exited normally]
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b5 in main at code.c:18breakpoint already hit 1 time
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe Breakpoint 1, main () at code.c:18
18	    printf("process is running\n");
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) yStarting program: /home/gsm/linux/112/lesson9/myexe Breakpoint 1, main () at code.c:18
18	    printf("process is running\n");
(gdb) n
process is running
20	    int start = 1;
(gdb) 
21	    int end = 100;
(gdb) 
22	    int result = Sum(start, end);
(gdb) s
Sum (s=1, e=100) at code.c:5
5	    int sum = 0;
(gdb) bt
#0  Sum (s=1, e=100) at code.c:5
#1  0x00000000004005dc in main () at code.c:22
(gdb) n
6	    int i = s;
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) n
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) display i
1: i = 11
(gdb) display sum
2: sum = 66
(gdb) n
10	        sum += i;
2: sum = 66
1: i = 12
(gdb) 
8	    for (; i <= e; i++)
2: sum = 78
1: i = 12
(gdb) 
10	        sum += i;
2: sum = 78
1: i = 13
(gdb) 
8	    for (; i <= e; i++)
2: sum = 91
1: i = 13
(gdb) 
10	        sum += i;
2: sum = 91
1: i = 14
(gdb) 
8	    for (; i <= e; i++)
2: sum = 105
1: i = 14
(gdb) 
10	        sum += i;
2: sum = 105
1: i = 15
(gdb) 
8	    for (; i <= e; i++)
2: sum = 120
1: i = 15
(gdb) 
10	        sum += i;
2: sum = 120
1: i = 16
(gdb) 
8	    for (; i <= e; i++)
2: sum = 136
1: i = 16
(gdb) 
10	        sum += i;
2: sum = 136
1: i = 17
(gdb) 
8	    for (; i <= e; i++)
2: sum = 153
1: i = 17
(gdb) undisplay 1
(gdb) n
10	        sum += i;
2: sum = 153
(gdb) 
8	    for (; i <= e; i++)
2: sum = 171
(gdb) display i
3: i = 18
(gdb) display &sum
4: &sum = (int *) 0x7fffffffe41c
(gdb) display &i
5: &i = (int *) 0x7fffffffe418
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) n
10	        sum += i;
5: &i = (int *) 0x7fffffffe418
4: &sum = (int *) 0x7fffffffe41c
3: i = 19
2: sum = 171
(gdb) 
8	    for (; i <= e; i++)
5: &i = (int *) 0x7fffffffe418
4: &sum = (int *) 0x7fffffffe41c
3: i = 19
2: sum = 190
(gdb) 
10	        sum += i;
5: &i = (int *) 0x7fffffffe418
4: &sum = (int *) 0x7fffffffe41c
3: i = 20
2: sum = 190
(gdb) 
8	    for (; i <= e; i++)
5: &i = (int *) 0x7fffffffe418
4: &sum = (int *) 0x7fffffffe41c
3: i = 20
2: sum = 210
(gdb) undisplay 2
(gdb) undisplay 3
(gdb) undisplay 4
(gdb) undisplay 5
(gdb) n
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) p sum
$1 = 276
(gdb) p i
$2 = 24
(gdb) n
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) p sum
$3 = 325
(gdb) p i
$4 = 25
(gdb) p &sum
$5 = (int *) 0x7fffffffe41c
(gdb) p i+sum
$6 = 350
(gdb) p 1+1
$7 = 2
(gdb) n
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) 
10	        sum += i;
(gdb) 
8	    for (; i <= e; i++)
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) until 13
Sum (s=1, e=100) at code.c:13
13	    return sum;
(gdb) p sum
$8 = 5050
(gdb) n
14	}
(gdb) 
main () at code.c:24
24	    printf("process is done, result:%d\n", result);
(gdb) bt
#0  main () at code.c:24
(gdb) l 1
1	#include <stdio.h>
2	
3	int Sum(int s, int e)
4	{
5	    int sum = 0;
6	    int i = s;
7	
8	    for (; i <= e; i++)
9	    {
10	        sum += i;
(gdb) 
11	    }
12	
13	    return sum;
14	}
15	
16	int main()
17	{
18	    printf("process is running\n");
19	
20	    int start = 1;
(gdb) 
21	    int end = 100;
22	    int result = Sum(start, end);
23	
24	    printf("process is done, result:%d\n", result);
25	
26	    return 0;
27	}
(gdb) quit
A debugging session is active.Inferior 1 [process 19178] will be killed.Quit anyway? (y or n) y

cgdb安装命令:

sudo yum install -y cgdb

[gsm@VM-4-3-centos lesson9]$ vim code.c 
[gsm@VM-4-3-centos lesson9]$ cat code.c 
#include <stdio.h>int Sum(int s, int e)
{int sum = 0;int i = s;for (; i <= e; i++){sum += i;}return sum;
}int Add(int x, int y)
{return x + y;
}int div(int x, int y)
{int z = x / y;return z;
}int main()
{printf("process is running\n");int start = 1;int end = 100;int result = Sum(start, end);printf("process is done, result:%d\n", result);int a = div(10, 0);printf("process is done, div result:%d\n", a);int b = Add(10, 20);printf("process is done, add result:%d\n", b);return 0;
}
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe code.c -g
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	34
Breakpoint 1 at 0x40062c: file code.c, line 34.
(gdb) b	37
Breakpoint 2 at 0x400652: file code.c, line 37.
(gdb) b	40
Breakpoint 3 at 0x400678: file code.c, line 40.
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x000000000040062c in main at code.c:34
2	breakpoint     keep y   0x0000000000400652 in main at code.c:37
3	breakpoint     keep y   0x0000000000400678 in main at code.c:40
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:34
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) n
process is done, result:5050
(gdb) c
Continuing.Program received signal SIGFPE, Arithmetic exception.
0x00000000004005ef in div (x=10, y=0) at code.c:23
(gdb) quit
A debugging session is active.Inferior 1 [process 7983] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	36
Breakpoint 1 at 0x400640: file code.c, line 36.
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x0000000000400640 in main at code.c:36
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is running
process is done, result:5050Breakpoint 1, main () at code.c:36
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
div (x=10, y=0) at code.c:23
(gdb) nProgram received signal SIGFPE, Arithmetic exception.
0x00000000004005ef in div (x=10, y=0) at code.c:23
(gdb) quit
A debugging session is active.Inferior 1 [process 8565] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ vim code.c 
[gsm@VM-4-3-centos lesson9]$ cat code.c 
#include <stdio.h>int Sum(int s, int e)
{int sum = 0;int i = s;for (; i <= e; i++){sum += i;}return sum;
}int Add(int x, int y)
{return x + y;
}int div(int x, int y)
{if (y == 0){return -1;}int z = x / y;return z;
}int main()
{printf("process is running\n");int start = 1;int end = 100;int result = Sum(start, end);printf("process is done, result:%d\n", result);int a = div(10, 0);printf("process is done, div result:%d\n", a);int b = Add(10, 20);printf("process is done, add result:%d\n", b);return 0;
}
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe code.c -g
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	38
Breakpoint 1 at 0x400627: file code.c, line 38.
(gdb) b	41
Breakpoint 2 at 0x40064d: file code.c, line 41.
(gdb) b	44
Breakpoint 3 at 0x400673: file code.c, line 44.
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x0000000000400627 in main at code.c:38
2	breakpoint     keep y   0x000000000040064d in main at code.c:41
3	breakpoint     keep y   0x0000000000400673 in main at code.c:44
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:38
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
Sum (s=1, e=100) at code.c:5
(gdb) finish
Run till exit from #0  Sum (s=1, e=100) at code.c:5
0x0000000000400636 in main () at code.c:38
Value returned is $1 = 5050
(gdb) c
process is done, result:5050
Continuing.Breakpoint 2, main () at code.c:41
(gdb) s
div (x=10, y=0) at code.c:23
(gdb) finish
Run till exit from #0  div (x=10, y=0) at code.c:23
0x000000000040065c in main () at code.c:41
Value returned is $2 = -1
(gdb) quit
A debugging session is active.Inferior 1 [process 11068] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) info locals
No frame selected.
(gdb) b	45
Breakpoint 1 at 0x400685: file code.c, line 45.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is running
process is done, result:5050
process is done, div result:-1Breakpoint 1, main () at code.c:45
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) info locals
start = 1
end = 100
result = 5050
a = -1
b = 30
(gdb) quit
A debugging session is active.Inferior 1 [process 11948] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	38
Breakpoint 1 at 0x400627: file code.c, line 38.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:38
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
Sum (s=1, e=100) at code.c:5
(gdb) watch sum
Hardware watchpoint 2: sum
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x0000000000400627 in main at code.c:38breakpoint already hit 1 time
2	hw watchpoint  keep y                      sum
(gdb) n
(gdb) n
(gdb) n
(gdb) n
Hardware watchpoint 2: sumOld value = 0
New value = 1
Sum (s=1, e=100) at code.c:8
(gdb) watch i
Hardware watchpoint 3: i
(gdb) n
Hardware watchpoint 3: iOld value = 1
New value = 2
0x00000000004005c0 in Sum (s=1, e=100) at code.c:8
(gdb) 
(gdb) 
Hardware watchpoint 2: sumOld value = 1
New value = 3
Sum (s=1, e=100) at code.c:8
(gdb) 
Hardware watchpoint 3: iOld value = 2
New value = 3
0x00000000004005c0 in Sum (s=1, e=100) at code.c:8
(gdb) 
(gdb) 
Hardware watchpoint 2: sumOld value = 3
New value = 6
Sum (s=1, e=100) at code.c:8
(gdb) quit
A debugging session is active.Inferior 1 [process 12877] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	38
Breakpoint 1 at 0x400627: file code.c, line 38.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:38
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
Sum (s=1, e=100) at code.c:5
(gdb) n
(gdb) n
(gdb) n
(gdb) watch sum
Hardware watchpoint 2: sum
(gdb) watch i  
Hardware watchpoint 3: i
(gdb) n
Hardware watchpoint 2: sumOld value = 0
New value = 1
Sum (s=1, e=100) at code.c:8
(gdb) 
Hardware watchpoint 3: iOld value = 1
New value = 2
0x00000000004005c0 in Sum (s=1, e=100) at code.c:8
(gdb) 
(gdb) 
Hardware watchpoint 2: sumOld value = 1
New value = 3
Sum (s=1, e=100) at code.c:8
(gdb) 
Hardware watchpoint 3: iOld value = 2
New value = 3
0x00000000004005c0 in Sum (s=1, e=100) at code.c:8
(gdb) 
(gdb) 
Hardware watchpoint 2: sumOld value = 3
New value = 6
Sum (s=1, e=100) at code.c:8
(gdb) 
Hardware watchpoint 3: iOld value = 3
New value = 4
0x00000000004005c0 in Sum (s=1, e=100) at code.c:8
(gdb) p	e
$1 = 100
(gdb) set var e=5
(gdb) p	e
$2 = 5
(gdb) n
(gdb) n
Hardware watchpoint 2: sumOld value = 6
New value = 10
Sum (s=1, e=5) at code.c:8
(gdb) n
Hardware watchpoint 3: iOld value = 4
New value = 5
0x00000000004005c0 in Sum (s=1, e=5) at code.c:8
(gdb) n
(gdb) n
Hardware watchpoint 2: sumOld value = 10
New value = 15
Sum (s=1, e=5) at code.c:8
(gdb) n
Hardware watchpoint 3: iOld value = 5
New value = 6
0x00000000004005c0 in Sum (s=1, e=5) at code.c:8
(gdb) n
(gdb) p	sum
$3 = 15
(gdb) quit
A debugging session is active.Inferior 1 [process 14295] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ vim code.c 
[gsm@VM-4-3-centos lesson9]$ cat code.c 
#include <stdio.h>int flag = 0;int Sum(int s, int e)
{int sum = 0;int i = s;for (; i <= e; i++){sum += i;}return sum * flag;
}int Add(int x, int y)
{return x + y;
}int div(int x, int y)
{if (y == 0){return -1;}int z = x / y;return z;
}int main()
{printf("process is running\n");int start = 1;int end = 100;int result = Sum(start, end);printf("process is done, result:%d\n", result);int a = div(10, 0);printf("process is done, div result:%d\n", a);int b = Add(10, 20);printf("process is done, add result:%d\n", b);return 0;
}
[gsm@VM-4-3-centos lesson9]$ make
gcc -o myexe code.c -g
[gsm@VM-4-3-centos lesson9]$ ./myexe 
process is running
process is done, result:0
process is done, div result:-1
process is done, add result:30
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	40
Breakpoint 1 at 0x40062e: file code.c, line 40.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:40
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
Sum (s=1, e=100) at code.c:7
(gdb) until 15
Sum (s=1, e=100) at code.c:15
(gdb) p	sum
$1 = 5050
(gdb) p	flag
$2 = 0
(gdb) set var flag=-1
(gdb) n
(gdb) n
main () at code.c:41
(gdb) n
process is done, result:-5050
(gdb) quit
A debugging session is active.Inferior 1 [process 17224] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	40
Breakpoint 1 at 0x40062e: file code.c, line 40.
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:40
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) s
Sum (s=1, e=100) at code.c:7
(gdb) b	12 if i == 10
Breakpoint 2 at 0x4005b6: file code.c, line 12.
(gdb) display i
1: i = 4195504
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x000000000040062e in main at code.c:40breakpoint already hit 1 time
2	breakpoint     keep y   0x00000000004005b6 in Sum at code.c:12stop only if i == 10
(gdb) finish
Run till exit from #0  Sum (s=1, e=100) at code.c:7Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: i = 10
(gdb) finish
Run till exit from #0  Sum (s=1, e=100) at code.c:12
0x000000000040063d in main () at code.c:40
Value returned is $1 = 0
(gdb) quit
A debugging session is active.Inferior 1 [process 19530] will be killed.Quit anyway? (y or n) y
[gsm@VM-4-3-centos lesson9]$ cgdb myexe 
[?1034h[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl
.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copyi
ng"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/gsm/linux/112/lesson9/myexe...done.
(gdb) b	40
Breakpoint 1 at 0x40062e: file code.c, line 40.
(gdb) b	12
Breakpoint 2 at 0x4005b6: file code.c, line 12.
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x000000000040062e in main at code.c:40
2	breakpoint     keep y   0x00000000004005b6 in Sum at code.c:12
(gdb) r
Starting program: /home/gsm/linux/112/lesson9/myexe 
process is runningBreakpoint 1, main () at code.c:40
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_
9.3.x86_64
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
(gdb) info b
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x000000000040062e in main at code.c:40breakpoint already hit 1 time
2	breakpoint     keep y   0x00000000004005b6 in Sum at code.c:12breakpoint already hit 1 time
(gdb) display sum
1: sum = 0
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 1
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 3
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 6
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 10
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 15
(gdb) c
Continuing.Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 21
(gdb) p	i
$1 = 7
(gdb) condition	2 i==10 
(gdb) info b  
Num     Type           Disp Enb Address            What
1	breakpoint     keep y   0x000000000040062e in main at code.c:40breakpoint already hit 1 time
2	breakpoint     keep y   0x00000000004005b6 in Sum at code.c:12stop only if i==10breakpoint already hit 7 times
(gdb) finish
Run till exit from #0  Sum (s=1, e=100) at code.c:12Breakpoint 2, Sum (s=1, e=100) at code.c:12
1: sum = 45
(gdb) p	i
$2 = 10
(gdb) until 15
Sum (s=1, e=100) at code.c:15
1: sum = 5050
(gdb) p	i
$3 = 101
(gdb) d	2
(gdb) quit
A debugging session is active.Inferior 1 [process 24279] will be killed.Quit anyway? (y or n) y
http://www.dtcms.com/a/589148.html

相关文章:

  • 一文详解分布式事务
  • HarmonyOS DataShareExtension深度解析:构建安全高效的数据共享架构
  • 团风做网站网站建站前期准备工作
  • .net 网站开发架构企业网站设置
  • 面试题 16.25. LRU 缓存
  • st表详解
  • 企业网站优化甲薇g71679做同等效果下拉词做网站白云
  • 9、webgl 基本概念 + 复合变换 + 平面内容复习
  • gRPC C++库架构与异步编程实践
  • 做网站如何收益发送wordpress
  • 人工智能备考——4部分总结
  • Vite.js 快速入门指南 (React + JavaScript 版)
  • 如何建微信商城网站wordpress手机模板
  • 基于springboot纺织品企业财务管理系统【带源码和文档】
  • CHAR、VARCHAR、TEXT 的差别与存储方式
  • QtMainWindow C++详解:构建桌面应用的核心框架
  • 红帽虚拟机,NG搭建网站练习
  • EntryAbility继承FlutterAbility应用入口深度解析
  • (3)项目启航:Qt实战项目之创建项目
  • 补充说明:Windows 完全可以开发 Qt 鸿蒙应用!(附专属适配方案)
  • Apache 工具包(commons-io commons-lang3 )保姆介绍
  • 大小鼠跑步机 小动物跑台 动物跑步机 大鼠实验跑台
  • 哪里网站建设联系方式ppt模板下载网
  • PHP Mail:高效邮件发送的解决方案详解
  • 分布式专题——48 ElasticSearch聚合操作详解
  • 免费品牌网站制作给娃娃做衣服卖的网站
  • 【AI大模型技术】1.NLP
  • Linux应用开发-18- select、poll、epoll
  • 进程3:进程切换
  • PHP中各种超全局变量使用