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

📄 memview.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 <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "drwatcom.h"
#include "memwnd.h"
#include "mem.h"

typedef struct {
    HANDLE                      prochdl;
    DWORD                       procid;
    ProcStats                   *stats;
    HWND                        labels;
    DWORD                       label_hite;
    LBoxHdl                     *lbox;
    MemListData                 listdata;
} MemWalkerInfo;

static HWND                     curWalkHwnd;

#define MEM_ALLOC_INCR          200
#define MEM_LIMIT               0x80000000
#define MEM_WALKER_CLASS        "Dr_WATCOM_NT_mem_Walker"


void FormatMemListEntry( char *buf, MemListItem *item ) {

    char                        *prot;
    char                        *state;
    MEMORY_BASIC_INFORMATION    *mbi;

    mbi = &item->mbi;
    switch( mbi->State ) {
    case MEM_COMMIT:
        state = AllocRCString( STR_COMMITTED );
        break;
    case MEM_FREE:
        state = AllocRCString( STR_FREE );
        break;
    case MEM_RESERVE:
        state = AllocRCString( STR_RESERVED );
        break;
    default:
        state = AllocRCString( STR_UNKNOWN );
    }
    if( mbi->Protect & PAGE_READONLY ) {
        prot = "RO";
    } else if( mbi->Protect & PAGE_READWRITE ) {
        prot = "RW";
    } else if( mbi->Protect & PAGE_WRITECOPY ) {
        prot = "WC";
    } else if( mbi->Protect & PAGE_EXECUTE ) {
        prot = "Ex";
    } else if( mbi->Protect & PAGE_EXECUTE_READ ) {
        prot = "ExRO";
    } else if( mbi->Protect & PAGE_EXECUTE_READWRITE ) {
        prot = "ExRW";
    } else if( mbi->Protect & PAGE_EXECUTE_WRITECOPY ) {
        prot = "ExWC";
    } else if( mbi->Protect & PAGE_NOACCESS ) {
        prot = "NA";
    } else {
        if( mbi->State == MEM_RESERVE ) {
            prot = "NA";
        } else {
            prot = "??";
        }
    }
    sprintf( buf, "%08lX %08lX  %8lX %-4s %-10s %-9s %s", mbi->BaseAddress,
                mbi->AllocationBase, mbi->RegionSize, prot, state,
                item->objname, item->modname );
    FreeRCString( state );
}

/*
 * redrawMemList
 */
static void redrawMemList( LBoxHdl *hdl, MemListData *info ) {

    DWORD       i;
    char        buf[150];

    ClearListBox( hdl );
    for( i=0; i < info->used; i++ ) {
        FormatMemListEntry( buf, info->data[i] );
        LBStrPrintf( hdl, buf );
    }
    SendMessage( GetListBoxHwnd( hdl ), LB_SETTOPINDEX, 0, 0 );
}

/*
 * createMemListItem
 */
static MemListItem *createMemListItem( char *objname, char *modname,
                                        MEMORY_BASIC_INFORMATION *mbi )
{
    MemListItem         *new;
    DWORD               namelen;

    namelen = strlen( modname ) + 1;
    new = MemAlloc( sizeof( MemListItem ) + namelen + strlen( objname ) + 1 );
    strcpy( new->data, modname );
    strcpy( new->data + namelen, objname );
    new->mbi = *mbi;
    new->modname = new->data;
    new->objname = new->data + namelen;
    return( new );
}

/*
 * identifySingleStack - identify the memory pages that are the stack for
 *                       a give thread
 */
static void identifySingleStack( ProcNode *pnode, ThreadNode *tnode,
                                 MemListData *list ) {

    BOOL                        found_guard;
    MEMORY_BASIC_INFORMATION    mbi;
    DWORD                       i;
    DWORD                       last;
    MemListItem                 *old;
    char                        modname[20];
    char                        *objname;

    VirtualQueryEx( pnode->prochdl, (void *) tnode->stack, &mbi,
                    sizeof( mbi ) );
    if( mbi.AllocationBase != 0 ) {
        for( i=0; i < list->used; i++ ) {
            if( list->data[i]->mbi.AllocationBase == mbi.AllocationBase ) break;
        }
        if( i == list->used ) return;
        last = i + 1;
        /*
         * stack space is contiguous and all pages in a stack share the
         * same allocation base
         */
        while( list->data[last]->mbi.AllocationBase == mbi.AllocationBase ) {
            last++;
        }
        found_guard = FALSE;
        for( ; i < last; i++ ) {
            /*
             * identify the guard page
             * The guard page is a single page of committed memory at
             * the lower end of committed memory and immediately adjacent
             * to the stack's reserved memory. If there is no more reserved
             * stack space left, the guard page will be the last page of
             * committed memory.  A stack cannot exist without a guard page.
             * There will be at most three regions of memory in the stack:
             * the actual stack space is committed, the guard page is
             * committed and the remaining address space is marked reserved.
             */
            if( !found_guard && list->data[i]->mbi.State == MEM_COMMIT ) {
                objname = AllocRCString( STR_STACK_GUARD );
                found_guard = TRUE;
            } else {
                objname = AllocRCString( STR_STACK );
            }
            sprintf( modname, "tid: %08lX", tnode->threadid );
            old = list->data[i];
            list->data[i] = createMemListItem( objname, modname, &old->mbi );
            FreeRCString( objname );
            MemFree( old );
        }
    }
}

/*
 * identifyAllStacks
 */
static void identifyAllStacks( ProcNode *pnode, MemListData *list ) {

    ThreadNode          *thread;

    if( pnode == NULL ) {
        thread = NULL;
    } else {
        thread = pnode->thread;
        while( thread != NULL ) {
            identifySingleStack( pnode, thread, list );
            thread = thread->next;
        }
    }
}

/*
 * addMemListItem
 */
static void addMemListItem( ProcNode *pnode, MemListData *info,
                            MEMORY_BASIC_INFORMATION *mbi ) {

    ModuleNode                  *mnode;
    MemListItem                 *new;
    ObjectInfo                  *obj;
    DWORD                       i;
    DWORD                       rva;
    char                        *name;
    char                        *objname;
    MEMORY_BASIC_INFORMATION    topass;

    if( info->used >= info->allocated ) {
        info->allocated += MEM_ALLOC_INCR;
        info->data = MemReAlloc( info->data,
                                 info->allocated * sizeof( void * ) );
    }
    mnode = ModuleFromAddr( pnode, mbi->BaseAddress );
    if( mnode == NULL ) {
        name = "";
        objname = "";
    } else {
        if( mnode->name == NULL ) {
            name = "";
        } else {
            name = mnode->name;
        }
        obj = mnode->objects;
        rva = (DWORD)mbi->BaseAddress - mnode->base;
        for( i=0; i < mnode->num_objects; i++ ) {

⌨️ 快捷键说明

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