c程序。。。一个莫名其妙的bug。。。。

时间:2022-09-20 10:39:25
我在linux上写了个读取配置文件的测试程序。。。然后运行结果和预期不同。。。



pavo_nandflash.c:



#define PAVO_NAND_PAGE_SIZE   2048
#define PAVO_NAND_OOB_SIZE     64
#define PAVO_NAND_PAGES          0x80000
#define PAVO_NAND_SIZE             0X8000000                  /*does not include oob*/

#include <confuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#include "types.h"
#include "nand_flash.h"

char *NAND_Flash = NULL;

char *U_boot = NULL;
uint32_t U_boot_start, U_boot_end,U_boot_add_ecc;
char *Serial = NULL;
uint32_t Serial_start, Serial_end,Serial_add_ecc;
char *Kernel = NULL;
uint32_t Kernel_start, Kernel_end,Kernel_add_ecc;
char *ROOT_FS = NULL;
uint32_t ROOT_FS_start, ROOT_FS_end, ROOT_FS_oob,ROOT_FS_add_ecc;
char *Settings = NULL;
uint32_t Settings_start, Settings_end, Settings_oob,Settings_add_ecc;
char *Program_FS = NULL;
uint32_t Program_FS_start, Program_FS_end, Program_FS_oob,Program_FS_add_ecc;
char *Data_FS = NULL;
uint32_t Data_FS_start, Data_FS_end, Data_FS_oob,Data_FS_add_ecc;
char *User_FS = NULL;
uint32_t User_FS_start, User_FS_end, User_FS_oob,User_FS_add_ecc;
char *User_Disk = NULL;
uint32_t User_Disk_start, User_Disk_end, User_Disk_oob,User_Disk_add_ecc;

static int parse_configure(char *file_name)
{
    cfg_opt_t opts[] = {
        CFG_SIMPLE_STR("NAND_Flash", &NAND_Flash),
        
        CFG_SIMPLE_STR("U_boot", &U_boot),
        CFG_SIMPLE_INT("U_boot_start", &U_boot_start),
        CFG_SIMPLE_INT("U_boot_end", &U_boot_end),
        CFG_SIMPLE_INT("U_boot_add_ecc", &U_boot_add_ecc),

        CFG_SIMPLE_STR("Serial", &Serial),
        CFG_SIMPLE_INT("Serial_start", &Serial_start),
        CFG_SIMPLE_INT("Serial_end", &Serial_end),
        CFG_SIMPLE_INT("Serial_add_ecc", &Serial_add_ecc),

        CFG_SIMPLE_STR("Kernel", &Kernel),
        CFG_SIMPLE_INT("Kernel_start", &Kernel_start),
        CFG_SIMPLE_INT("Kernel_end", &Kernel_end),
        CFG_SIMPLE_INT("Kernel_add_ecc", &Kernel_add_ecc),

        CFG_SIMPLE_STR("ROOT_FS", &ROOT_FS),
        CFG_SIMPLE_INT("ROOT_FS_start", &ROOT_FS_start),
        CFG_SIMPLE_INT("ROOT_FS_end", &ROOT_FS_end),
        CFG_SIMPLE_INT("ROOT_FS_oob", &ROOT_FS_oob),
        CFG_SIMPLE_INT("ROOT_FS_add_ecc", &ROOT_FS_add_ecc),

        CFG_SIMPLE_STR("Settings", &Settings),
        CFG_SIMPLE_INT("Settings_start", &Settings_start),
        CFG_SIMPLE_INT("Settings_end", &Settings_end),
        CFG_SIMPLE_INT("Settings_oob", &Settings_oob),
        CFG_SIMPLE_INT("Settings_add_ecc", &Settings_add_ecc),

        CFG_SIMPLE_STR("Program_FS", &Program_FS),
        CFG_SIMPLE_INT("Program_FS_start", &Program_FS_start),
        CFG_SIMPLE_INT("Program_FS_end", &Program_FS_end),
        CFG_SIMPLE_INT("Program_FS_oob", &Program_FS_oob),
        CFG_SIMPLE_INT("Program_FS_add_ecc", &Program_FS_add_ecc),

        CFG_SIMPLE_STR("Data_FS", &Data_FS),
        CFG_SIMPLE_INT("Data_FS_start", &Data_FS_start),
        CFG_SIMPLE_INT("Data_FS_end", &Data_FS_end),
        CFG_SIMPLE_INT("Data_FS_oob", &Data_FS_oob),
        CFG_SIMPLE_INT("Data_FS_add_ecc", &Data_FS_add_ecc),

        CFG_SIMPLE_STR("User_FS", &User_FS),
        CFG_SIMPLE_INT("User_FS_start", &User_FS_start),
        CFG_SIMPLE_INT("User_FS_end", &User_FS_end),
        CFG_SIMPLE_INT("User_FS_oob", &User_FS_oob),
        CFG_SIMPLE_INT("User_FS_add_ecc", &User_FS_add_ecc),

        CFG_SIMPLE_STR("User_Disk", &User_Disk),
        CFG_SIMPLE_INT("User_Disk_start", &User_Disk_start),
        CFG_SIMPLE_INT("User_Disk_end", &User_Disk_end),
        CFG_SIMPLE_INT("User_Disk_oob", &User_Disk_oob),
        CFG_SIMPLE_INT("User_Disk_add_ecc", &User_Disk_add_ecc),

        CFG_END()
    };
    cfg_t *cfg;

    cfg = cfg_init(opts, 0);
    if (cfg_parse(cfg, file_name) == CFG_PARSE_ERROR)
        return (-1);
    cfg_free(cfg);
    return (0);
}

