登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

lgjmfyx的博客

享受生活每一天

 
 
 

日志

 
 

用WinDbg在双机环境下调试驱动程序   

2008-12-01 20:55:03|  分类: 技术技巧 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
 
最近一直在学习Windows下驱动开发,尝试过SoftICE调试,可安装上后系统启动(Start NetICE)后,整个系统就死机了,以前在Win2K下安装挺好的,郁闷。所以改成WinDbg调试,发现很好用,把配置和调试过程记录如下,以为日后提供参考。
attrib -h -s -r boot.ini
notepad boot.ini
attrib +h +s +r boot.ini
然后添加一行:
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional Debug" /fastdetect /debug /debugport=com1 /baudrate=115200
修改其内容为:
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional Debug" /fastdetect /debug /debugport=com1 /baudrate=115200
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.1 在主计算机上打开WinDBG,File->Kernel Debug启动主机。用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.2 打开被调试计算机,并选择Debug状态菜单,进入被调试状态。按下CTRL+Break进入调试状态用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
0: kd> bp wdm1!Wdm1Read
0: kd> bp wdm1!Wdm1Write
2.6 待被调试计算机启动后,运行WDM1Test.exe,可以看到windbg捕获到断点。
在Wdm1Read函数处中断:用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
在Watch窗口中,输入fdo,可以显示变量用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.5 在Wdm1Write处中断用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.6 Wdm1Write函数
NTSTATUS Wdm1Write( IN PDEVICE_OBJECT fdo,
                    IN PIRP Irp)
{
    PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
    NTSTATUS status = STATUS_SUCCESS;
    LONG BytesTxd = 0;
    // Get call parameters
    LONGLONG FilePointer = IrpStack->Parameters.Write.ByteOffset.QuadPart;
    ULONG WriteLen = IrpStack->Parameters.Write.Length;
    DebugPrint("Write %d bytes from file pointer %d",(int)WriteLen,(int)FilePointer);
    if( FilePointer<0)
        status = STATUS_INVALID_PARAMETER;
    else
    {
        // Get access to the shared buffer
        KIRQL irql;
        KeAcquireSpinLock(&BufferLock,&irql);
        BytesTxd = WriteLen;
        // (Re)allocate buffer if necessary
        if( ((ULONG)FilePointer)+WriteLen>BufferSize)
        {
            ULONG NewBufferSize = ((ULONG)FilePointer)+WriteLen;
            PVOID NewBuffer = ExAllocatePool(NonPagedPool,NewBufferSize);
            if( NewBuffer==NULL)
            {
                BytesTxd = BufferSize - (ULONG)FilePointer;
                if( BytesTxd<0) BytesTxd = 0;
            }
            else
            {
                RtlZeroMemory(NewBuffer,NewBufferSize);
                if( Buffer!=NULL)
                {
                    RtlCopyMemory(NewBuffer,Buffer,BufferSize);
                    ExFreePool(Buffer);
                }
                Buffer = (PUCHAR)NewBuffer;
                BufferSize = NewBufferSize;
            }
        }
        // Write to shared memory
        if( BytesTxd>0 && Buffer!=NULL)
            RtlCopyMemory( Buffer+FilePointer, Irp->AssociatedIrp.SystemBuffer, BytesTxd);
        // Release shared buffer
        KeReleaseSpinLock(&BufferLock,irql);
    }
    DebugPrint("Write: %d bytes written",(int)BytesTxd);
    // Complete IRP
    return CompleteIrp(Irp,status,BytesTxd);
}
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
单步调试可以观察Irp->AssociatedIrp.SystemBuffer的变化。
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客3 用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客结束语
    个人感觉来说Windbg还是一个很不错的工具,调试时不用担心主计算机系统蓝屏,重启等,而且调试方便。
评论:没有评论。
发表评论请输入评论


最近评论文章分类收藏相册My Photoprogramm photo网站链结自己以前的主页自己以前写的小程序串口类LsComm 几个数字信号处理算法程序学生成绩管理系统ADO版简易软盘镜像工具的实现及操作系统编写初步存档软件项目交易订阅我的博客
 
