Showing posts with label Kernel. Show all posts
Showing posts with label Kernel. Show all posts

Wednesday, July 20, 2011

[Windows]windbg kernel debugging

debugging virtual machine using windbg:
http://www.ndis.com/ndis-debugging/virtual/vmwaresetup.htm


    Steps:
  1. (One time) On host machine, install WinDbg
  2. On the target machine (i.e., the VM running on HyperV), ran following in cmd:
    bcdedit -debug on
    bcdedit -dbgsettings serial debugport:1 baudrate:115200
  3. In HyperV’s control panel, open the settings for the VM.  Under COM1, add a Named Pipe called “MyVIrtualCom1”.
  4. Create a shortcut for windbg on host machine, make it run as administrator (otherwise, you can not access the com). The command to run windbg is something like:
  5. C:\Debuggers\windbg.exe -y SRV*C:\symbols*http://msdl.microsoft.com/download/symbols -k com:port=\\.\pipe\MyVirtualCom1,pipe,resets=0,reconnect

Monday, June 13, 2011

[Winodws]内核驱动笔记

占坑
背景知识

用户模式(User Mode):
内核模式(Kernel Mode):
Win32 API:

C++注意事项

微软官方的文档C++ for Kernel Mode Drivers: Pros and Cons
求摘要?看这里Advanced C++ features and Kernel-mode programming don't mix


cmd或者在(Start->Run)运行msinfo32,然后选"software environment"->"system drivers"可以看到当前系统里所有安装了的驱动

编译

source文件相当于Linux/Unix下的Makefile. 如何写一个source文件? 参见http://randomlearningnotes.wordpress.com/2009/04/20/using-wdkddk-build-environment-for-drivers-and-non-drivers/

build 命令相当于Linux/Unix下的make

build /c 把所有obj文件全部删除再编译

build的output很少, 详细信息可以参见编译时生成的.log,.wrn和.err文件

disable所有编译优化: 在source 文件中加设置环境变量
MSC_OPTIMIZATION=/Od /Oi

debugview是一个可以用来查看kernel debug输出信息的工具. 可以connect到local machine来查看本机的, 也可以连接到remote机器上. 后者的方法是先在remote的机器上运行debugview /c, 接着使用debugview在local机器上

常用API

信号量
KeWaitForSingleObject 阻塞当前线程直至信号变量(比如一个event object)被置成signaled
KeSetEvent 把一个event object设为signaled
KeResetEvent 把一个event object重置成not-signaled

文件操作:
ZwReadFile: Read data from the file.
ZwWriteFile: Write data to the file.
ZwQueryInformationFile: Read metadata for the file or file handle.
ZwSetInformationFile: Write metadata for the file or file handle.

Sunday, March 27, 2011

libc interface v.s. system call

啥是System Call

$man 2 intro
INTRO(2)                                                Linux Programmer's Manual                                               INTRO(2)

NAME
       intro - Introduction to system calls

DESCRIPTION
       Section  2 of the manual describes the Linux system calls.  A system call is an entry point into the Linux kernel.  Usually, sys‐
       tem calls are not invoked directly: instead, most system calls have corresponding C library wrapper functions which  perform  the
       steps  required (e.g., trapping to kernel mode) in order to invoke the system call.  Thus, making a system call looks the same as
       invoking a normal library function.


每个system call都有一个number在<syscall.h>中. system call列表在Linux Kernel Source的arch/i386/kernel/entry.S中.

strace: 跟踪一个程序, 输出程序执行过程中所调用的system call

$strace ls 
execve("/bin/ls", ["ls"], [/* 25 vars */]) = 0
brk(0)                                  = 0x25b3000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7c0f616000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=144586, ...}) = 0
...

System Call Reference