程序运行出现| program abort — Invalid Memory Access问题

C语言 码拜 8年前 (2016-04-08) 1617次浏览
/* accquire the packet and adjudge whether the packet is mine */
pkptr = op_pk_get(op_intrpt_strm());
/* get the type field of the instream packet */
op_pk_nfd_access(pkptr,"type",&type);
if (type == 34824)
	{
		/* get the packet"s destination address,the NULL,and opcode field 
		of the instream packet */
		/* adjudge whether the packet is own node throught analysis the value
		of these field*/
		op_pk_nfd_access(pkptr,"dest_addr",&ip_addr);
		op_pk_nfd_access(pkptr,"LLID",&onuid);
		op_pk_nfd_access(pkptr,"opcode",&opcode);
		if ((ip_addr == src_addr) && (onuid == llid) && (opcode == 2))
			 {
				/* get the information of gate packet associated with the service */
			 	op_pk_nfd_get(pkptr,"channel_list1",&gate_var->channel_list1);
				op_pk_nfd_get(pkptr,"channel_list2",&gate_var->channel_list2);
				op_pk_nfd_get(pkptr,"channel_list3",&gate_var->channel_list3);
				op_pk_nfd_get(pkptr,"grant_start_time",&gate_var->start_time);
				op_pk_nfd_get(pkptr,"num_grants",&gate_var->num_grants);
				op_pk_nfd_get(pkptr,"tx_wave",&gate_var->tx_wave);
				/* statstic the number of gate packet received by this node */
				op_stat_write(gate_rcvd_stathandle, 1.0);
				gate_num++;
				/* acquire the pointer to the ICI that has been previously 
				associated with the packet	*/
				iciptr = op_pk_ici_get(pkptr);
				op_ici_attr_get (iciptr, "poll_time", &poll_time);
				op_ici_attr_get (iciptr, "extra_flag", &ex_flag);
				op_ici_attr_get (iciptr, "extra_length", &ex_length);
				flag_sla1 = 0;
				flag_sla2 = 0;
				flag_sla3 = 0;

				/* Reverse the order for list and set the flag of service not empty */
				if (op_prg_list_size (gate_var->channel_list2) > 0)
					{
						flag_sla2 = 1;
					}
				if (op_prg_list_size (gate_var->channel_list3) > 0)
					{
						flag_sla3 = 1;
					}
				/* set the flag of service not empty based on the number of flag */
				if (gate_var->num_grants == 1 || gate_var->num_grants == 3)
					{
						/* set the flag of the hight priority service true */
						flag_sla1 = 1;
					}
				/* destroy the packet if the information is saved */
				op_pk_destroy(pkptr);

				/* acquire the bits of packets now in AF queue */
				/* save the increase of af queue when waiting the gate packet arrival */
				af_size_now = op_subq_stat (1, OPC_QSTAT_BITSIZE);
				if (gate_num  <=  16)
					{
						temp_af[gate_num - 1] = af_size_now - af_size;
					}
				else
					{
						for (index = 0; index < 15; index++)
							{
								temp_af[index] = temp_af[index + 1];
							}
						temp_af[15] = af_size_now - af_size;
					}
				/* caculate the average increase of af queue when waiting the gate
				packet arrival */
				if (gate_num <= 16)
					{
						sum_poll = gate_num;
					}
				else
					{
						sum_poll = 16;
					}
				for (index = 0,temp_mean_af = 0; index < sum_poll; index++)
					{
						temp_mean_af += temp_af[index];
					}
				temp_mean_af  = temp_mean_af /sum_poll;
				ef_length = gate_var->channel_list1->sum_channel * 560;

				/* set a self intrpt to process the sequence in time  */
				op_intrpt_schedule_self(op_sim_time (),TIME_INTRPT);
			 }
		else
			 {
				/* destroy the packet if the gate packet is not mine */
			 	op_pk_destroy (pkptr);
			 }			  
	}

出现问题后跟踪了一下,结果为:
| * condition: “GATE_ARRIVAL”
|
| _________state (gate_process): enter executives_________
|
| +- op_intrpt_strm ()
| |       active strm      (42)
| +–
|
| +- op_pk_get (instrm_index)
| |       strm. index      (42)
| |       packet ID        (65466)
| +–
|
| +- op_pk_nfd_access (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (type)
| |       value            (34,824)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_access (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (dest_addr)
| |       value            (77,632,601,899,226)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_access (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (LLID)
| |       value            (63)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_access (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (opcode)
| |       value            (2)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (channel_list1)
| |       completion code  (failure)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (channel_list2)
| |       value            (58,959,736)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (channel_list3)
| |       value            (58,959,704)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (grant_start_time)
| |       value            (0.0)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (num_grants)
| |       value            (1)
| |       completion code  (success)
| +–
|
| +- op_pk_nfd_get (pkptr, fd_name, value_ptr)
| |       packet ID        (65466)
| |       field name       (tx_wave)
| |       value            (4)
| |       completion code  (success)
| +–
|
| +- op_stat_write (stath, value)
| |       stat name        (Gate recieved (pcks))
| |       stat value       (1.0)
| +–
|
| +- op_pk_ici_get (pkptr)
| |       packet ID        (65466)
| |       ICI id           (65324)
| +–
|
| +- op_ici_attr_get (iciptr, attr_name, attr_value_ptr)
| |       ICI id           (65324)
| |       attr name        (poll_time)
| |       completion code  (success)
| |       value            (0.0)
| +–
|
| +- op_ici_attr_get (iciptr, attr_name, attr_value_ptr)
| |       ICI id           (65324)
| |       attr name        (extra_flag)
| |       completion code  (success)
| |       value            (68,377,048)
| +–
|
| +- op_ici_attr_get (iciptr, attr_name, attr_value_ptr)
| |       ICI id           (65324)
| |       attr name        (extra_length)
| |       completion code  (success)
| |       value            (0.0)
| +–
|
| +- op_prg_list_size (list_ptr)
| |       list address     (0x0383A778)
| |       list size        (0)
| +–
|
| +- op_prg_list_size (list_ptr)
| |       list address     (0x0383A758)
| |       list size        (0)
| +–
|
| +- op_pk_destroy (pkptr)
| |       packet ID        (65466)
| |
| | * Kernel Action: Destroying Packet
| |       packet ID        (65466)
| +–
|
| +- op_subq_stat (subq_select, stat_flag)
| |       subq index       (1)
| |       stat index (free pksize)
| |       stat value       (3,951,504)
| +–
<<< Program Fault >>>
* Time:      17:30:07 Sun Jun 19 2016
* Product:   modeler (32-bit)
* Package:   process (onu_mac_4) at module (top.Enterprise Network.4_node_15.onu_mac)
* Function:  onu_mac_4 [gate_process enter execs]
* Error:     program abort — Invalid Memory Access
T (0.000532342), EV (137794), MOD (top.Enterprise Network.4_node_15.onu_mac), PROC (onu_mac_4 [gate_process enter execs])
|–|
| <<< Program Fault >>>                                                       |
| program abort — Invalid Memory Access                                      |
| T (0.000532342), EV (137794), MOD (top.Enterprise Network.4_node_15.onu_mac), PROC (onu_mac_4 [gate_process enter execs]) |
|–|
<<< Program Abort >>>
* Time:      17:30:08 Sun Jun 19 2016
* Product:   modeler (64-bit)
* Package:   op_runsim
* Function:  sim_runsim_exec
* Error:
看执行顺序应该是该执行语句

op_intrpt_schedule_self(op_sim_time (),TIME_INTRPT);

时,却跳出了执行了根本不应该执行的

	 {
				/* destroy the packet if the gate packet is not mine */
			 	op_pk_destroy (pkptr);
			 }

中的内容,问一下这是怎么回事?

解决方案

20

有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论怎么样也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:

//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#ifdef _MSC_VER
    #pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
    #include <windows.h>
    #include <process.h>
    #include <io.h>
    #define  MYVOID             void
    #define  vsnprintf          _vsnprintf
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  MYVOID             void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
    Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
    usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;
    if (NULL==pszFmt||0==pszFmt[0]) return;
    vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;
    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
#define ASIZE    200
#define BSIZE    240
#define CSIZE      2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX ;
CRITICAL_SECTION cs_BBB ;
struct FIFO_BUFFER {
    int  head;
    int  tail;
    int  size;
    char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
    int i,j,k;
    char binstr[80];
    Lock(&cs_HEX);
    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%03d %04x -",cn,i);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,("!"<buf[j]&&buf[j]<="~")?buf[j]:".");
            }
            Log("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,("!"<buf[j]&&buf[j]<="~")?buf[j]:".");
        }
        Log("%s\n",binstr);
    }
    Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;
    lent=0;
    Lock(cs);
    if (fbuf->size>=len) {
        lent=len;
        if (fbuf->head+lent>BSIZE) {
            len1=BSIZE-fbuf->head;
            memcpy(buf     ,fbuf->data+fbuf->head,len1);
            len2=lent-len1;
            memcpy(buf+len1,fbuf->data           ,len2);
            fbuf->head=len2;
        } else {
            memcpy(buf     ,fbuf->data+fbuf->head,lent);
            fbuf->head+=lent;
        }
        fbuf->size-=lent;
    }
    Unlock(cs);
    return lent;
}
MYVOID thdB(void *pcn) {
    char        *recv_buf;
    int          recv_nbytes;
    int          cn;
    int          wc;
    int          pb;
    cn=(int)pcn;
    Log("%03d thdB              thread begin...\n",cn);
    while (1) {
        sleep_ms(10);
        recv_buf=(char *)Cbuf;
        recv_nbytes=CSIZE;
        wc=0;
        while (1) {
            pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
            if (pb) {
                Log("%03d recv %d bytes\n",cn,pb);
                HexDump(cn,recv_buf,pb);
                sleep_ms(1);
            } else {
                sleep_ms(1000);
            }
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
#ifndef _MSC_VER
    pthread_exit(NULL);
#endif
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;
    Lock(cs);
    lent=len;
    if (fbuf->size+lent>BSIZE) {
        lent=BSIZE-fbuf->size;
    }
    if (fbuf->tail+lent>BSIZE) {
        len1=BSIZE-fbuf->tail;
        memcpy(fbuf->data+fbuf->tail,buf     ,len1);
        len2=lent-len1;
        memcpy(fbuf->data           ,buf+len1,len2);
        fbuf->tail=len2;
    } else {
        memcpy(fbuf->data+fbuf->tail,buf     ,lent);
        fbuf->tail+=lent;
    }
    fbuf->size+=lent;
    Unlock(cs);
    return lent;
}
MYVOID thdA(void *pcn) {
    char        *send_buf;
    int          send_nbytes;
    int          cn;
    int          wc;
    int           a;
    int          pa;
    cn=(int)pcn;
    Log("%03d thdA              thread begin...\n",cn);
    a=0;
    while (1) {
        sleep_ms(100);
        memset(Abuf,a,ASIZE);
        a=(a+1)%256;
        if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
        send_buf=(char *)Abuf;
        send_nbytes=ASIZE;
        Log("%03d sending %d bytes\n",cn,send_nbytes);
        HexDump(cn,send_buf,send_nbytes);
        wc=0;
        while (1) {
            pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
            Log("%03d sent %d bytes\n",cn,pa);
            HexDump(cn,send_buf,pa);
            send_buf+=pa;
            send_nbytes-=pa;
            if (send_nbytes<=0) break;//
            sleep_ms(1000);
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
#ifndef _MSC_VER
    pthread_exit(NULL);
#endif
}
int main() {
#ifdef _MSC_VER
    InitializeCriticalSection(&cs_log);
    InitializeCriticalSection(&cs_HEX );
    InitializeCriticalSection(&cs_BBB );
#else
    pthread_t threads[2];
    int threadsN;
    int rc;
    pthread_mutex_init(&cs_log,NULL);
    pthread_mutex_init(&cs_HEX,NULL);
    pthread_mutex_init(&cs_BBB,NULL);
#endif
    Log("Start===========================================================\n");
    BBB.head=0;
    BBB.tail=0;
    BBB.size=0;
#ifdef _MSC_VER
    _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
    _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);
#else
    threadsN=0;
    rc=pthread_create(&(threads[threadsN++]),NULL,thdA,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
    rc=pthread_create(&(threads[threadsN++]),NULL,thdB,(void *)2);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
    if (!access("No_Loop",0)) {
        remove("No_Loop");
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    while (1) {
        sleep_ms(1000);
        if (No_Loop) break;//
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    sleep_ms(3000);
    Log("End=============================================================\n");
#ifdef _MSC_VER
    DeleteCriticalSection(&cs_BBB );
    DeleteCriticalSection(&cs_HEX );
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_BBB);
    pthread_mutex_destroy(&cs_HEX);
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明程序运行出现| program abort — Invalid Memory Access问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)