程序动态调试利器,在运行中将监视数据弹出

笨笨仔 2014-09-30 10:16:28
当编写单片机程序,或编写通信程序时,因为需要监视实时数据,而无法设置静态断点,或者静态断点根本无法反映程序的真实运行状态。为此我们在产品开发中为了动态调试程序,编写了一个程序的动态监视软件。
工作原理:都知道“木马”吧,其实这个工具的工作原理很简单,就是木马的原理。它由一个监视窗口接收程序弹出的数据,另外提供一个使用UDP协议的通信类,在需要弹出的地方调用这个类的弹出函数,该类自动将监视数据弹出到监视窗口。
监视窗口程序:ShowServer.exe
C++程序使用的UDP通信类:PopStrToUDP.h,PopStrToUDP.cpp
调用函数:

// 将字串弹出到UDP窗口
void SendStrTo(CString str);

为了弹出非字符串数据,可以在程序中加入一个转换函数:

// 弹出到UDP
void CCommThread::PopToUdp(byte* pdata,int len,BOOL isSend,int port)
{
CString const Exstr[]={L"0",L"1",L"2",L"3",L"4",L"5",L"6",L"7",L"8",L"9"
,L"A",L"B",L"C",L"D",L"E",L"F"};
CString bz=isSend? L"Sending-->\r" : L"Recevied<--\r";

CString str=bz;
byte l,h;
for(int i=0;i<len;i++)
{
l=pdata[i]&0x0f;
h=(pdata[i]&0xf0)>>4;
str+=Exstr[h]+Exstr[l];
if(((i+1)%8)==0 && i!=0)
str+=L" ";
if(((i+1)%16)==0 && i!=0)
str+=L"\r";
else
str+=L" ";
}
CPopStrToUDP ps(port);
ps.SendStrTo(str);
}

PC中应用实例
在“*”分隔的区域中为调用弹出
1、直接弹出字符串

// 压入发送数据
void CCommThread::PushSendDatas(byte* pd,int len)
{
// 溢出检查
int const maxLen=OUT_BUFF_SIZE;
if((maxLen-m_sendBufLen)<len)
{
//**************************
CPopStrToUDP ps;
ps.SendStrTo(L"发送缓冲区溢出");
//**************************
m_sendBufLen=0;
}

2、弹出Byte数据(直接调用了前面的转换函数)

// 保存发送数据
memcpy(&m_pSendBuff[m_sendBufLen],pd,len);
m_sendBufLen+=len;
//****************************
PopToUdp(m_pSendBuff,m_sendBufLen,true);
//****************************

// 调用发送
SendData();
}


说明:
为了监视多个数据,可能需要打开多个监视窗口,为了不产生冲突,可任意选择接收、弹出端口。默认的弹出端口为3030,但可以根据接收窗口的端口设置,在创建通信类实例的时候随时更改。

CPopStrToUDP ps(port);

其中 port 为所需要的端口号。

监视窗口软件说明
1、启动软件后可选择立刻更换端口,也可以不更换

2、监视窗口使用示例

3、监视的数据可保存为TXT文件。

软件下载地址:http://download.csdn.net/detail/wxhxj0268/4398727
...全文
398 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tim_tan2015 2017-04-12
  • 打赏
  • 举报
回复
老兄Q号多少
赵4老师 2014-10-29
  • 打赏
  • 举报
回复
费那事! 有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#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 WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#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}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
jianghandaxue 2014-10-28
  • 打赏
  • 举报
回复
谢谢大哥分享!。。。。
beiyiwangdeshen 2014-10-16
  • 打赏
  • 举报
回复
不错不错,已经下了,留着备用。 (帮我看看那个HID设备的问题吧

2,586

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