static int valid_conf()
{
    printf("\t正在检查配置文件\n");

    int fd;

    if (NAND_Flash)
    {
        printf("\tNAND_Flash = \t%s\n",NAND_Flash);
    }
    else
    {
        fprintf(stderr,"配置文件中NAND_Flash没有设置\n");
        return (-1);
    }

    if (U_boot)
    {
        printf("\tU_boot = \t%s\n",U_boot);
        printf("\tU_boot_start = \t0x%X\n",U_boot_start);
        printf("\tU_boot_end = \t0x%X\n",U_boot_end);
        printf("\tU_boot_add_ecc = \t%d\n",U_boot_add_ecc);
    }

    if (Serial)
    {
        printf("\tSerial = \t%s\n",Serial);
        printf("\tSerial_start = \t0x%X\n",Serial_start);
        printf("\tSerial_end = \t0x%X\n",Serial_end);
        printf("\tSerial_add_ecc = \t%d\n",Serial_add_ecc);
    }

    if (Kernel)
    {
        printf("\tKernel = \t%s\n",Kernel);
        printf("\tKernel_start = \t0x%X\n",Kernel_start);
        printf("\tKernel_end = \t0x%X\n",Kernel_end);
        printf("\tKernel_add_ecc = \t%d\n",Kernel_add_ecc);
    }

    if (ROOT_FS)
    {
        printf("\tROOT_FS = \t%s\n",ROOT_FS);
        printf("\tROOT_FS_start = \t0x%X\n",ROOT_FS_start);
        printf("\tROOT_FS_end = \t0x%X\n",ROOT_FS_end);
        printf("\tROOT_FS_obb = \t%d\n",ROOT_FS_oob);
        printf("\tROOT_FS_add_ecc = \t%d\n",ROOT_FS_add_ecc);
    }

    if (Settings)
    {
        printf("\tSettings = \t%s\n",Settings);
        printf("\tSettings_start = \t0x%X\n",Settings_start);
        printf("\tSettings_end = \t0x%X\n",Settings_end);
        printf("\tSettings_obb = \t%d\n",Settings_oob);
        printf("\tSettings_add_ecc = \t%d\n",Settings_add_ecc);
    }

    if (Program_FS)
    {
        printf("\tProgram_FS = \t%s\n",Program_FS);
        printf("\tProgram_FS_start = \t0x%X\n",Program_FS_start);
        printf("\tProgram_FS_end = \t0x%X\n",Program_FS_end);
        printf("\tProgram_FS_obb = \t%d\n",Program_FS_oob);
        printf("\tProgram_FS_add_ecc = \t%d\n",Program_FS_add_ecc);
    }

    if (Data_FS)
    {
        printf("\tData_FS = \t%s\n",Data_FS);
        printf("\tData_FS_start = \t0x%X\n",Data_FS_start);
        printf("\tData_FS_end = \t0x%X\n",Data_FS_end);
        printf("\tData_FS_obb = \t%d\n",Data_FS_oob);
        printf("\tData_FS_add_ecc = \t%d\n",Data_FS_add_ecc);
    }

    if (User_FS)
    {
        printf("\tUser_FS = \t%s\n",User_FS);
        printf("\tUser_FS_start = \t0x%X\n",User_FS_start);
        printf("\tUser_FS_end = \t0x%X\n",User_FS_end);
        printf("\tUser_FS_obb = \t%d\n",User_FS_oob);
        printf("\tUser_FS_add_ecc = \t%d\n",User_FS_add_ecc);
    }

    if (User_Disk)
    {
        printf("\tUser_Disk = \t%s\n",User_Disk);
        printf("\tUser_Disk_start = \t0x%X\n",User_Disk_start);
        printf("\tUser_Disk_end = \t0x%X\n",User_Disk_end);
        printf("\tUser_Disk_obb = \t%d\n",User_Disk_oob);
        printf("\tUser_Disk_add_ecc = \t%d\n",User_Disk_add_ecc);
    }

    printf("\n");
    
    return (0);
}

