| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1817 人关注过本帖
标题:用什么方法比较快速的截取指定字符串之后成对出现的entry和ret的内容
只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
已结贴  问题点数:30 回复次数:13 
用什么方法比较快速的截取指定字符串之后成对出现的entry和ret的内容
assemble_trace.rar (549 Bytes)
#. function

    call8  0x10000000
    entry   a1,320
{  /"dd1" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    call8  0x20000000
    entry   a1,320
{  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew
{  /"dd3" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew.n
{  /"dd4" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
我的测试数据文件可能非常大(超过1G),因此按照普通的方式将整个文件先读取到内存再查找的方式可能非常的费资源,各位达人有什么解决的好建议吗?
比如指定字符串0x20000000,则需要截取
entry   a1,320
{  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew
并存放到新文件内,如果是0x10000000,则需要截取(确保所截取的内容entry和ret/ret.n成对)
    entry   a1,320
{  /"dd1" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    call8  0x20000000
    entry   a1,320
{  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew
{  /"dd3" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew.n


[ 本帖最后由 vfdff 于 2012-7-29 17:41 编辑 ]
搜索更多相关主题的帖子: 112 nop function 字符串 
2012-07-29 17:38
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:10 
好高深 没看懂
2012-07-29 17:42
姻脂梦
Rank: 6Rank: 6
等 级:侠之大者
帖 子:264
专家分:424
注 册:2012-7-3
收藏
得分:10 
汇编吗?
2012-07-29 18:12
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <string.h>

#define MAX_BUFFER_LEN  4096    /* max line length */
#define INSN_NUM  3             /* the number of entry and retw insns */

/* specify insns which need to be consider */
const char *insns[INSN_NUM] = {
    "entry",
    "retw",
    "retw.n"
};

/* record current status */
static int sequence = 0;                   /* the sequence of access specify function */
static int status = 0, laststatus = 0;     /* entry: +1, retw: -1 */

int main(int argc, char **argv)
{
    char linebuf[MAX_BUFFER_LEN];  /* get line info */
    // status = 0;

    /*if (!argv[1]) {
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
        return -1;
    }*/
   
    //FILE *fp = fopen(argv[1], "r");
    FILE *fp = fopen("assemble_trace.txt", "r");
    if (!fp) {
        fprintf(stderr, "cannot open %s\n", argv[1]);
        return -1;
    }
   
    while(fgets(linebuf, MAX_BUFFER_LEN, fp)) {
   
        /* check there is entry or retw insns */
        unsigned int i = 0, flag = 0;
        for (i=0; i<INSN_NUM; i++) {
            if (strstr(linebuf, insns[i])) {
                flag = 1;        /* exist entry or retw insns */
                break;
            }
        }
        
        /* update the status , no need to update if flag == 0 */
        if (flag == 1)
        {
            switch(i) {
                case 0: status += 1; break; /* entry: +1 */
                case 1:
                case 2: status -= 1; break; /* retw: -1 */
                default: printf("error flag status\n"); return -1;
            }

            /* check current status */
            // printf("current status = %d\n", status);
            if ( status < 0 ) {
                printf("error status value\n");
                return -1;
            }
        }

        if ( (status != 0) || (laststatus != 0) ) {
            printf("%s\n", linebuf);
        }
       
        laststatus = status;  /* make sure print the last retw insn */
    }
   
    return 0;
   
}
实现了部分,还需要对这个程序增加个条件,即缺乏对指定字符串之后的判定,当前已经能够获取成对出现的entry和ret的内容

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-07-29 19:27
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:10 
你的要求很简单,我更感兴趣的是你这些文本是什么东东?

你的描述很有意思。1楼引用中是rew/rew.n,描述中你说的是ret/ret.n,而4楼的代码中是retw/retw.n。到底以哪个为准?

重剑无锋,大巧不工
2012-07-29 19:44
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <string.h>

#define MAX_BUFFER_LEN  4096    /* max line length */
#define INSN_NUM  3             /* the number of entry and retw insns */

/* specify insns which need to be consider */
const char *insns[INSN_NUM] = {
    "entry",
    "retw",
    "retw.n"
};

/* record current status */
static int sequence = 0;                   /* the sequence of access specify function */
static int status = 0, laststatus = 0;     /* entry: +1, retw: -1 */


int main(int argc, char **argv)
{
    char linebuf[MAX_BUFFER_LEN];  /* record line info */
    char functionAddr[16] = {"0x20000000"};   /* record function address string */
    unsigned int enableprint = 0;
    // status = 0;

    /*if (!argv[1]) {
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
        return -1;
    }*/
   
    //FILE *fp = fopen(argv[1], "r");
    FILE *fp = fopen("assemble_trace.txt", "r");
    if (!fp) {
        fprintf(stderr, "cannot open %s\n", argv[1]);
        return -1;
    }
   
    while(fgets(linebuf, MAX_BUFFER_LEN, fp)) {
       
        unsigned int i = 0;
        unsigned int statusflag = 0;

        /* only check the call address when the statusflag =0 to avoid recursive call*/
        if (enableprint == 0) {
            if (strstr(linebuf, "call") && strstr(linebuf, functionAddr))
                enableprint = 1;        /* enable to print the content */
        } else {
         
            /* check there is entry or retw insns */
            for (i=0; i<INSN_NUM; i++) {
                if (strstr(linebuf, insns[i])) {
                    statusflag = 1;        /* exist entry or retw insns */
                    break;
                }
            }

            /* update the status , no need to update if flag == 0 */
            if (statusflag == 1)
            {
                switch(i) {
                case 0: status += 1; break; /* entry: +1 */
                case 1:                     /* retw: -1 */
                case 2: status -= 1; break; /* retw.n: -1 */
                default: printf("error flag status\n"); return -1;
                }
               
                /* check current status */
                // printf("current status = %d\n", status);
                if ( status < 0 ) {
                    printf("error status value\n");
                    return -1;
                }
            }
           
        }
        
        if ( (status != 0) || (laststatus != 0) ) {
            // if (enableprint == 1)
                printf("%s\n", linebuf);
        }
       
        if ( (status == 0) && (laststatus == 1) )
           enableprint = 0;

        laststatus = status;  /* make sure print the last retw insn */

    }
   
    return 0;
   
}
是的,要求很简单,以retw/retw.n为准

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-07-31 23:15
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <string.h>

#define MAX_BUFFER_LEN  4096    /* max line length */
#define INSN_NUM  3             /* the number of entry and retw insns */

/* specify insns which need to be consider */
const char *insns[INSN_NUM] = {
    "entry",
    "retw",
    "retw.n"
};

/* record current status */
static int sequence = 0;                   /* the sequence of access specify function */
static int status = 0, laststatus = 0;     /* entry: +1, retw: -1 */


int main(int argc, char **argv)
{
    char linebuf[MAX_BUFFER_LEN];             /* record line info */
    char functionAddr[16] = {"0x20000000"};   /* record function address string */
    unsigned int enableprint = 0;

    if (argc != 2 ) {
        fprintf(stderr, "usage  : <tracefile> <functionAddrStr>\n");
        fprintf(stderr, "example: xtensa_assemble_trace 0x20000000\n");
        return -1;

    }

    FILE *fp = fopen(argv[1], "r");
    // FILE *fp = fopen("assemble_trace.txt", "r");
    if (!fp) {
        fprintf(stderr, "cannot open %s\n", argv[1]);
        return -1;
    }
   

    memset(functionAddr, 0, sizeof(functionAddr));
    memcpy(functionAddr, argv[2], sizeof(functionAddr));

    while(fgets(linebuf, MAX_BUFFER_LEN, fp)) {
       

        unsigned int i = 0;
        unsigned int statusflag = 0;

        /* only check the call address when the statusflag =0 to avoid recursive call*/
        if (enableprint == 0) {
            if (strstr(linebuf, "call") && strstr(linebuf, functionAddr))
                enableprint = 1;        /* enable to print the content */
        } else {
         

            /* check there is entry or retw insns */
            for (i=0; i<INSN_NUM; i++) {
                if (strstr(linebuf, insns[i])) {
                    statusflag = 1;        /* exist entry or retw insns */
                    break;
                }
            }

            /* update the status , no need to update if flag == 0 */
            if (statusflag == 1)
            {
                switch(i) {
                case 0: status += 1; break; /* entry: +1 */
                case 1:                     /* retw: -1 */
                case 2: status -= 1; break; /* retw.n: -1 */
                default: printf("error flag status\n"); return -1;
                }
               

                /* check current status */
                // printf("current status = %d\n", status);
                if ( status < 0 ) {
                    printf("error status value\n");

                    return -1;
                }
            }
           

        }
        

        if ( (status != 0) || (laststatus != 0) ) {
            printf("%s\n", linebuf);
        }
       

        if ( (status == 0) && (laststatus == 1) )
           enableprint = 0;

        laststatus = status;  /* make sure print the last retw insn */

    }
   

    return 0;
   

}

/*  trace file format:


    call8  0x10000000
    entry   a1,320
    {  /"dd1" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
    }
    call8  0x20000000
    entry   a1,320
    {  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
    }
    retw
    {  /"dd3" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
    }
    retw.n
    {  /"dd4" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
    }
*/


~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-08-04 22:22
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
用 awk 或者 Perl 写脚本你是担心处理的效率不行吗?
2012-08-04 23:59
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
嗯 是的,不过脚本我也不太擅长,如果您有好的实现,我可以去试试效率

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-08-05 10:57
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
回复 9楼 vfdff
awk 我也好久没用了,不知道写的这个行不行:
程序代码:
# filename: entry.awk

BEGIN { stat = 0; level = 0 }

$0~pat { stat = 1 }
{ if ( stat == 1 ) print $0 }

/entry/ { if ( stat == 1 ) ++level }
/rew/ { if ( stat == 1 && --level == 0 ) exit }

至少你给的那个例子看上去还能通过:
$ gawk -v pat="0x20000000" -f entry.awk test.txt
    call8  0x20000000
    entry   a1,320
{  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew

$ gawk -v pat="0x10000000" -f entry.awk test.txt
    call8  0x10000000
    entry   a1,320
{  /"dd1" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    call8  0x20000000
    entry   a1,320
{  /"dd2" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew
{  /"dd3" /ds  /#dsd
    s32i    a6,a1,112
    nop      \//dd
    movi    a6,0
}
    rew.n



[ 本帖最后由 pangding 于 2012-8-5 13:46 编辑 ]
2012-08-05 13:37
快速回复:用什么方法比较快速的截取指定字符串之后成对出现的entry和ret的内容
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035951 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved