⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command_parse.c

📁 linux内核调试小工具.可以读写指定地址上的数据
💻 C
📖 第 1 页 / 共 2 页
字号:
    if(input_count == 0)
    {
        output_len += sprintf(&output[output_len], "Missed func name, %s\n", ksh_cmd[cmd_index].help_string);
        return output_len;
    }
    
    if(input_count > 5)
    {
        output_len += sprintf(&output[output_len], "Too many args, %s\n", ksh_cmd[cmd_index].help_string);
        return output_len;
    } 
    
    /*解析符号表,找到函数的地址*/
    func_addr = search_symbol(&func_para[0][0]);
    if(func_addr == NULL)
    {
        output_len += sprintf(&output[output_len],"Undefined symbol '%s'\n", &func_para[0][0]);
        return output_len;
    }else
    {
        output_len += sprintf(&output[output_len],"%s @ 0x%08x\n", &func_para[0][0], (UINT32)func_addr);
    }

    switch(input_count){
        case 1:
            res = (UINT32)((RUNC_FUNC0)(func_addr))();
            break;
        case 2:
            res = (UINT32)((RUNC_FUNC1)(func_addr))(simple_strtoul(&func_para[1][0], NULL, 0));
            break;
        case 3:
            res = (UINT32)((RUNC_FUNC2)(func_addr))(simple_strtoul(&func_para[1][0], NULL, 0), \
                                     simple_strtoul(&func_para[2][0], NULL, 0));
            break;
        case 4:
            res = (UINT32)((RUNC_FUNC3)(func_addr))(simple_strtoul(&func_para[1][0], NULL, 0), \
                                     simple_strtoul(&func_para[2][0], NULL, 0), \
                                     simple_strtoul(&func_para[3][0], NULL, 0));            
            break;
        case 5:
            res = (UINT32)((RUNC_FUNC4)(func_addr))(simple_strtoul(&func_para[1][0], NULL, 0), \
                                     simple_strtoul(&func_para[2][0], NULL, 0), \
                                     simple_strtoul(&func_para[3][0], NULL, 0), \
                                     simple_strtoul(&func_para[4][0], NULL, 0));             
            break;
        default:
            output_len += sprintf(&output[output_len],"Too many args\n");
            break;
    }
    output_len += sprintf(&output[output_len],"func res = %d = 0x%08x\n", res, res);
    return output_len;
    
}

int set_var(int cmd_index, int input_count, char *output)
{
    int output_len = 0;
    void * var_addr = NULL;
    UINT32 width = 0;
    UINT32 value = 0;
    
    if(sym_file == NULL)
    {
        output_len += sprintf(&output[output_len],"Should not run to here %s\n", __func__);
        return output_len;
    }
    
    if(input_count < 2)
    {
        output_len += sprintf(&output[output_len], "Missed var name or value, %s\n", ksh_cmd[cmd_index].help_string);
        return output_len;
    }
    
    if(input_count > 3)
    {
        output_len += sprintf(&output[output_len], "Too many args, %s\n", ksh_cmd[cmd_index].help_string);
        return output_len;
    }

    /*解析符号表,找到变量的地址*/
    var_addr = search_symbol(&func_para[0][0]);
    if(var_addr == NULL)
    {
        output_len += sprintf(&output[output_len],"Undefined symbol '%s'\n", &func_para[0][0]);
        return output_len;
    }else
    {
        output_len += sprintf(&output[output_len],"%s @ 0x%08x\n", &func_para[0][0], (UINT32)var_addr);
    }

    if(input_count == 2){
        /*s var_name value*/
        width = sizeof(UINT32);
        value = simple_strtoul(&func_para[1][0], NULL, 0);
    }else if(input_count == 3){
        /*s var_name width value*/
        width = simple_strtoul(&func_para[1][0], NULL, 0);
        value = simple_strtoul(&func_para[2][0], NULL, 0);
    }

    switch(width){
        case 1:
            *((volatile UINT8*)var_addr) = (UINT8)value;
            output_len += sprintf(&output[output_len],"set %s ok, width %d\n", &func_para[0][0], width);            
            break;
        case 2:
            *((volatile UINT16*)var_addr) = (UINT16)value;
            output_len += sprintf(&output[output_len],"set %s ok, width %d\n", &func_para[0][0], width);            
            break;
        case 4:
            *((volatile UINT32*)var_addr) = (UINT32)value;
            output_len += sprintf(&output[output_len],"set %s ok, width %d\n", &func_para[0][0], width);            
            break;            
        default:
            output_len += sprintf(&output[output_len],"unspport width %d\n", width);
            break;
    }

    return output_len;
    
}