int main(int argc, char **argv)
{
    int nand_flash_fd;
    if (parse_configure("pavo_nandflash.conf") < 0)
    {
        fprintf(stderr,"解析配置文件时出错!\n");
        exit(-1);
    }

    if (valid_conf() < 0)
        exit(-1);


    return (0);
}









pavo_nandflash.conf:




NAND_Flash = np1100.bin

U_boot =                 data/u-boot-nand.bin
U_boot_start =                 0x00000000
U_boot_end =                 0x00200000
U_boot_add_ecc =         0

Serial =                 data/mtd1
Serial_start =                 0x00200000
Serial_end =                 0x00400000
Serial_add_ecc =         0

Kernel =                 data/uImage
Kernel_start =                 0x00400000
Kernel_end =                 0x00600000
Kernel_add_ecc =         0

ROOT_FS =                 data/rootfs.yaffs2
ROOT_FS_start =         0x00600000
ROOT_FS_end =                 0x01800000
ROOT_FS_oob =                 1
ROOT_FS_add_ecc =         0

Settings =                 data/Settings.yaffs2
Settings_start =         0x01800000
Settings_end =                 0x02300000
Settings_oob =                 1
Settings_add_ecc =         0

Program_FS =                 data/ProgramFS.yaffs2
Program_FS_start =         0x02300000
Program_FS_end =         0x0cd00000
Program_FS_oob =         1
Program_FS_add_ecc =         0

Data_FS =                 data/DataFS.yaffs2
Data_FS_start =         0x0cd00000
Data_FS_end =                 0x15900000
Data_FS_oob =                 1
Data_FS_add_ecc =         0

User_FS =                 data/UsrFS.yaffs2
User_FS_start =         0x15900000
User_FS_end =                 0x16500000
User_FS_oob =                 1
User_FS_add_ecc =         0

User_Disk =                 data/UsrDisk.vfat
User_Disk_start =         0x16500000
User_Disk_end =         0x40000000
User_Disk_oob =         1
User_Disk_add_ecc =         0


Makefile:

CC     = gcc

#specify your custmized compile flags here
CFLAGS  = -Wall -g
LDFLAGS = -lconfuse

CFLAGS += -o3



pavo_nandflash:pavo_nandflash.o 
        $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)


clean:
        rm -rf pavo_nandflash pavo_nandflash.o 
















本来,理论上会读取出配置文件内容并且打印出的,可是,,我最后的运行读取结果却和配置文件开头几个东西内容完全不同。。。求解!!

11 个解决方案

#1


屏幕输出的是:


        正在检查配置文件                                                                
        NAND_Flash =    np1100.bin                                                      
        U_boot =        data/u-boot-nand.bin                                            
        U_boot_start =  0x0                                                             
        U_boot_end =    0x0                                                             
        U_boot_add_ecc =        0                                                       
        Serial =        data/mtd1                                                       
        Serial_start =  0x0                                                             
        Serial_end =    0x400000                                                        
        Serial_add_ecc =        0                                                       
        Kernel =        data/uImage                                                     
        Kernel_start =  0x0                                                             
        Kernel_end =    0x0                                                             
        Kernel_add_ecc =        0
        ROOT_FS =       data/rootfs.yaffs2
        ROOT_FS_start =         0x0
        ROOT_FS_end =   0x0
        ROOT_FS_obb =   0
        ROOT_FS_add_ecc =       0
        Settings =      data/Settings.yaffs2
        Settings_start =        0x1800000
        Settings_end =  0x2300000
        Settings_obb =  1
        Settings_add_ecc =      0
        Program_FS =    data/ProgramFS.yaffs2
        Program_FS_start =      0x2300000
        Program_FS_end =        0xCD00000
        Program_FS_obb =        0
        Program_FS_add_ecc =    0
        Data_FS =       data/DataFS.yaffs2
        Data_FS_start =         0xCD00000
        Data_FS_end =   0x15900000
        Data_FS_obb =   0
        Data_FS_add_ecc =       0
        User_FS =       data/UsrFS.yaffs2
        User_FS_start =         0x15900000
        User_FS_end =   0x16500000
        User_FS_obb =   1
        User_FS_add_ecc =       0
        User_Disk =     data/UsrDisk.vfat
        User_Disk_start =       0x16500000
        User_Disk_end =         0x40000000
        User_Disk_obb =         1
        User_Disk_add_ecc =     0



