Tuesday, September 28, 2010

The first part of the Win32 debugging API


In this tutorial we will learn Win32 available to developers for debugging in the original language. At the end of tutorial we will learn how to debug a process.
Download example program.

Theory:
Win32 for programmers to use some API, which provides the equivalent function of the debugger. They were called the Win32 debugging API (or original language). Use the API, we can:

Load a program or bundled into a running program for debugging on
Obtained by low-level debugging of the program information, such as process ID, enter the address, the image base address and so on.
When the incident occurred and when debugging the notice, such as process / thread start / end, DLL loading / discharge, etc..
Modify the debugging process or thread
In short, we can use the API to write a simple debugger. As some of this subject is too large, I divide it into several parts, but this tutorial is the first part of it. In this tutorial, I will explain some of basic concepts and the general framework of the Win32 debugging API.
Use the Win32 debugging API, the following steps:

Create a process or tied to a running processes. This is the first step in using the Win32 debugging API. As our program to play the role of the debugger, we find a program for debugging. A program being debugged is called To debuggee. can be obtained in two ways debuggee:
Created by CreateProcess debuggee process. In order to create a process being debugged, you must specify DEBUG_PROCESS flag. This flag tells Windows that we want to debug the process. Debuggee occur when important events related with the debug (debug event), Windows will give us The program sends notification. debuggee immediately suspended to wait for our program ready. If debuggee also created a child process, Windows will for each child in the process of debugging events to send notification to our programs. This feature is usually not necessary. we can specify a combination of signs DEBUG_PROCESS DEBUG_ONLY_THIS_PROCESS and to ban it.
We can also use the DebugActiveProcess flag tied to a running processes.
Waiting for debug events. Debuggee process in obtaining a post, debuggee the main thread is suspended, the situation will continue to call WaitForDebugEvent up our procedures. WaitForXXX this function and other similar functions, for example, it blocks the calling thread until waiting for events. For this function, it wait for Windows to send debug events from the. Here is its definition:
WaitForDebugEvent proto lpDebugEvent: DWORD, dwMilliseconds: DWORD

lpDebugEvent is the address of a DEBUG_EVENT this structure will be filled on the debuggee debug events that occur in the information.

dwMilliseconds the function to wait for debugging events of the time, in milliseconds. If there is no time to debug the incident, WaitForDebugEvent return to the caller. On the other hand, if the parameter is designated as INFIN99vE constant, the function will have to wait until the debug events .

Now we look at DEBUG_EVENT structure.

DEBUG_EVENT STRUCT
dwDebugEventCode dd?
dwProcessId dd?
dwThreadId dd?
u DEBUGSTRUCT <>
DEBUG_EVENT ENDS

dwDebugEventCode The value specifies the wait events occurred in the type of debugging. because there are many types of events occur, our procedure to check the value, know the type of events and respond. The value of the possible values are as follows:

Value meaning
CREATE_PROCESS_DEBUG_EVENT process is created. When debuggee process has just been created (not running) or our program just to DebugActiveProcess tied to a process running when the event occurs. This is our procedure should be the first event.
EX99v_PROCESS_DEBUG_EVENT process exit.
CREATE_THEAD_DEBUG_EVENT When a new thread is created in deuggee process or our program to run in the first binding event occurs during the process. It should be noted that when debugge the main thread is created will not receive the notice.
EX99v_THREAD_DEBUG_EVENT debuggee the thread exit event. Debugee exit the main thread does not receive the notice. We believe that the main thread and debugge debuggee process is synonymous. So when we see CREATE_PROCESS_DEBUG_EVENT signs the procedure, the main thread it is CREATE_THREAD_DEBUG_EVENT logo.
LOAD_DLL_DEBUG_EVENT debuggee into a DLL. When the PE loader DLL first decomposition point to a link, we will receive the event. (When you call CreateProcess into debuggee time) and when debuggee also occurs when calling LoadLibrary.
UNLOAD_DLL_DEBUG_EVENT a DLL is unloaded from debuggee events.
EXCEPTION_DEBUG_EVENT in debuggee abnormal events occur. Note: This event is only the beginning in debuggee took place before the first instruction of its time. Exception is actually a debug interrupt (int 3h). If you want to restore debuggee things to DBG_CONTINUE signs calling ContinueDebugEvent function. Do not use the logo or debuggee DBG_EXCEPTION_NOT_HANDLED refuse to run in the NT (Win98 to run well).
OUTPUT_DEBUG_STRING_EVENT when debuggee DebugOutputString function call to send a message to our string when the event occurs.
RIP_EVENT system debugging error

dwProcessId and dwThreadId debug the course of events took place and thread Id. We can use these values as we are interested in the process or thread identifier. Remember, if we use CreateProcess to load debuggee, we can get debuggee in PROCESS_INFO the process of structural and thread. We can use these values to different debug event occurred in debuggee in the process or its children (if not specified DEBUG_ONLY_THIS_PROCESS signs).

u is a joint, including the commissioning event for more information. dwDebugEventCode different according to the above, it can be the following structure:

dwDebugEventCode u explained
CREATE_PROCESS_DEBUG_EVENT structure called CreateProcessInfo the CREATE_PROCESS_DEBUG_INFO
EX99v_PROCESS_DEBUG_EVENT structure called ExitProcess the EX99v_PROCESS_DEBUG_INFO
CREATE_THREAD_DEBUG_EVENT structure called CreateThread the CREATE_THREAD_DEBUG_INFO
EX99v_THREAD_DEBUG_EVENT structure called ExitThread the EX99v_THREAD_DEBUG_EVENT
LOAD_DLL_DEBUG_EVENT structure called LoadDll the LOAD_DLL_DEBUG_INFO
UNLOAD_DLL_DEBUG_EVENT structure called UnloadDll the UNLOAD_DLL_DEBUG_INFO
EXCEPTION_DEBUG_EVENT structure called the Exception of EXCEPTION_DEBUG_INFO
OUTPUT_DEBUG_STRING_EVENT structure called DebugString the OUTPUT_DEBUG_STRING_INFO
RIP_EVENT structure called RipInfo of RIP_INFO

In this tutorial I will not say all of these structures in detail, here only to talk about CREATE_PROCESS_DEBUG_INFO structure in detail.
Suppose our program calls the WaitForDebugEvent function and returns, the first thing we need to do is to check the value in terms debuggee dwDebugEventCode process occurred in the type of debug event. For example, if dwDebugEventCode value CREATE_PROCESS_DEBUG_EVENT, on u can be considered a member of the CreateProcessInfo with u.CreateProcessInfo to visit.

In our program, do the debugging event response. When WaitForDebugEvent return, which means that the process took place in the debuggee debug event or timeout occurred. So our procedure to check dwDebugEventCode to make an appropriate response. Here are some like handling Windows messages: the user to select and ignore messages.
Continue to run debuggee. When the debug event occurs, Windows hangs a debuggee, so when we finished with debugging event, but also to debuggee continue. ContinueDebugEvent function call to complete the process.
ContinueDebugEvent proto dwProcessId: DWORD, dwThreadId: DWORD, dwContinueStatus: DWORD

The function recovery as thread debugging events pending.
dwProcessId and dwThreadId is to restore the thread's process ID and thread ID, usually the two values from DEBUG_EVENT structure dwProcessId and dwThreadId members are.
dwContinueStatus report shows how to debug the event thread. There are two possible values: DBG_CONTINUE and DBG_EXCEPTION_NOT_HANDLED. For most debugging events, the two values are the same: to restore the thread. The only exception is EXCEPTION_DEBUG_EVENT, if the thread is reported in an exception debug event, which means that the thread in the debuggee an exception occurs. If you specify DBG_CONTINUE, the thread will ignore its own exception handling section and continue. In this case, our process must be to DBG_CONTINUE restoration of prior thread and handle the exception, or exceptions will be endless if we continue to occur .... DBG_EXCEPTION_NOT_HANDLED specified value, is to tell Windows that our procedure does not handle the exception: Windows will use the default exception handler debuggee to deal with exception.
In short, if our program does not consider the exceptions, and debugging process of the event has a point debuggee exception, it should call the function with DBG_CONTINUE logo ContinueDebugEvent. Otherwise, our program will have to DBG_EXCEPTION_NOT_HANDLED call ContinueDebugEvent. But in following this circumstances must DBG_CONTINUE signs: the first members of the value in ExceptionCode EXCEPTION_BREAKPOINT of EXCEPTION_DEBUG_EVENT event. When debuggee started its first instruction, our function will receive exception debug event. It is in fact a debug interrupt (int 3h). If we are to respond to DBG_EXCEPTION_NOT_HANDLED call ContinueDebugEvent debug events, Windows NT will refuse debuggee (because it is no exception handling). So in this case, the use DBG_CONTINUE flag tells Windows that we want the thread to continue execution.

Continue the above steps until the debuggee process cycle exit. Our program must be a message loop like the infinite loop until the end debuggee. The cycle of roughly as follows:
. While TRUE
invoke WaitForDebugEvent, addr DebugEvent, INFIN99vE
. Break. If DebugEvent.dwDebugEventCode == EX99v_PROCESS_DEBUG_EVENT

invoke ContinueDebugEvent, DebugEvent.dwProcessId, DebugEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED
. Endw


That is, when starting the debugger, we can not debuggee separate program until it ended.

Let us summarize these steps:

Create a process or a bundle of our program to run the processes.
Wait for the debug events
Response debug event.
Continue debuggee.
To continue this endless loop until the end of the process debuggee
Examples:
This example shows debug a win32 program and handle, such as process, process Id, image base address and so on.

.386
. Model flat, stdcall
option casemap: none
include masm32includewindows.inc
include masm32includekernel32.inc
include masm32includecomdlg32.inc
include masm32includeuser32.inc
includelib masm32libkernel32.lib
includelib masm32libcomdlg32.lib
includelib masm32libuser32.lib
. Data
AppName db "Win32 Debug Example no.1", 0
ofn OPENFILENAME <>
FilterString db "Executable Files", 0, "*. exe", 0
db "All Files", 0 ,"*.*", 0,0
ExitProc db "The debuggee exits", 0
NewThread db "A new thread is created", 0
EndThread db "A thread is destroyed", 0
ProcessInfo db "File Handle:% lx", 0dh, 0Ah
db "Process Handle:% lx", 0Dh, 0Ah
db "Thread Handle:% lx", 0Dh, 0Ah
db "Image Base:% lx", 0Dh, 0Ah
db "Start Address:% lx", 0
. Data?
buffer db 512 dup (?)
startinfo STARTUPINFO <>
pi PROCESS_INFORMATION <>
DBEvent DEBUG_EVENT <>
. Code
start:
mov ofn.lStructSize, sizeof ofn
mov ofn.lpstrFilter, offset FilterString
mov ofn.lpstrFile, offset buffer
mov ofn.nMaxFile, 512
mov ofn.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetOpenFileName, ADDR ofn
. If eax == TRUE
invoke GetStartupInfo, addr startinfo
invoke CreateProcess, addr buffer, NULL, NULL, NULL, FALSE, DEBUG_PROCESS + DEBUG_ONLY_THIS_PROCESS, NULL, NULL, addr startinfo, addr pi
. While TRUE
invoke WaitForDebugEvent, addr DBEvent, INFIN99vE
. If DBEvent.dwDebugEventCode == EX99v_PROCESS_DEBUG_EVENT
invoke MessageBox, 0, addr ExitProc, addr AppName, MB_OK + MB_ICONINFORMATION
. Break
. Elseif DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT
invoke wsprintf, addr buffer, addr ProcessInfo, DBEvent.u.CreateProcessInfo.hFile, DBEvent.u.CreateProcessInfo.hProcess, DBEvent.u.CreateProcessInfo.hThread, DBEvent.u.CreateProcessInfo.lpBaseOfImage, DBEvent.u.CreateProcessInfo.lpStartAddress
invoke MessageBox, 0, addr buffer, addr AppName, MB_OK + MB_ICONINFORMATION
. Elseif DBEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
. If DBEvent.u.Exception.pExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT
invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE
. Continue
. Endif
. Elseif DBEvent.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT
invoke MessageBox, 0, addr NewThread, addr AppName, MB_OK + MB_ICONINFORMATION
. Elseif DBEvent.dwDebugEventCode == EX99v_THREAD_DEBUG_EVENT
invoke MessageBox, 0, addr EndThread, addr AppName, MB_OK + MB_ICONINFORMATION
. Endif
invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED
. Endw
invoke CloseHandle, pi.hProcess
invoke CloseHandle, pi.hThread
. Endif
invoke ExitProcess, 0
end start

Analysis:
Program first fill OPENFILENAME structure, called GetOpenFileName allows users to choose to debug procedures.

invoke GetStartupInfo, addr startinfo
invoke CreateProcess, addr buffer, NULL, NULL, NULL, FALSE, DEBUG_PROCESS + DEBUG_ONLY_THIS_PROCESS, NULL, NULL, addr startinfo, addr pi

When receiving the user selection, call the CreateProcess loader. And call the default value of fill GetStartupInfo STARTUPINFO structure. NOTE We will DEBUG_PROCESS mark and logo combination DEBUG_ONLY_THIS_PROCESS only debug the program, not including the child process.

. While TRUE
invoke WaitForDebugEvent, addr DBEvent, INFIN99vE


Is loaded in the debuggee, we call WaitForDebugEvent into endless loop debugging, WaitForDebugEvent debug event occurs in the debuggee returns, because we specified INFIN99vE as the second argument. When debug event occurs, WaitForDebugEvent back and fill DBEvent structure.

. If DBEvent.dwDebugEventCode == EX99v_PROCESS_DEBUG_EVENT
invoke MessageBox, 0, addr ExitProc, addr AppName, MB_OK + MB_ICONINFORMATION
. Break

We first check dwDebugEventCode of value if it is EX99v_PROCESS_DEBUG_EVENT, with a message box displays "The debuggee exits" and exit the debugging cycle.

. Elseif DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT
invoke wsprintf, addr buffer, addr ProcessInfo, DBEvent.u.CreateProcessInfo.hFile, DBEvent.u.CreateProcessInfo.hProcess, DBEvent.u.CreateProcessInfo.hThread, DBEvent.u.CreateProcessInfo.lpBaseOfImage, DBEvent.u.CreateProcessInfo.lpStartAddress
invoke MessageBox, 0, addr buffer, addr AppName, MB_OK + MB_ICONINFORMATION

If dwDebugEventCode value CREATE_PROCESS_DEBUG_EVENT, we show some interest in the message box the bottom of the information. The information obtained from the u.CreateProcessInfo. CreateProcessInfo a CREATE_PROCESS_DEBUG_INFO type of structure. You can access Win32 API access to it for more information e .

. Elseif DBEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT
. If DBEvent.u.Exception.pExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT
invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE
. Continue
. Endif

If dwDebugEventCode value EXCEPTION_DEBUG_EVENT, we will further check the exception type. It is a lot of nested structure, but we can get from the exception type ExceptionCode members. If ExceptionCode value EXCEPTION_BREAKPOINT and was the first place (or we No known deuggee int 3h instructions), we can safely assume that the first instruction to be executed in debuggee this exception occurs when. in our completion of these processing, you can call ContinueDebugEvent with DBG_CONTINUE to continue debuggee. Then we continue to wait next debug events.

. Elseif DBEvent.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT
invoke MessageBox, 0, addr NewThread, addr AppName, MB_OK + MB_ICONINFORMATION
. Elseif DBEvent.dwDebugEventCode == EX99v_THREAD_DEBUG_EVENT
invoke MessageBox, 0, addr EndThread, addr AppName, MB_OK + MB_ICONINFORMATION
. Endif

If dwDebugEventCode value CREATE_THREAD_DEBUG_EVENT or EX99v_THREAD_DEBUG_EVENT, our program displays a message box.

invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED
. Endw

In addition to the above discussed EXCEPTION_DEBUG_EVENT, with signs calling ContinueDebugEvent function DBG_EXCEPTION_NOT_HANDLED resume debuggee implementation.

invoke CloseHandle, pi.hProcess
invoke CloseHandle, pi.hThread

When debuggee the end, we jumped out of the debugging cycle, this time to close the thread and process handles debuggee. To close the handle does not mean to shut down these processes and threads. Just not use these handles Bale.







Recommended links:



Easy Seasonal - Screen Savers



Additional team bonus, energy stimulate team



TS TO MKV



3GP To WMV



Shared experience OF domestic boutique Foxmail 5.0 (multi-map)



Acer executives boasted that Dell no longer pose a threat to Acer



"Torchlight" HARDCORE mode Survival Guide



Magic Assistant: can not control the computer keystrokes



Move on



Digital TV in Chongqing three months free premium channels



Firefox DOWNLOAD break 5 million mark will be issued a limited edition of 50 sets of



DivX to IPod



Premier Screen Capture



Tuesday, September 14, 2010

Operators tangle: 3G what is the best billing



Without considering the number of sticky premise, you will choose which operators of 3G services? Estimate many would say, depends on which operator's network and the prices are cheap.

However, From these two indicators, placed in front of people is a full problem. Because of China's three operators using three kinds of different network standards, unfortunately, at present, the three operators in the 3G Internet business basically used 3 different billing methods:

China Telecom is mainly a long time and billing, traffic accounting by China Mobile, China Unicom is unique brand new billing and accounting concepts: M / T as multimedia content downloads billing unit.

Three operators using different billing methods, of course mainly from its own business characteristics and network the conditions. China Telecom has a wealth of fixed broadband resources, while a considerable part of a nationwide deployment of a large-scale urban WiFi network. Users can use broadband indoors, outdoors you can use EVDO, EVDO network in case of bad use of WiFi, the three major networks through a seamless integration of network coverage. In view of the three different network technologies, long-time billing is undoubtedly the most viable option, but also more in line with the formation of fixed broadband users habits.

China Mobile to traffic accounting program at this stage despite opposition by many consumers, but it does more in line with the current conditions of China Mobile's network.

As China Mobile's 3G network coverage is relatively not so perfect, "If the Internet when network conditions are bad places, users within a longer time than usual, just visit a website, then the user, is based on traffic charges more cost-effective or more cost-effective long time then? "a China Mobile insider on the outside world has such" high rates "challenge to be refuted.

China Unicom's tariff program also fully incorporates its own characteristics. As the WCDMA network in the experiences of foreign countries, as well as speeds has some advantages, WCDMA will become an important way for Internet access, in the process, download the content to the user fee will be the biggest concern.

M / T's billing sucked video and text based content to more reflect the image of the way, such as a song 2 M, 3 T, a novel, as each M and each T charges is clear and, therefore, the user also be further quantified for each download, in addition to no longer receive any further flow of charge value-added services to dispel the concerns of users.

Although the three major operators designed to meet the conditions of the tariff program itself, but to put these programs to the practice of market competition, inexperience 3G operators have begun to highlight the situation.

China Telecom "sad" to find, as are three means of access by users to choose, rather than automatically assigned according to the network conditions, a considerable proportion of users will be preferred EVDO network speeds faster than WiFi and ADSL, which allow EVDO networks overwhelmed.

China Mobile also had to face the reality: Nearly all of the users are accusing their rates are too high, according to traffic accounting statistics is not easy, so that access and network cards in this market, far below expectations. Only China Unicom, as a large-scale users of the development of not yet started, the user feedback received the least.

Thus, the three operators under the competition began to have to make changes to the billing. China Telecom, the phone line to take in accordance with traffic accounting, the mobile also "low key" to launch a long-time billing of two packages, in addition, a dream in the current mobile Internet, mobile also began to press "a" fee-charging such as a cell phone or a music video the charges, in addition to no longer receive traffic charges, which Unicom M / T billing would be similar.

Which accounting method is best? In fact, depending on customer needs. If a user wants to own MSN 24 hours online, while the actual MSN traffic is very small, so he must tend to charge by traffic; But if a user wants to watch a movie online, then he needs is a long time and billing , or press "one (the Department)" fee.

So, be sure the next three operators will adopt a more pluralistic approach to charging, in this regard to solve the operator's network limitations, on the other hand can meet the different demands of more users, which is more intense competition in the market inevitable.

As for the pre-market strategies, we might as an initial market research. As mentioned above, as China Mobile, who said: "We also need to test the market response, gradually reducing tariffs is a good thing after all, but a very low start thinking about improving tougher nut to crack."







Recommended links:



Quicktime Swf



wav CONVERTER



Rmvb to dvd



Mkv