标题:用什么方法比较快速的截取指定字符串之后成对出现的entry和ret的内容
取消只看楼主
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
已结贴  问题点数:30 回复次数:7 
用什么方法比较快速的截取指定字符串之后成对出现的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
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
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
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
嗯 是的,不过脚本我也不太擅长,如果您有好的实现,我可以去试试效率

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-08-05 10:57
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
非常强悍简洁的程序,版主果然是高手!

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-08-05 21:39
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
if ( (status != 0) || (laststatus != 0) ) {
            printf("%s\n", linebuf); --> printf("%s", linebuf);
        }


 if (argc != 2 ) {  -->  if (argc != 3 ) {


~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013-01-14 16:12
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
有时并非直接使用call8,而是使用callx8
比如:l32r a8,0x213090f8
       callx8 a8 /* 0x213090f8 中的地址才是真正的地址,此时查找call会失效*/

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013-01-14 18:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-374326-1-1.html




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

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