与pavo_nandflash.conf内容不同,啊。。。

#2


楼主用gdb调试过没?

#3


gdb我还不怎么会用啊

#4


引用 2 楼  的回复:
楼主用gdb调试过没?
而且。。这个源码调用cfg_parse的,没法调试。。。用cfg_parse一读取后,就是这个样子了

#5


引用 4 楼  的回复:
引用 2 楼  的回复:

楼主用gdb调试过没?
而且。。这个源码调用cfg_parse的,没法调试。。。用cfg_parse一读取后,就是这个样子了


这样的话,你就在代码中加些输出语句吧,这也是很有效的debug方式

#6


可是我用cfg_parse一读取,它就是错误的了,是不是我用法有问题?可是我看不出哪里错误啊。。

#7


先别解析这么多,先简单的解析一句,看有错误没有。

#8


简单的没问题。。

#9


printf 调试

#10


这么长,谁去看?

#11


有可能读的不是同一个文件,只是名字相同而已。

#1


屏幕输出的是:


        正在检查配置文件                                                                
        NAND_Flash =    np1100.bin                                                      
        U_boot =        data/u-boot-nand.bin                                            
        U_boot_start =  0x0                                                             
        U_boot_end =    0x0                                                             
        U_boot_add_ecc =        0                                                       
        Serial =        data/mtd1                                                       
        Serial_start =  0x0                                                             
        Serial_end =    0x400000                                                        
        Serial_add_ecc =        0                                                       
        Kernel =        data/uImage                                                     
        Kernel_start =  0x0                                                             
        Kernel_end =    0x0                                                             
        Kernel_add_ecc =        0
        ROOT_FS =       data/rootfs.yaffs2
        ROOT_FS_start =         0x0
        ROOT_FS_end =   0x0
        ROOT_FS_obb =   0
        ROOT_FS_add_ecc =       0
        Settings =      data/Settings.yaffs2
        Settings_start =        0x1800000
        Settings_end =  0x2300000
        Settings_obb =  1
        Settings_add_ecc =      0
        Program_FS =    data/ProgramFS.yaffs2
        Program_FS_start =      0x2300000
        Program_FS_end =        0xCD00000
        Program_FS_obb =        0
        Program_FS_add_ecc =    0
        Data_FS =       data/DataFS.yaffs2
        Data_FS_start =         0xCD00000
        Data_FS_end =   0x15900000
        Data_FS_obb =   0
        Data_FS_add_ecc =       0
        User_FS =       data/UsrFS.yaffs2
        User_FS_start =         0x15900000
        User_FS_end =   0x16500000
        User_FS_obb =   1
        User_FS_add_ecc =       0
        User_Disk =     data/UsrDisk.vfat
        User_Disk_start =       0x16500000
        User_Disk_end =         0x40000000
        User_Disk_obb =         1
        User_Disk_add_ecc =     0



与pavo_nandflash.conf内容不同,啊。。。

#2


楼主用gdb调试过没?

#3


gdb我还不怎么会用啊

#4


引用 2 楼  的回复:
楼主用gdb调试过没?
而且。。这个源码调用cfg_parse的,没法调试。。。用cfg_parse一读取后,就是这个样子了

#5


引用 4 楼  的回复:
引用 2 楼  的回复:

楼主用gdb调试过没?
而且。。这个源码调用cfg_parse的,没法调试。。。用cfg_parse一读取后,就是这个样子了


这样的话,你就在代码中加些输出语句吧,这也是很有效的debug方式

#6


可是我用cfg_parse一读取,它就是错误的了,是不是我用法有问题?可是我看不出哪里错误啊。。

#7


先别解析这么多,先简单的解析一句,看有错误没有。

#8


简单的没问题。。

#9


printf 调试

#10


这么长,谁去看?

#11


有可能读的不是同一个文件,只是名字相同而已。