鲲鹏社区首页
中文
注册
开发者
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

增量扫描使用场景

用户每次扫描完成之后,系统将会生成本次扫描的报告,并将完整报告保存至历史报告中。若再次扫描指定-eh为true,工具会将当前结果与上一次历史报告进行对比,若本次扫描结果与上一次内容无变更(行号可能变更),报告将会过滤,但是历史报告仍会保存完整信息。

  • 最多可保存100份历史报告,超过后将自动删除最早的报告。
  • 若代码内容不变,但是行号变化大于100行,则无法识别重复的races,从而导致过滤失败。
  1. 编译源码文件,生成可执行二进制文件。
    1
    gcc test_barrier.c -o test_barrier -g -lpthread
    

    test_barrier.c文件内容如下:

     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
    #include <unistd.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    volatile int *x,*y,*r1,*r2;
    volatile int flag;
    
    
    void thread0(void *arg)
    {
        while(flag == 0);
        *y = 1;
        *x = 1;
    }
    
    void thread1(void *arg)
    {
        while(flag == 0);
        *r2 = *x;
        *r1 = *y;
    }
    
    int main()
    {
        int n = 0;
        int count = 0;
        while(n < 100) {
            pthread_t t1, t2;
            x = (int *)malloc(sizeof(int));
            y = (int *)malloc(sizeof(int));
            r1 = (int *)malloc(sizeof(int));
            r2 = (int *)malloc(sizeof(int));
            *x = *y = *r1 = *r2 = flag = 0;
    
            pthread_create(&t1, NULL, (void *)thread0, NULL);
            pthread_create(&t2, NULL, (void *)thread1, NULL);
    
            usleep(10);
            flag = 1;
    
            pthread_join(t1, NULL);
            pthread_join(t2, NULL);
    
            if (*r1 == 0 && *r2 == 1)
            {
                printf("error %d\n", n);
                count++;
            }
    
            free((void *)x);
            free((void *)y);
            free((void *)r1);
            free((void *)r2);
            n++;
        }
        printf("Run %d times, error %d times.\n", n, count);
        return 0;
    }
    
  2. 执行内存一致性动态检查。
    devkit advisor dr-check -f /home/advisor/test_barrier

    /home/advisor/test_barrier:ELF文件路径,请根据实际情况进行修改。

    返回信息如下,从返回信息中的加粗信息可以看出有6行代码推荐修改。

    Executing dynamic memory consistency check task, please wait...
    The pid of the memtracer process is 773285
    Scanned time: 2025/11/17 08:02:55
    
    Configuration:
        ELF filepath: /home/advisor/test_barrier
        Scan source code path: None
        Generate report path: /home/DevKit_Software
        Generate report type: all
        Task Timeout Interval: The timeout period is not set.
        Log level: info
    
    Summary:
    There are 6 recommended code lines to modify.
    
    For the detailed information on multi-thread races, please check:
        /home/DevKit_Software/dr-check_test_barrier_20251117080255_2458.json
        /home/DevKit_Software/dr-check_test_barrier_20251117080255_2458.html
        /home/DevKit_Software/dr-check_test_barrier_20251117080255_2458.csv
  3. 查看扫描报告。发现存在3对数据竞争问题,1个待修改文件,6行待修改代码。
    图1 扫描报告
  4. 再次执行内存一致性动态检查。指定-eh为true开启历史报告过滤,将会自动过滤已识别的数据竞争问题
    devkit advisor dr-check -f /home/advisor/test_barrier -eh true

    返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改,但历史报告仍会保留完整的数据,包括被过滤的内容。

    Executing dynamic memory consistency check task, please wait...
    The pid of the memtracer process is 796538
    Scanned time: 2025/11/17 09:05:48
    
    Configuration:
        ELF filepath: /home/advisor/test_barrier
        Scan source code path: None
        Generate report path: /home/DevKit_Software
        Generate report type: all
        Task Timeout Interval: The timeout period is not set.
        Log level: info
    
    Summary:
    There are 0 recommended code lines to modify.
    
    For the detailed information on multi-thread races, please check:
        /home/DevKit_Software/dr-check_test_barrier_20251117090548_e08d.json
        /home/DevKit_Software/dr-check_test_barrier_20251117090548_e08d.html
        /home/DevKit_Software/dr-check_test_barrier_20251117090548_e08d.csv

    JSON报告可以视为HTML报告的另一种表现形式,若开启历史结果过滤,生成的JSON报告将包含“is_new”字段(当前报告和历史报告均含),用于标识数据是否为新增(true)或已过滤(false)。

  5. 查看历史报告。

    历史扫描报告会存储在“advisor/dr_check_reports/{elf_name}_{md5sum(elf_path)}/时间戳”文件夹下,包含以下内容:

    • source:保存源码文件。
    • dr_check_mp_时间戳_随机数.json:多进程报告,在开启多进程检测时会生成。
    • dr_check_{elf_name}_时间戳_随机数.json:多线程报告。
    • mp_时间戳.memtracer_mp_report:纯文本格式的多进程文件,在开启多进程检测时会生成,用于历史报告解析(即-dm选项)。
    • mp_时间戳.memtracer_mp_report.json:JSON格式的多进程文件,在开启多进程检测时会生成。
    • {elf_name}.memtracer_report:主进程线程报告,用于历史报告解析(即-d选项)。若开启多进程检测,将会生成一份线程报告,但是历史报告会保存多份。
    • {elf_name}_PID.memtracer_report:子进程线程报告,用于历史报告解析(即-d选项)。在开启多进程检测时会生成。
  6. 查看历史报告详情。
    1
    vi dr-check_test_barrier_20251117090548_e08d.json
    

    从报告中可以看出,历史报告保留了完整的数据。存在3对数据竞争,6行待修改代码,1个待修改文件。

    图2 JSON报告内容1

    “is_new”字段可以看出为过滤数据。

    图3 JSON报告内容2