toolhelp.c 14 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Misc Toolhelp functions
 *
 * Copyright 1996 Marcus Meissner
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#include "config.h"

23
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24 25
#include <stdlib.h>
#include <string.h>
26 27 28
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
29
#include <ctype.h>
30
#include <assert.h>
31
#include "windef.h"
32 33
#include "winbase.h"
#include "wine/winbase16.h"
34
#include "winerror.h"
35
#include "local.h"
36
#include "tlhelp32.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
37
#include "toolhelp.h"
38
#include "wine/server.h"
39
#include "wine/debug.h"
40

41
WINE_DEFAULT_DEBUG_CHANNEL(toolhelp);
42

43

44
/* FIXME: to make this work, we have to call back all these registered
Alexandre Julliard's avatar
Alexandre Julliard committed
45 46 47 48 49
 * functions from all over the WINE code. Someone with more knowledge than
 * me please do that. -Marcus
 */
static struct notify
{
Alexandre Julliard's avatar
Alexandre Julliard committed
50
    HTASK16   htask;
Alexandre Julliard's avatar
Alexandre Julliard committed
51
    FARPROC16 lpfnCallback;
Alexandre Julliard's avatar
Alexandre Julliard committed
52 53 54 55 56
    WORD     wFlags;
} *notifys = NULL;

static int nrofnotifys = 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
57 58
static FARPROC16 HookNotify = NULL;

59
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
60
 *		NotifyRegister (TOOLHELP.73)
61
 */
62
BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
Alexandre Julliard's avatar
Alexandre Julliard committed
63
                              WORD wFlags )
Alexandre Julliard's avatar
Alexandre Julliard committed
64 65 66
{
    int	i;

67
    FIXME("(%x,%lx,%x), semi-stub.\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
68
                      htask, (DWORD)lpfnCallback, wFlags );
Alexandre Julliard's avatar
Alexandre Julliard committed
69
    if (!htask) htask = GetCurrentTask();
Alexandre Julliard's avatar
Alexandre Julliard committed
70 71 72 73 74
    for (i=0;i<nrofnotifys;i++)
        if (notifys[i].htask==htask)
            break;
    if (i==nrofnotifys) {
        if (notifys==NULL)
75
            notifys=(struct notify*)HeapAlloc( GetProcessHeap(), 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
76
                                               sizeof(struct notify) );
Alexandre Julliard's avatar
Alexandre Julliard committed
77
        else
78
            notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
Alexandre Julliard's avatar
Alexandre Julliard committed
79 80
                                        sizeof(struct notify)*(nrofnotifys+1));
        if (!notifys) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
81 82 83 84 85 86 87 88
        nrofnotifys++;
    }
    notifys[i].htask=htask;
    notifys[i].lpfnCallback=lpfnCallback;
    notifys[i].wFlags=wFlags;
    return TRUE;
}

89
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
90
 *		NotifyUnregister (TOOLHELP.74)
91
 */
92
BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
Alexandre Julliard's avatar
Alexandre Julliard committed
93 94
{
    int	i;
95

96
    FIXME("(%x), semi-stub.\n", htask );
Alexandre Julliard's avatar
Alexandre Julliard committed
97
    if (!htask) htask = GetCurrentTask();
Alexandre Julliard's avatar
Alexandre Julliard committed
98 99 100 101 102 103
    for (i=nrofnotifys;i--;)
        if (notifys[i].htask==htask)
            break;
    if (i==-1)
        return FALSE;
    memcpy(notifys+i,notifys+(i+1),sizeof(struct notify)*(nrofnotifys-i-1));
104
    notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
Alexandre Julliard's avatar
Alexandre Julliard committed
105
                                        (nrofnotifys-1)*sizeof(struct notify));
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107 108
    nrofnotifys--;
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
109

110
/***********************************************************************
111
 *		StackTraceCSIPFirst (TOOLHELP.67)
112
 */
113
BOOL16 WINAPI StackTraceCSIPFirst16(STACKTRACEENTRY *ste, WORD wSS, WORD wCS, WORD wIP, WORD wBP)
Alexandre Julliard's avatar
Alexandre Julliard committed
114
{
115
    FIXME("(%p, ss %04x, cs %04x, ip %04x, bp %04x): stub.\n", ste, wSS, wCS, wIP, wBP);
Alexandre Julliard's avatar
Alexandre Julliard committed
116 117 118
    return TRUE;
}

