⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proclist.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <windows.h>
#include <stdio.h>
#include "drwatcom.h"
#include "srchmsg.h"
#include "priority.h"
#include "menu.h"
#include "jdlg.h"
#include "madrtn.h"
#include "madsys.h"


typedef struct {
    DWORD       procid;
    ProcStats   stats;
    HANDLE      hdl;            // used for owned processes only
} ProcPriorityInfo;

static ProcNode         *procList;
static HWND             procDlg;

ProcNode *FindProcess( DWORD process ) {

    ProcNode    *cur;

    cur = procList;
    while( cur != NULL ) {
        if( cur->procid == process ) break;
        cur = cur->next;
    }
    return( cur );
}

void GetProcName( DWORD process, char *name ) {

    ProcNode    *cur;

    cur = FindProcess( process );
    if( cur != NULL && cur ->procname != NULL ) {
        strcpy( name, cur->procname );
    } else {
        *name =  '\0';
    }
}


static DWORD getStackPtr( HANDLE threadhdl )
{
    mad_registers   *regs;
    addr_ptr        addr;

    AllocMadRegisters( &regs );
    LoadMADRegisters( regs, threadhdl );
    MADRegSpecialGet( MSR_SP, regs, &addr );
    DeAllocMadRegisters( regs );
    return( addr.offset );
}
/*
 * AddThread
 */

void AddThread( DWORD procid, DWORD threadid, HANDLE threadhdl ) {

    ProcNode    *process;
    ThreadNode  *new;

    process = FindProcess( procid );
    if( process != NULL ) {
        new = FindThread( process, threadid );
        if( new == NULL ) {
            new = MemAlloc( sizeof( ThreadNode ) );
            new->threadid = threadid;
            new->threadhdl = threadhdl;
            new->next = process->thread;
            process->thread = new;
            new->stack = getStackPtr( threadhdl );
        } else {
            if( new->threadhdl == NULL ) {
                new->threadhdl = threadhdl;
                new->stack = getStackPtr( threadhdl );
            }
        }
    }
}

/*
 * AddProcess
 */
void AddProcess( DWORD procid, HANDLE prochdl, DWORD threadid,
                 HANDLE threadhdl )
{
    ProcNode            *process;
    ProcNode            *new;
    ProcStats           stats;
    unsigned            cnt;
    BOOL                noprocinfo;

    process = FindProcess( procid );
    RefreshInfo();
    cnt = 0;
    noprocinfo = FALSE;
    while( !GetProcessInfo( procid, &stats ) ) {
        Sleep( 100 );
        RefreshInfo();
        if( cnt > 100 ) {
            noprocinfo = TRUE;
            break;
        }
    }
    if( process == NULL ) {
#if (defined M_I86 || defined M_I386)
        CONTEXT context;
#endif

        new = MemAlloc( sizeof( ProcNode ) );
        new->procid = procid;
        new->prochdl = prochdl;
        new->thread = NULL;
        if( noprocinfo ) {
            new->procname[0] = '\0';
        } else {
            strcpy( new->procname, stats.name );
        }
#if (defined M_I86 || defined M_I386)
        context.ContextFlags = CONTEXT_SEGMENTS | CONTEXT_CONTROL;
        GetThreadContext( threadhdl, &context );
        new->SegCs = context.SegCs;
        new->SegDs = context.SegDs;
#else
        new->SegCs = 1;
        new->SegDs = 1;
#endif
        new->next = procList;
        procList = new;
    }
    AddThread( procid, threadid, threadhdl );
}

/*
 * freeModuleNode
 */

static void freeModuleNode( ModuleNode *node ) {
    if( node != NULL ) {
        if( node->syminfo != NULL ) {
            MemFree( node->syminfo );
        }
        if( node->name != NULL ) {
            MemFree( node->name );
        }
        if( node->objects != NULL ) {
            MemFree( node->objects );
        }
        if( node->fhdl != NULL ) {
            CloseHandle( node->fhdl );
        }
        MemFree( node );
    }
}

/*
 * GetFirstModule
 */
ModuleNode *GetFirstModule( ProcNode *procinfo ) {
    if( procinfo == NULL ) return( NULL );
    return( procinfo->module );
}

/*
 * GetNextModule
 */
ModuleNode *GetNextModule( ModuleNode *modinfo ) {
    if( modinfo == NULL ) return( NULL );
    return( modinfo->next );
}

/*
 * AddModule
 */
void AddModule( DWORD procid, HANDLE fhdl, DWORD base, char *name ) {

    ProcNode    *process;
    ModuleNode  *new;
    ModuleNode  **cur;

    process = FindProcess( procid );
    if( process != NULL ) {
        cur = &process->module;
        for( ;; ) {
            if( (*cur) == NULL ) break;
            if( (*cur)->base > base ) break;
            cur = &(*cur)->next;
        }
        new = MemAlloc( sizeof( ModuleNode ) );
        new->next = (*cur);
        (*cur) = new;
        new->syminfo = NULL;
        new->base = base;
        new->fhdl = fhdl;
        new->name = name;
        new->procnode = process;
        if( !GetModuleSize( fhdl, &new->size ) ) {
            new->size = -1;
        }
        new->objects = GetModuleObjects( fhdl, &new->num_objects );
    }
}

