• GDB用法(三)


    预备

    测试代码参照GDB用法(二)

    命令历史

    可以将命令历史保存到文件中

    (show history) 展示当前gdb中history的设置信息

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    设置expansion
    (set history expansion) 打开历史扩展

    能使用历史处理命令对历史数据进行处理, 暂不细究

    (show history expansion) 展示历史扩展配置

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    设置save
    (set history save) 启用命令历史保存到文件

    等关闭的时候就会保存到history save中的文件夹

    (show history save) 展示命令历史保存的配置

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    设置size
    (set history size 数字) 设置保存的命令条数

    默认条数256

    (show history size) 展示设置的命令条数配置

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    设置保存文件filename
    (set history filename 文件名) 设置保存历史文件

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    (show history filename) 展示历史文件名

    默认是当前目录下的.gdb_history文件中

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    关闭gdb, 保存到文件中

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    存的是最后十条命令记录

    初始化文件(.gdbinit)

    增加.gdbinit文件
    p "自动 gdbinit 初始化完成!"
    
    • 1
    自动调用.gdbinit文件

    只能叫.gdbinit, 且目录位置要对

    1. root用户, .gdbinit文件放在~/ 目录下

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    1. 其他用户, .gdbinit文件放在/home/用户名/ 下, 对于用户本身, 其实也就是在~/目录下

      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

      先切换到普通用户

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    手动加载.gdbinit文件

    source 目录/文件 // 用linux命令读取文件内容

    文件名都不需要是.gdbinit

    在工程下新建文件testinit

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    p "手动加载 testinit文件成功"
    
    • 1

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    之前gdb设置的一些参数, 退出gdb后再进入配置就不生效了, 写入初始化文件则会一直生效

    自定义gdb命令

    (define) 自定义命令

    define 命令名

    ​ 命令

    ​ …

    end

    (document) 给自定义命令添加说明

    documnt 命令名

    ​ 说明

    end

    help 命令 // 显示说明

    stl中pvector的自定义

    define pvector
    	if $argc == 0
    		help pvector
    	else
    		set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start
    		set $capacity = $arg0._M_impl._M_end_of_storage - $arg0._M_impl._M_start
    		set $size_max = $size - 1
    	end
    	if $argc == 1
    		set $i = 0
    		while $i < $size
    			printf "elem[%u]: ", $i
    			p *($arg0._M_impl._M_start + $i)
    			set $i++
    		end
    	end
    	if $argc == 2
    		set $idx = $arg1
    		if $idx < 0 || $idx > $size_max
    			printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
    		else
    			printf "elem[%u]: ", $idx
    			p *($arg0._M_impl._M_start + $idx)
    		end
    	end
    	if $argc == 3
    	  set $start_idx = $arg1
    	  set $stop_idx = $arg2
    	  if $start_idx > $stop_idx
    	    set $tmp_idx = $start_idx
    	    set $start_idx = $stop_idx
    	    set $stop_idx = $tmp_idx
    	  end
    	  if $start_idx < 0 || $stop_idx < 0 || $start_idx > $size_max || $stop_idx > $size_max
    	    printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
    	  else
    	    set $i = $start_idx
    		while $i <= $stop_idx
    			printf "elem[%u]: ", $i
    			p *($arg0._M_impl._M_start + $i)
    			set $i++
    		end
    	  end
    	end
    	if $argc > 0
    		printf "Vector size = %u\n", $size
    		printf "Vector capacity = %u\n", $capacity
    		printf "Element "
    		whatis $arg0._M_impl._M_start
    	end
    end
    
    document pvector
    	Prints std::vector<T> information.
    	Syntax: pvector <vector> <idx1> <idx2>
    	Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1].
    	Examples:
    	pvector v - Prints vector content, size, capacity and T typedef
    	pvector v 0 - Prints element[idx] from vector
    	pvector v 1 2 - Prints elements in range [idx1..idx2] from vector
    end 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    加载其他库定义的函数

    修改工程

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    main.cpp
    #include 
    #include 
    
    using namespace std;
    
    int main(int argc,char *argv[])
    {
        cout << "参数个数:" << argc << endl;
        for(int i = 0; i < argc; i++)
        {
            cout << "参数[" << i << "]:" << argv[i] << endl;
        }
        
        vector<int> v = {1, 3};
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    Functions/pFun.h
    #ifndef _PFUN_H_
    #define _PFUN_H_
    
    #include 
    #include 
    
    void pvector_self(const std::vector<int> v);
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    Functions/pFun.cpp
    #include "pFun.h"
    
    void pvector_self(const std::vector<int> v)
    {
        if(v.empty())
        {
            std::cout << "为空" << std::endl;
        }
    
        for(int i = 0; i < v.size(); i++)
        {
            std::cout << "num" << i << ": " << v[i];
            if(i != v.size() - 1)
            {
                std::cout << std::endl;
            }  
        }
    
        std::cout << std::endl;
        std::cout << "vector 读取完毕" << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    Functions/CMakeLists.txt
    cmake_minimum_required(VERSION 3.10)
    
    AUX_SOURCE_DIRECTORY(. FUNCTIONS_SOURCE)
    
    add_library(Functions SHARED ${FUNCTIONS_SOURCE})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    CMakeLists.txt
    cmake_minimum_required(VERSION 3.10)
    
    project(main)
    
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall")
    
    INCLUDE_DIRECTORIES(./)
    
    AUX_SOURCE_DIRECTORY(./ MAIN)
    
    add_subdirectory(Functions)
    
    add_executable(main ${MAIN})
    
    target_include_directories(main PUBLIC Functions)
    target_link_libraries(main Functions)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    testinit
    p "手动加载 testinit文件成功"
    
    set $pvector_self = pvector_self
    
    • 1
    • 2
    • 3

    Functions/CMakeLists.txt 会生成 libFunctions.so, gdb就要调用这个函数的输出方法, 输出vector

    编译生成libFunctions.so和main

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 相关阅读:
    一篇搞定Sentinel-搭建Spring Cloud Alibaba服务组件Sentinel实现服务资源控制
    XTU-OJ 1247-Robb‘s Problem
    26 - 数据分析与Excel(Excel 快速入门上)
    霍夫hough变换直线检测浅显理解
    [USACO3.4] 美国血统 American Heritage
    iwebsec--代码执行漏洞
    我国实战攻防演练的发展现状
    GAN入门|第二篇:人脸图像生成(DCGAN)
    Pandas多级索引数据处理及fillna()填充方式
    jquery和jquery-ui拖动元素(vue2)
  • 原文地址:https://blog.csdn.net/zbbzb/article/details/132798016