int fill_mem(int cmd_index, int input_count, char *output)
{
    int output_len = 0;
    UINT32 addr = 0;
    UINT32 length = 0;
    UINT8 value = 0;
    UINT32 i = 0;

    if(input_count != 3){
        output_len += sprintf(&output[output_len], "format of cmd error, %s\n", ksh_cmd[cmd_index].help_string);
        return output_len;
    }

    
    addr = simple_strtoul(&func_para[0][0], NULL, 0);
    length = simple_strtoul(&func_para[1][0], NULL, 0);
    value = (UINT8)simple_strtoul(&func_para[2][0], NULL, 0);

    for(i = 0; i<length; i++){
        *((volatile UINT8 *)(addr+i)) = value;
    }

    output_len += sprintf(&output[output_len], "fill addr@%08x, length[%d], value[0x%2.2x]\n",
                                              addr, length, value);

    return output_len;
}
    
#if 1//def DEBUG

void run_func0(void)
{
    printk("run into func %s\n", __func__);
}

void run_func1(UINT32 arg1)
{
    printk("run into func %s, arg1 = %d\n", __func__, arg1);
}

void run_func2(UINT32 arg1, UINT32 arg2)
{
    printk("run into func %s, arg1 = %d, arg2 = %d\n", __func__, arg1, arg2);
}

void run_func3(UINT32 arg1, UINT32 arg2, UINT32 arg3)
{
    printk("run into func %s, arg1 = %d, arg2 = %d, arg3 = %d\n", __func__, arg1, arg2, arg3);
}

void run_func4(UINT32 arg1, UINT32 arg2, UINT32 arg3, UINT32 arg4)
{
    printk("run into func %s, arg1 = %d, arg2 = %d, arg3 = %d, arg4 = %d\n", \
        __func__, arg1, arg2, arg3, arg4);
}

#endif

/*
搜索符号表
返回值:
NULL :没有找到符号
addr:符号所在的地址, void* 
*/
void * search_symbol(char *input_symbol)
{
    char buff[256] = {0};
    char seps[] = " ,\t\n\r";
    char *token = NULL;
    char *temp = NULL;
    mm_segment_t  oldfs;
    
    oldfs = get_fs();
    set_fs(KERNEL_DS);
    
    if (sym_file->f_op->read == NULL ||
        sym_file->f_op->llseek== NULL){
        /*fs doesn't allow read*/
        printk("fs doesn't allow read or seek\n");
        return NULL;
    }
    
    /*回到文件的起始位置*/
    //sym_file->f_pos = 0;
    sym_file->f_op->llseek(sym_file, 0, 0);
    //rewind(symbol_fd); usr mode
    temp = read_line(buff, sizeof(buff)-1, sym_file);
    while( temp )
    {
        char addr[MAX_ADDR_LEN+2+1] = {0};
        char symbol_name[MAX_SYMS_NAME_LEN+1] = {0};
              
        token = (char*)strtok(buff, seps);
        
        if(token == NULL){
            set_fs(oldfs);
            return NULL;
        }
        
        strncpy(addr+2, token, MAX_ADDR_LEN);
        addr[0] = '0';
        addr[1] = 'x';
        token = (char *)strtok(NULL, seps);
        strncpy(symbol_name, token, MAX_SYMS_NAME_LEN);
        
        if(strncmp(symbol_name, input_symbol, MAX_SYMS_NAME_LEN) == 0){
            set_fs(oldfs);
            return (void*)(simple_strtoul(addr, NULL, 0));
        } 
        
        temp = read_line(buff, sizeof(buff)-1, sym_file);
        
    }
    
    set_fs(oldfs);
    return NULL;
}

char * read_line(char *buf, size_t count, struct file *fp)
{
    size_t i = 0;
    char in_char;
    do
    {
    	in_char = read_char(fp);
		buf[i] = (char)in_char;
    	i++;
    }while( (i < count-1) && (in_char != (char) (-1)) && (in_char != (char) (0)) &&(in_char != '\n') && (in_char != '\r') );

    /*EOF =  0 or -1 ? */
    if(in_char == 0){
        printk("return NULL\n");
        return NULL;
    }
    
	buf[i] = 0; 
    //printk("%s\n", buf);
    return(buf);
}

char read_char(struct file *fp)
{
    char out;
    int bytes_read = 0;
    bytes_read = fp->f_op->read(fp, &out, 1, &fp->f_pos);

    return out;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -