Use services thread instead of timer signals.
[wine] / misc / toolhelp.c
1 /*
2  * Misc Toolhelp functions
3  *
4  * Copyright 1996 Marcus Meissner
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <assert.h>
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "winerror.h"
15 #include "process.h"
16 #include "tlhelp32.h"
17 #include "toolhelp.h"
18 #include "heap.h"
19 #include "server.h"
20 #include "debug.h"
21
22
23 /* FIXME: to make this working, we have to callback all these registered 
24  * functions from all over the WINE code. Someone with more knowledge than
25  * me please do that. -Marcus
26  */
27 static struct notify
28 {
29     HTASK16   htask;
30     FARPROC16 lpfnCallback;
31     WORD     wFlags;
32 } *notifys = NULL;
33
34 static int nrofnotifys = 0;
35
36 static FARPROC16 HookNotify = NULL;
37
38 BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
39                               WORD wFlags )
40 {
41     int i;
42
43     TRACE(toolhelp, "(%x,%lx,%x) called.\n",
44                       htask, (DWORD)lpfnCallback, wFlags );
45     if (!htask) htask = GetCurrentTask();
46     for (i=0;i<nrofnotifys;i++)
47         if (notifys[i].htask==htask)
48             break;
49     if (i==nrofnotifys) {
50         if (notifys==NULL)
51             notifys=(struct notify*)HeapAlloc( SystemHeap, 0,
52                                                sizeof(struct notify) );
53         else
54             notifys=(struct notify*)HeapReAlloc( SystemHeap, 0, notifys,
55                                         sizeof(struct notify)*(nrofnotifys+1));
56         if (!notifys) return FALSE;
57         nrofnotifys++;
58     }
59     notifys[i].htask=htask;
60     notifys[i].lpfnCallback=lpfnCallback;
61     notifys[i].wFlags=wFlags;
62     return TRUE;
63 }
64
65 BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
66 {
67     int i;
68     
69     TRACE(toolhelp, "(%x) called.\n", htask );
70     if (!htask) htask = GetCurrentTask();
71     for (i=nrofnotifys;i--;)
72         if (notifys[i].htask==htask)
73             break;
74     if (i==-1)
75         return FALSE;
76     memcpy(notifys+i,notifys+(i+1),sizeof(struct notify)*(nrofnotifys-i-1));
77     notifys=(struct notify*)HeapReAlloc( SystemHeap, 0, notifys,
78                                         (nrofnotifys-1)*sizeof(struct notify));
79     nrofnotifys--;
80     return TRUE;
81 }
82
83 BOOL16 WINAPI StackTraceCSIPFirst16(STACKTRACEENTRY *ste, WORD wSS, WORD wCS, WORD wIP, WORD wBP)
84 {
85     return TRUE;
86 }
87
88 BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
89 {
90     return TRUE;
91 }
92
93 BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
94 {
95     return TRUE;
96 }
97
98 /***********************************************************************
99  *           ToolHelpHook                             (KERNEL.341)
100  *      see "Undocumented Windows"
101  */
102 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
103 {
104 FARPROC16 tmp;
105         tmp = HookNotify;
106         HookNotify = lpfnNotifyHandler;
107         /* just return previously installed notification function */
108         return tmp;
109 }
110
111
112 /***********************************************************************
113  *           CreateToolHelp32Snapshot                   (KERNEL32.179)
114  */
115 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process ) 
116 {
117     struct create_snapshot_request req;
118     struct create_snapshot_reply reply;
119
120     TRACE( toolhelp, "%lx,%lx\n", flags, process );
121     if (flags & (TH32CS_SNAPHEAPLIST|TH32CS_SNAPMODULE|TH32CS_SNAPTHREAD))
122         FIXME( toolhelp, "flags %lx not implemented\n", flags );
123     if (!(flags & TH32CS_SNAPPROCESS))
124     {
125         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
126         return INVALID_HANDLE_VALUE;
127     }
128
129     /* Now do the snapshot */
130     req.flags   = flags & ~TH32CS_INHERIT;
131     req.inherit = (flags & TH32CS_INHERIT) != 0;
132     CLIENT_SendRequest( REQ_CREATE_SNAPSHOT, -1, 1, &req, sizeof(req) );
133     CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
134     return reply.handle;
135 }
136
137
138 /***********************************************************************
139  *              TOOLHELP_Process32Next
140  *
141  * Implementation of Process32First/Next
142  */
143 static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY lppe, BOOL first )
144 {
145     struct next_process_request req;
146     struct next_process_reply reply;
147
148     if (lppe->dwSize < sizeof (PROCESSENTRY))
149     {
150         SetLastError( ERROR_INSUFFICIENT_BUFFER );
151         ERR (toolhelp, "Result buffer too small\n");
152         return FALSE;
153     }
154     req.handle = handle;
155     req.reset = first;
156     CLIENT_SendRequest( REQ_NEXT_PROCESS, -1, 1, &req, sizeof(req) );
157     if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
158     lppe->cntUsage            = 1;
159     lppe->th32ProcessID       = (DWORD)reply.pid;
160     lppe->th32DefaultHeapID   = 0;  /* FIXME */ 
161     lppe->th32ModuleID        = 0;  /* FIXME */
162     lppe->cntThreads          = reply.threads;
163     lppe->th32ParentProcessID = 0;  /* FIXME */
164     lppe->pcPriClassBase      = reply.priority;
165     lppe->dwFlags             = -1; /* FIXME */
166     lppe->szExeFile[0]        = 0;  /* FIXME */
167     return TRUE;
168 }
169
170
171 /***********************************************************************
172  *              Process32First    (KERNEL32.555)
173  *
174  * Return info about the first process in a toolhelp32 snapshot
175  */
176 BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY lppe)
177 {
178     return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE );
179 }
180
181 /***********************************************************************
182  *              Process32Next   (KERNEL32.556)
183  *
184  * Return info about the "next" process in a toolhelp32 snapshot
185  */
186 BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY lppe)
187 {
188     return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE );
189 }
190
191 /***********************************************************************
192  *              Module32First   (KERNEL32.527)
193  *
194  * Return info about the "first" module in a toolhelp32 snapshot
195  */
196 BOOL WINAPI Module32First(HANDLE hSnapshot, LPMODULEENTRY lpme)
197 {
198     FIXME(toolhelp,"(%d,%p),stub!\n",hSnapshot,lpme);
199     return FALSE;
200 }
201
202 /***********************************************************************
203  *              Module32Next   (KERNEL32.528)
204  *
205  * Return info about the "next" module in a toolhelp32 snapshot
206  */
207 BOOL WINAPI Module32Next(HANDLE hSnapshot, LPMODULEENTRY lpme)
208 {
209     FIXME(toolhelp,"(%d,%p),stub!\n",hSnapshot,lpme);
210     return FALSE;
211 }