/*
 * ModuleFromAddr
 */
ModuleNode *ModuleFromAddr( ProcNode *proc, void *addr ) {

    ModuleNode  *cur;

    if( proc == NULL ) {
        return( NULL );
    }
    cur = proc->module;
    for( ;; ) {
        if( cur == NULL ) break;
        if( cur->base > (DWORD)addr ) {
            cur = NULL;
            break;
        }
        if( cur->size == -1 ) {
            if( cur->next == NULL || (DWORD)addr < cur->next->base ) break;
        } else {
            if( (DWORD)addr < cur->base + cur->size ) break;
        }
        cur = cur->next;
    }
    return( cur );
}

/*
 * RemoveModule
 */
void RemoveModule( DWORD procid, DWORD base ) {

    ProcNode    *process;
    ModuleNode  **cur;
    ModuleNode  *toremove;

    process = FindProcess( procid );
    if( process != NULL ) {
        cur = &process->module;
        while( *cur != NULL ) {
            if( (*cur)->base == base ) {
                toremove = *cur;
                *cur = (*cur)->next;
                freeModuleNode( toremove );
                break;
            }
            cur = &(*cur)->next;
        }
    }
}

/*
 * FindThread
 */

ThreadNode *FindThread( ProcNode *procnode, DWORD threadid ) {

    ThreadNode          *thread;

    if( procnode == NULL ) {
        thread = NULL;
    } else {
        thread = procnode->thread;
        while( thread != NULL ) {
            if( thread->threadid == threadid ) break;
            thread = thread->next;
        }
    }
    return( thread );
}

/*
 * freeThreadNode
 */
static void freeThreadNode( ThreadNode *node ) {

    if( node == NULL ) {
        CloseHandle( node->threadhdl );
        MemFree( node );
    }
}

/*
 * RemoveThread
 */
void RemoveThread( DWORD processid, DWORD threadid ) {

    ProcNode    *process;
    ThreadNode  **cur;
    ThreadNode  *tmp;

    process = FindProcess( processid );
    if( process != NULL ) {
        cur = &process->thread;
        while( *cur != NULL ) {
            tmp = *cur;
            if( tmp->threadid == threadid ) {
                *cur = tmp->next;
                freeThreadNode( tmp );
                break;
            }
            cur = &tmp->next;
        }
        if( process->thread == NULL ) {
            RemoveProcess( processid );
        }
        if( procDlg != NULL ) {
            SendMessage( procDlg, DR_TASK_LIST_CHANGE, 0, 0L );
        }
    }
}

/*
 * freeProcNode
 */
static void freeProcNode( ProcNode *node ) {

    ModuleNode  *mod;

    if( node != NULL ) {
        while( node->module != NULL ) {
            mod = node->module;
            node->module = mod->next;
            freeModuleNode( mod );
        }
        CloseHandle( node->prochdl );
        MemFree( node );
    }
}


/*
 * RemoveProcess
 */
void RemoveProcess( DWORD process ) {

    ProcNode    **proc;
    ProcNode    *tmp;
    ThreadNode  *thread;
    ThreadNode  *tofree;

    proc = &procList;
    while( *proc != NULL ) {
        tmp = *proc;
        if( tmp->procid == process ) {
            *proc = tmp->next;
            break;
        }
        proc = &tmp->next;
    }
    if( tmp != NULL ) {
        thread  = tmp->thread;
        while( thread != NULL ) {
            tofree = thread;
            thread = thread->next;
            freeThreadNode( tofree );
        }
        freeProcNode( tmp );
        if( procDlg != NULL ) {
            SendMessage( procDlg, DR_TASK_LIST_CHANGE, 0, 0L );
        }
    }
}

/*
 * GetNextOwnedProc
 */
ProcNode *GetNextOwnedProc( ProcNode *cur ) {
    if( cur == NULL ) {
        return( procList );
    } else {
        return( cur->next );
    }
}

/*
 * enableProcChoices
 */
static void enableProcChoices( HWND hwnd, BOOL enable ) {
    EnableWindow( GetDlgItem( hwnd, PROCCTL_KILL ), enable );
    EnableWindow( GetDlgItem( hwnd, PROCCTL_THREAD ), enable );
    EnableWindow( GetDlgItem( hwnd, PROCCTL_VIEWMEM ), enable );
    EnableWindow( GetDlgItem( hwnd, PROCCTL_SET_PRIORITY ), enable );
    EnableWindow( GetDlgItem( hwnd, PROCCTL_ATTATCH ), enable );
#ifndef CHICAGO

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -