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

📄 reg.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:

/*
 * initObj
 */
static void initObj( PERF_DATA_BLOCK **data, PERF_OBJECT_TYPE **obj,
                     char *objname )
{
    if( *obj != NULL ) return;
    *obj = findObject( (char *)*data, objname );
}

/*
 * beginRead
 */
static void beginRead( BOOL costly ) {
    HANDLE              mut;
    CRITICAL_SECTION    *cntsect;
    DWORD               *counter;

    if( costly ) {
        mut = costlyWriteMutex;
        cntsect = &costlyReadCntSection;
        counter = &costlyReadCounter;
    } else {
        mut = writeMutex;
        cntsect = &readCntSection;
        counter = &readCounter;
    }
    EnterCriticalSection( cntsect );
    if( *counter == 0 ) {
        WaitForSingleObject( mut, INFINITE );
    }
    *counter = *counter + 1;
    LeaveCriticalSection( cntsect );
}

/*
 * endRead
 */
static void endRead( BOOL costly ) {
    HANDLE              mut;
    CRITICAL_SECTION    *cntsect;
    DWORD               *counter;

    if( costly ) {
        mut = costlyWriteMutex;
        cntsect = &costlyReadCntSection;
        counter = &costlyReadCounter;
    } else {
        mut = writeMutex;
        cntsect = &readCntSection;
        counter = &readCounter;
    }
    EnterCriticalSection( cntsect );
    *counter = *counter - 1;
    if( *counter == 0 ) {
        ReleaseMutex( mut );
    }
    LeaveCriticalSection( cntsect );
}


/*
 * GetNextThread -
 * NB - callers must continue to call this function until it returns FALSE
 */
BOOL GetNextThread( ThreadList *info, ThreadPlace *place,
                    DWORD pid, BOOL first )
{

    DWORD                       curpid;
    PERF_COUNTER_DEFINITION     *counter;
    BOOL                        error;

    error = FALSE;
    if( first ) {
        beginRead( FALSE );
        initObj( &regData, &threadObject, N_THREAD );
        if( threadObject == NULL ) error = TRUE;
        if( !error ) {
            place->index = 0;
            place->obj = threadObject;
            place->pid = pid;
            place->inst = getFirstInstance( threadObject );
        }
    } else {
        place->index ++;
        if( place->index < place->obj->NumInstances ) {
            place->inst = getNextInstance( place->inst );
        }
    }
    if( !error ) {
        counter = findCounter( place->obj, N_PROCID );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        for( ; place->index < place->obj->NumInstances; place->index ++ ) {
            if( place->inst == NULL ) {
                error = TRUE;
                break;
            }
            curpid = getCounterDWORD( place->inst, counter );
            if( curpid == place->pid ) break;
            place->inst = getNextInstance( place->inst );
        }
    }
    if( !error && place->index >= place->obj->NumInstances ) error = TRUE;
    if( !error ) {
        counter = findCounter( place->obj, N_THREADID );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        info->tid = getCounterDWORD( place->inst, counter );
        counter = findCounter( place->obj, N_BASE_PRIORITY );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        info->priority = getCounterDWORD( place->inst, counter );
    } else {
        endRead( FALSE );
    }
    return( !error );
}

/*
 * GetNextProcess
 * NB - callers must continue to call this function until it returns FALSE
 */
BOOL GetNextProcess( ProcList *info, ProcPlace *place, BOOL first ) {

    PERF_COUNTER_DEFINITION     *counter;
    BOOL                        error;

    error = FALSE;
    if( first ) {
        beginRead( FALSE );
        initObj( &regData, &processObject, N_PROCESS );
        if( processObject == NULL ) error = TRUE;
        if( !error ) {
            place->index = 0;
            place->obj = processObject;
            place->inst = getFirstInstance( processObject );
        }
    } else {
        place->index ++;
        if( place->index >= processObject->NumInstances ) {
            endRead( FALSE );
            return( FALSE );
        }
        place->inst = getNextInstance( place->inst );
    }
    if( place->inst == NULL ) error = TRUE;
    if( !error ) {
        counter = findCounter( place->obj, N_PROCID );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        info->pid = getCounterDWORD( place->inst, counter );
        counter = findCounter( place->obj, N_BASE_PRIORITY );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        info->priority = getCounterDWORD( place->inst, counter );
        wsprintf( info->name, "%ls",
                  (char *)( place->inst ) + place->inst->NameOffset );
    } else {
        endRead( FALSE );
    }
    return( !error );
}

/*
 * GetThreadInfo
 */
BOOL GetThreadInfo( DWORD pid, DWORD tid, ThreadStats *info ) {

    PERF_COUNTER_DEFINITION     *pid_counter;
    PERF_COUNTER_DEFINITION     *tid_counter;
    PERF_COUNTER_DEFINITION     *counter;
    PERF_INSTANCE_DEFINITION    *inst;
    DWORD                       curid;
    DWORD                       i;
    BOOL                        error;

    error = FALSE;
    beginRead( FALSE );
    initObj( &regData, &threadObject, N_THREAD );
    if( threadObject == NULL ) {
        error = TRUE;
    }
    if( !error ) {
        pid_counter = findCounter( threadObject, N_PROCID );
        tid_counter = findCounter( threadObject, N_THREADID );
        if( pid_counter == NULL || tid_counter == NULL ) error = TRUE;
    }
    if( !error ) {
        inst = getFirstInstance( threadObject );
        for( i=0; i < threadObject->NumInstances; i++ ) {
            if( inst == NULL ) {
                error = TRUE;
                break;
            }
            curid = getCounterDWORD( inst, tid_counter );
            if( curid == tid ) {
                curid = getCounterDWORD( inst, pid_counter );
                if( curid == pid ) break;
            }
            inst = getNextInstance( inst );
        }
    }
    if( !error && i == threadObject->NumInstances ) {
         error = TRUE;
    } else {
        info->tid = tid;
        info->pid = pid;
        counter = findCounter( threadObject, N_BASE_PRIORITY );
        info->base_pri = getCounterDWORD( inst, counter );
        counter = findCounter( threadObject, N_CUR_PRIORITY );
        info->cur_pri = getCounterDWORD( inst, counter );
        counter = findCounter( threadObject, N_THREAD_STATE );
        info->state = getCounterDWORD( inst, counter );
        counter = findCounter( threadObject, N_WAIT_REASON );
        info->wait_reason = getCounterDWORD( inst, counter );
    }
    endRead( FALSE );
    return( !error );
}

/*
 * GetProcessInfo
 */
BOOL GetProcessInfo( DWORD pid, ProcStats *info ) {

    DWORD                       i;
    PERF_COUNTER_DEFINITION     *counter;
    PERF_INSTANCE_DEFINITION    *inst;
    DWORD                       curpid;
    BOOL                        error;

    beginRead( FALSE );
    error = FALSE;
    initObj( &regData, &processObject, N_PROCESS );
    if( processObject == NULL ) error = TRUE;
    if( !error ) {
        counter = findCounter( processObject, N_PROCID );
        if( counter == NULL ) error = TRUE;
    }
    if( !error ) {
        inst = getFirstInstance( processObject );
        for( i=0; i < processObject->NumInstances; i++ ) {
            if( inst == NULL ) {
                error = TRUE;
                break;
            }
            curpid = getCounterDWORD( inst, counter );
            if( curpid == pid ) break;
            inst = getNextInstance( inst );
        }
    }
    if( !error && curpid == pid && info != NULL ) {
        info->pid = curpid;
        counter = findCounter( processObject, N_BASE_PRIORITY );
        if( counter == NULL ) {
            error = TRUE;
        } else {
            info->priority = getCounterDWORD( inst, counter );
            wsprintf( info->name, "%ls",
                      (char *)inst + inst->NameOffset );
        }
    }
    endRead( FALSE );
    return( !error && curpid == pid );
}

/*
 * FreeModuleList
 */
void FreeModuleList( char **ptr, DWORD cnt ) {

    DWORD       i;

    if( ptr == NULL ) return;
    for( i=0; i < cnt; i++ ) {
        MemFree( ptr[i] );
    }
    MemFree( ptr );
}

/*
 * getProcessIndex
 */
static BOOL getProcessIndex( DWORD pid, DWORD *indexout ) {

    DWORD                       curpid;
    PERF_COUNTER_DEFINITION     *counter;
    PERF_INSTANCE_DEFINITION    *inst;
    DWORD                       index;

    counter = findCounter( processObject, N_PROCID );
    if( counter == NULL ) return( FALSE );
    inst = getFirstInstance( processObject );
    if( inst == NULL ) return( FALSE );
    for( index=0; index < processObject->NumInstances; index++ ) {
        if( inst == NULL ) break;
        curpid = getCounterDWORD( inst, counter );
        if( curpid == pid ) {
            *indexout = index;
            return( TRUE );
        }
        inst = getNextInstance( inst );
    }
    return( FALSE );
}

/*
 * GetModuleList
 */
char **GetModuleList( DWORD pid, DWORD *cnt ) {

    DWORD                       allocsize;
    DWORD                       index;
    DWORD                       i;
    char                        **ret;
    PERF_INSTANCE_DEFINITION    *inst;
    BOOL                        error;

    error = FALSE;
    allocsize = 20;
    ret = NULL;
    beginRead( FALSE );
    beginRead( TRUE );

    initObj( &costlyData, &imageObject, N_IMAGE );
    initObj( &regData, &processObject, N_PROCESS );
    if( imageObject == NULL || processObject == NULL ) error = TRUE;
    if( !error ) {
        error = !getProcessIndex( pid, &index );
    }
    if( !error ) {
        ret = MemAlloc( allocsize * sizeof( char * ) );
        if( ret == NULL ) error = TRUE;
    }
    if( !error ) {
        inst = getFirstInstance( imageObject );
        *cnt = 0;
        for( i=0; i < imageObject->NumInstances; i += 1 ) {
            if( inst == NULL ) {
                error = TRUE;
                break;
            }
            if( inst->ParentObjectInstance == index ) {
                ret[*cnt] = MemAlloc( inst->NameLength / 2 );
                if( ret[*cnt] == NULL ) {
                    error = TRUE;
                    break;
                }
                wsprintf( ret[*cnt], "%ls", (char *)inst + inst->NameOffset );

⌨️ 快捷键说明

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