如何调试驱动程序加载错误?

时间:2009-11-13 15:24:58

标签: c windows kernel driver

我为Windows制作了一个驱动程序,编译了它并试图通过SC管理器启动它,但是我从SC管理器API中得到了系统错误:

ERROR_PROC_NOT_FOUND The specified procedure could not be found.

有没有办法获得有关驱动程序无法启动的原因的更多信息? WinDbg还是什么?如果我在DriverEntry例程中注释掉所有代码,则驱动程序启动。

我唯一要调用的是另一个源模块中的过程(尽管在我自己的项目中)。我可以注释掉所有外部依赖项,但仍然会遇到同样的错误。

修改
我也尝试了不同的DDK,即2003 DDK和Vista WDK(但不是Win7 WDK)

EDIT2: 这是我的驱动程序酸代码文件driver.cpp:

#ifdef __cplusplus
extern "C" {
#endif
#include <ntddk.h>
#include <ntstrsafe.h>
#ifdef __cplusplus
}; // extern "C"
#endif

#include "../distorm/src/distorm.h"

void DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
}

#define MAX_INSTRUCTIONS 20

#ifdef __cplusplus
extern "C" {
#endif
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
 UNICODE_STRING pFcnName;

 // Holds the result of the decoding.
 _DecodeResult res;
 // Decoded instruction information.
 _DecodedInst decodedInstructions[MAX_INSTRUCTIONS];
 // next is used for instruction's offset synchronization.
 // decodedInstructionsCount holds the count of filled instructions' array by the decoder.
 unsigned int decodedInstructionsCount = 0, i, next;
 // Default decoding mode is 32 bits, could be set by command line.
 _DecodeType dt = Decode32Bits;

 // Default offset for buffer is 0, could be set in command line.
 _OffsetType offset = 0;
 char* errch = NULL;

 // Buffer to disassemble.
 char *buf;
 int len = 100;

 // Register unload routine
 DriverObject->DriverUnload = DriverUnload;

 DbgPrint("diStorm Loaded!\n");

 // Get address of KeBugCheck
 RtlInitUnicodeString(&pFcnName, L"KeBugCheck");
 buf = (char *)MmGetSystemRoutineAddress(&pFcnName);
 offset = (unsigned) (_OffsetType)buf;

 DbgPrint("Resolving KeBugCheck @ 0x%08x\n", buf);
 // Decode the buffer at given offset (virtual address).

 while (1) {
  res = distorm_decode(offset, (const unsigned char*)buf, len, dt, decodedInstructions, MAX_INSTRUCTIONS, &decodedInstructionsCount);
  if (res == DECRES_INPUTERR) {
   DbgPrint(("NULL Buffer?!\n"));
   break;
  }

  for (i = 0; i < decodedInstructionsCount; i++) {
   // Note that we print the offset as a 64 bits variable!!!
   // It might be that you'll have to change it to %08X...
   DbgPrint("%08I64x (%02d) %s %s %s\n", decodedInstructions[i].offset, decodedInstructions[i].size, 
    (char*)decodedInstructions[i].instructionHex.p,
    (char*)decodedInstructions[i].mnemonic.p,
    (char*)decodedInstructions[i].operands.p);
  }

  if (res == DECRES_SUCCESS || decodedInstructionsCount == 0) {
   break; // All instructions were decoded.
  }

  // Synchronize:
  next = (unsigned int)(decodedInstructions[decodedInstructionsCount-1].offset - offset);
  next += decodedInstructions[decodedInstructionsCount-1].size;

  // Advance ptr and recalc offset.
  buf += next;
  len -= next;
  offset += next;
 }

 DbgPrint(("Done!\n"));
 return STATUS_SUCCESS;
}

#ifdef __cplusplus
}; // extern "C"
#endif

我的目录结构如下:

base_dir\driver\driver.cpp
        \distorm\src\all_the_c_files
        \distorm\distorm.h
        \distorm\config.h

我的来源文件:

# $Id$
TARGETNAME=driver
TARGETPATH=obj
TARGETTYPE=DRIVER

# Additional defines for the C/C++ preprocessor
C_DEFINES=$(C_DEFINES) -DSUPPORT_64BIT_OFFSET

SOURCES=driver.cpp \
     distorm_dummy.c \
        drvversion.rc

INCLUDES=..\distorm\src;

TARGETLIBS=$(DDK_LIB_PATH)\ntdll.lib \
        $(DDK_LIB_PATH)\ntstrsafe.lib

您可以从此处下载diStorm:http://ragestorm.net/distorm/dl.php?id=8

distorm_dummy与diStorm lib中的dummy.c相同。

5 个答案:

答案 0 :(得分:3)

使用gflags启用“显示加载程序快照” - 在调试输出中,您应该找到有关加载程序无法解析的导入的信息。

答案 1 :(得分:2)

毫不奇怪,您拥有自己解决此问题所需的所有信息。

  

ERROR_PROC_NOT_FOUND找不到指定的程序。

这与您的依赖性Walker输出相结合,几乎指向一个损坏的Import Table

为什么你的IT坏了?我不确定,你的构建/链接器设置可能有问题,因为很明显,HAL.DLL就在%windir%\ system32中。

加载订单损坏的原因很多,您必须自己跟踪它们。

答案 2 :(得分:1)

您是否尝试在已编译的.sys上运行Dependency Walker并查看是否确实存在一些缺少的函数导入?

答案 3 :(得分:0)

您可以在WinDbg中添加延迟断点。

如果你指定一个断点,当驱动程序没有加载(或使用bu)时,它会在驱动程序加载并进入函数时被触发。

用于指定断点的命令是:

bp <module_name>!<function_name>

e.g。 :

bp my_driver!DriverEntry

答案 4 :(得分:0)

使用6000 WDK / DDK构建它(因为使用“实际”Build 7600 ...它链接到wdfldr.sys,但在Windows Vista和XP Systems下,此sys文件不可用)。 我不知道你可以在哪里正式下载,但我确实使用了洪流...