119
/***********************************************************************
120
 *		StackTraceFirst (TOOLHELP.66)
121
 */
122
BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
Alexandre Julliard's avatar
Alexandre Julliard committed
123
{
124
    FIXME("(%p, %04x), stub.\n", ste, Task);
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126 127
    return TRUE;
}

128
/***********************************************************************
129
 *		StackTraceNext (TOOLHELP.68)
130
 */
131
BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
Alexandre Julliard's avatar
Alexandre Julliard committed
132
{
133
    FIXME("(%p), stub.\n", ste);
Alexandre Julliard's avatar
Alexandre Julliard committed
134 135
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
136

137
/***********************************************************************
138
 *		InterruptRegister (TOOLHELP.75)
139
 */
140 141
BOOL16 WINAPI InterruptRegister16( HTASK16 task, FARPROC callback )
{
142
    FIXME("(%04x, %p), stub.\n", task, callback);
143 144 145
    return TRUE;
}

146
/***********************************************************************
147
 *		InterruptUnRegister (TOOLHELP.76)
148
 */
149 150
BOOL16 WINAPI InterruptUnRegister16( HTASK16 task )
{
151
    FIXME("(%04x), stub.\n", task);
152 153 154
    return TRUE;
}

155 156 157 158 159 160
/***********************************************************************
 *           TimerCount   (TOOLHELP.80)
 */
BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
{
    /* FIXME
161
     * In standard mode, dwmsSinceStart = dwmsThisVM
162 163 164
     *
     * I tested this, under Windows in enhanced mode, and
     * if you never switch VM (ie start/stop DOS) these
165
     * values should be the same as well.
166 167
     *
     * Also, Wine should adjust for the hardware timer
168
     * to reduce the amount of error to ~1ms.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
     * I can't be bothered, can you?
     */
    pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
    return TRUE;
}

/***********************************************************************
 *           SystemHeapInfo   (TOOLHELP.71)
 */
BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
{
    WORD user = LoadLibrary16( "USER.EXE" );
    WORD gdi = LoadLibrary16( "GDI.EXE" );
    pHeapInfo->wUserFreePercent = (int)LOCAL_CountFree(user) * 100 / LOCAL_HeapSize(user);
    pHeapInfo->wGDIFreePercent  = (int)LOCAL_CountFree(gdi) * 100 / LOCAL_HeapSize(gdi);
    pHeapInfo->hUserSegment = user;
    pHeapInfo->hGDISegment  = gdi;
    FreeLibrary16( user );
    FreeLibrary16( gdi );
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
192 193 194 195
/***********************************************************************
 *           ToolHelpHook                             (KERNEL.341)
 *	see "Undocumented Windows"
 */
196
FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
Alexandre Julliard's avatar
Alexandre Julliard committed
197
{
198 199 200
	FARPROC16 tmp;

	FIXME("(%p), stub.\n", lpfnNotifyHandler);
Alexandre Julliard's avatar
Alexandre Julliard committed
201 202 203 204 205
	tmp = HookNotify;
	HookNotify = lpfnNotifyHandler;
	/* just return previously installed notification function */
	return tmp;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
206

207

Alexandre Julliard's avatar
Alexandre Julliard committed
208
/***********************************************************************
209
 *           CreateToolhelp32Snapshot			(KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
210
 */
211
HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
212
{
213
    HANDLE ret;
214

215
    TRACE("%lx,%lx\n", flags, process );
216
    if (!(flags & (TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD|TH32CS_SNAPMODULE)))
217
    {
218
        FIXME("flags %lx not implemented\n", flags );
219
        SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
220
        return INVALID_HANDLE_VALUE;
221
    }
222

223
    /* Now do the snapshot */
224
    SERVER_START_REQ( create_snapshot )
225
    {
226 227 228 229 230
        req->flags = 0;
        if (flags & TH32CS_SNAPMODULE)   req->flags |= SNAP_MODULE;
        if (flags & TH32CS_SNAPPROCESS)  req->flags |= SNAP_PROCESS;
        if (flags & TH32CS_SNAPTHREAD)   req->flags |= SNAP_THREAD;
    
231
        req->inherit = (flags & TH32CS_INHERIT) != 0;
232
        req->pid     = process;
233 234
        wine_server_call_err( req );
        ret = reply->handle;
235 236
    }
    SERVER_END_REQ;
237
    if (!ret) ret = INVALID_HANDLE_VALUE;
238
    return ret;
239
}
240 241


242 243 244 245 246 247 248
/***********************************************************************
 *		TOOLHELP_Thread32Next
 *
 * Implementation of Thread32First/Next
 */
static BOOL TOOLHELP_Thread32Next( HANDLE handle, LPTHREADENTRY32 lpte, BOOL first )
{
249
    BOOL ret;
250 251 252 253 254 255 256

    if (lpte->dwSize < sizeof(THREADENTRY32))
    {
        SetLastError( ERROR_INSUFFICIENT_BUFFER );
        ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(THREADENTRY32), lpte->dwSize);
        return FALSE;
    }
257
    SERVER_START_REQ( next_thread )
258 259 260
    {
        req->handle = handle;
        req->reset = first;
261
        if ((ret = !wine_server_call_err( req )))
262
        {
263 264 265 266 267
            lpte->cntUsage           = reply->count;
            lpte->th32ThreadID       = (DWORD)reply->tid;
            lpte->th32OwnerProcessID = (DWORD)reply->pid;
            lpte->tpBasePri          = reply->base_pri;
            lpte->tpDeltaPri         = reply->delta_pri;
268 269 270 271 272
            lpte->dwFlags            = 0;  /* SDK: "reserved; do not use" */
        }
    }
    SERVER_END_REQ;
    return ret;
273 274
}

275
/***********************************************************************
276
 *		Thread32First    (KERNEL32.@)
277 278 279
 *
 * Return info about the first thread in a toolhelp32 snapshot
 */
280
BOOL WINAPI Thread32First(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
281
{
282
    return TOOLHELP_Thread32Next(hSnapshot, lpte, TRUE);
283 284 285
}

/***********************************************************************
286
 *		Thread32Next   (KERNEL32.@)
287 288 289
 *
 * Return info about the "next" thread in a toolhelp32 snapshot
 */
290
BOOL WINAPI Thread32Next(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
291
{
292
    return TOOLHELP_Thread32Next(hSnapshot, lpte, FALSE);
293 294
}

295 296 297 298 299
/***********************************************************************
 *		TOOLHELP_Process32Next
 *
 * Implementation of Process32First/Next
 */
300
static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY32 lppe, BOOL first )
301
{
302
    BOOL ret;
303 304
    WCHAR exe[MAX_PATH];
    DWORD len;
305

306
    if (lppe->dwSize < sizeof(PROCESSENTRY32))
307
    {
308
        SetLastError( ERROR_INSUFFICIENT_BUFFER );
309
        ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(PROCESSENTRY32), lppe->dwSize);
310
        return FALSE;
311
    }
312
    SERVER_START_REQ( next_process )
313 314 315
    {
        req->handle = handle;
        req->reset = first;
316
        wine_server_set_reply( req, exe, sizeof(exe) );
317
        if ((ret = !wine_server_call_err( req )))
318
        {
319
            lppe->cntUsage            = reply->count;
320
            lppe->th32ProcessID       = reply->pid;
321 322
            lppe->th32DefaultHeapID   = (DWORD)reply->heap;
            lppe->th32ModuleID        = (DWORD)reply->module;
323
            lppe->cntThreads          = reply->threads;
324
            lppe->th32ParentProcessID = reply->ppid;
325
            lppe->pcPriClassBase      = reply->priority;
326
            lppe->dwFlags             = -1; /* FIXME */
327 328 329
            len = WideCharToMultiByte( CP_ACP, 0, exe, wine_server_reply_size(reply) / sizeof(WCHAR),
                                       lppe->szExeFile, sizeof(lppe->szExeFile), NULL, NULL );
            lppe->szExeFile[len] = 0;
330 331 332 333
        }
    }
    SERVER_END_REQ;
    return ret;
334 335
}

336

337
/***********************************************************************
338
 *		Process32First    (KERNEL32.@)
339
 *
340 341
 * Return info about the first process in a toolhelp32 snapshot
 */
342
BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
343
{
344
    return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE );
345 346 347
}

/***********************************************************************
348
 *		Process32Next   (KERNEL32.@)
349
 *
350 351
 * Return info about the "next" process in a toolhelp32 snapshot
 */
352
BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
353
{
354
    return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
355
}
Marcus Meissner's avatar
Marcus Meissner committed
356

357 358 359 360 361 362 363 364

/***********************************************************************
 *		TOOLHELP_Module32Next
 *
 * Implementation of Module32First/Next
 */
static BOOL TOOLHELP_Module32Next( HANDLE handle, LPMODULEENTRY32 lpme, BOOL first )
{
365
    BOOL ret;
366 367
    WCHAR exe[MAX_PATH];
    DWORD len;
368

369 370 371 372 373 374
    if (lpme->dwSize < sizeof (MODULEENTRY32))
    {
        SetLastError( ERROR_INSUFFICIENT_BUFFER );
        ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(MODULEENTRY32), lpme->dwSize);
        return FALSE;
    }
375
    SERVER_START_REQ( next_module )
376 377 378
    {
        req->handle = handle;
        req->reset = first;
379
        wine_server_set_reply( req, exe, sizeof(exe) );
380
        if ((ret = !wine_server_call_err( req )))
381 382
        {
            lpme->th32ModuleID   = 0;  /* toolhelp internal id, never used */
383
            lpme->th32ProcessID  = reply->pid;
384
            lpme->GlblcntUsage   = 0; /* FIXME */
385 386
            lpme->ProccntUsage   = 0; /* FIXME */
            lpme->modBaseAddr    = reply->base;
387
            lpme->modBaseSize    = reply->size;
388
            lpme->hModule        = reply->base;
389
            lpme->szModule[0]    = 0;  /* FIXME */
390 391 392
            len = WideCharToMultiByte( CP_ACP, 0, exe, wine_server_reply_size(reply) / sizeof(WCHAR),
                                       lpme->szExePath, sizeof(lpme->szExePath), NULL, NULL );
            lpme->szExePath[len] = 0;
393 394 395 396
        }
    }
    SERVER_END_REQ;
    return ret;
397 398
}