最近一直在学习Windows下驱动开发,尝试过SoftICE调试,可安装上后系统启动(Start NetICE)后,整个系统就死机了,以前在Win2K下安装挺好的,郁闷。所以改成WinDbg调试,发现很好用,把配置和调试过程记录如下,以为日后提供参考。
attrib -h -s -r boot.ini
notepad boot.ini
attrib +h +s +r boot.ini
然后添加一行:
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional Debug" /fastdetect /debug /debugport=com1 /baudrate=115200
修改其内容为:
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional Debug" /fastdetect /debug /debugport=com1 /baudrate=115200
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.1 在主计算机上打开WinDBG,File->Kernel Debug启动主机。用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.2 打开被调试计算机,并选择Debug状态菜单,进入被调试状态。按下CTRL+Break进入调试状态用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
0: kd> bp wdm1!Wdm1Read
0: kd> bp wdm1!Wdm1Write
2.6 待被调试计算机启动后,运行WDM1Test.exe,可以看到windbg捕获到断点。
在Wdm1Read函数处中断:用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
在Watch窗口中,输入fdo,可以显示变量用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.5 在Wdm1Write处中断用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
2.6 Wdm1Write函数
NTSTATUS Wdm1Write( IN PDEVICE_OBJECT fdo,
                    IN PIRP Irp)
{
    PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
    NTSTATUS status = STATUS_SUCCESS;
    LONG BytesTxd = 0;
    // Get call parameters
    LONGLONG FilePointer = IrpStack->Parameters.Write.ByteOffset.QuadPart;
    ULONG WriteLen = IrpStack->Parameters.Write.Length;
    DebugPrint("Write %d bytes from file pointer %d",(int)WriteLen,(int)FilePointer);
    if( FilePointer<0)
        status = STATUS_INVALID_PARAMETER;
    else
    {
        // Get access to the shared buffer
        KIRQL irql;
        KeAcquireSpinLock(&BufferLock,&irql);
        BytesTxd = WriteLen;
        // (Re)allocate buffer if necessary
        if( ((ULONG)FilePointer)+WriteLen>BufferSize)
        {
            ULONG NewBufferSize = ((ULONG)FilePointer)+WriteLen;
            PVOID NewBuffer = ExAllocatePool(NonPagedPool,NewBufferSize);
            if( NewBuffer==NULL)
            {
                BytesTxd = BufferSize - (ULONG)FilePointer;
                if( BytesTxd<0) BytesTxd = 0;
            }
            else
            {
                RtlZeroMemory(NewBuffer,NewBufferSize);
                if( Buffer!=NULL)
                {
                    RtlCopyMemory(NewBuffer,Buffer,BufferSize);
                    ExFreePool(Buffer);
                }
                Buffer = (PUCHAR)NewBuffer;
                BufferSize = NewBufferSize;
            }
        }
        // Write to shared memory
        if( BytesTxd>0 && Buffer!=NULL)
            RtlCopyMemory( Buffer+FilePointer, Irp->AssociatedIrp.SystemBuffer, BytesTxd);
        // Release shared buffer
        KeReleaseSpinLock(&BufferLock,irql);
    }
    DebugPrint("Write: %d bytes written",(int)BytesTxd);
    // Complete IRP
    return CompleteIrp(Irp,status,BytesTxd);
}
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
单步调试可以观察Irp->AssociatedIrp.SystemBuffer的变化。
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客
用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客3 用WinDbg在双机环境下调试驱动程序  - lgjmfyx - lgjmfyx的博客结束语
    个人感觉来说Windbg还是一个很不错的工具,调试时不用担心主计算机系统蓝屏,重启等,而且调试方便。
评论:没有评论。
发表评论请输入评论





引文来源  用WinDbg在双机环境下调试驱动程序 - 实业兴国 实干报国 - CSDNBlog
  评论这张
 
阅读(911)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018