Marcus Meissner's avatar
Marcus Meissner committed
399
/***********************************************************************
400
 *		Module32First   (KERNEL32.@)
Marcus Meissner's avatar
Marcus Meissner committed
401 402 403
 *
 * Return info about the "first" module in a toolhelp32 snapshot
 */
404
BOOL WINAPI Module32First(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
Marcus Meissner's avatar
Marcus Meissner committed
405
{
406
    return TOOLHELP_Module32Next( hSnapshot, lpme, TRUE );
Marcus Meissner's avatar
Marcus Meissner committed
407 408 409
}

/***********************************************************************
410
 *		Module32Next   (KERNEL32.@)
Marcus Meissner's avatar
Marcus Meissner committed
411 412 413
 *
 * Return info about the "next" module in a toolhelp32 snapshot
 */
414
BOOL WINAPI Module32Next(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
Marcus Meissner's avatar
Marcus Meissner committed
415
{
416
    return TOOLHELP_Module32Next( hSnapshot, lpme, FALSE );
Marcus Meissner's avatar
Marcus Meissner committed
417
}
418 419

/************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
420
 *              GlobalMasterHandle (KERNEL.28)
421 422
 *
 *
423
 * Should return selector and handle of the information structure for
424 425 426 427 428 429 430
 * the global heap. selector and handle are stored in the THHOOK as
 * pGlobalHeap and hGlobalHeap.
 * As Wine doesn't have this structure, we return both values as zero
 * Applications should interpret this as "No Global Heap"
 */
DWORD WINAPI GlobalMasterHandle16(void)
{
431
    FIXME(": stub\n");
432 433
    return 0;
}
434 435 436 437 438 439 440 441 442 443

/************************************************************************
 *              Heap32ListFirst (KERNEL.@)
 *
 */
BOOL WINAPI Heap32ListFirst(HANDLE hSnapshot, LPHEAPLIST32 lphl)
{
    FIXME(": stub\n");
    return FALSE;
}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

/******************************************************************
 *		Toolhelp32ReadProcessMemory (KERNEL.@)
 *
 *
 */
BOOL WINAPI Toolhelp32ReadProcessMemory(DWORD pid, const void* base,
                                        void* buf, SIZE_T len, SIZE_T* r)
{
    HANDLE h;
    BOOL   ret = FALSE;

    h = (pid) ? OpenProcess(PROCESS_VM_READ, FALSE, pid) : GetCurrentProcess();
    if (h != NULL)
    {
        ret = ReadProcessMemory(h, base, buf, len, r);
        if (pid) CloseHandle(h);
    }
    return ret;
}