getsamps.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 522 行 · 第 1/2 页

C
522
字号
/****************************************************************************
*
*                            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:  Read sample data from a file.
*
****************************************************************************/


#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

#include "common.h"
#include "dip.h"
#include "aui.h"
#include "machtype.h"
#include "msg.h"
#include "sampinfo.h"
#include "pathlist.h"


extern void ClearSample(sio_data *curr_sio);
extern void *ProfAlloc(size_t size);
extern void ProfFree(void *ptr);
extern void *ProfRealloc(void *p,size_t new_size);
extern void *ProfCAlloc(size_t size);
extern unsigned int BigRead(int ,void *,unsigned int );
extern void ErrorMsg(char *msg,... );
extern void SetSampleInfo(sio_data *curr_sio);
extern bint LoadImageOverlays(void );
extern void SetCurrentMAD( mad_handle );

extern char             SamplePath[];
extern system_config    DefSysConfig;

sio_data                *SIOData;
sio_data                *CurrSIOData;



STATIC bint initCurrSIO( void )
/*****************************/
{
    image_info      *new_image;
    file_handle     fh;
    int             name_len;

    fh = open( SamplePath, O_RDONLY | O_BINARY, S_IREAD );
    if( fh == (file_handle) -1 ) {
        ErrorMsg( LIT( Cannot_Open_Smp_File ), SamplePath );
        return( B_FALSE );
    }
    CurrSIOData = ProfCAlloc( sizeof(sio_data) );
    CurrSIOData->fh = fh;
    name_len = strlen( SamplePath ) + 1;
    CurrSIOData->samp_file_name = ProfAlloc( name_len );
    memcpy( CurrSIOData->samp_file_name, SamplePath, name_len );
    CurrSIOData->images = ProfAlloc( 2*sizeof(pointer) );
    new_image = ProfCAlloc( sizeof(image_info) );
    name_len = strlen( LIT( Unknown_Image ) ) + 1;
    new_image->name = ProfAlloc( name_len );
    memcpy( new_image->name, LIT( Unknown_Image ), name_len );
    new_image->unknown_image = B_TRUE;
    CurrSIOData->images[0] = new_image;
    CurrSIOData->curr_image = new_image;
    new_image = ProfCAlloc( sizeof(image_info) );
    name_len = strlen( LIT( Gathered_Images ) ) + 1;
    new_image->name = ProfAlloc( name_len );
    memcpy( new_image->name, LIT( Gathered_Images ), name_len );
    new_image->ignore_gather = B_TRUE;
    new_image->gather_image = B_TRUE;
    CurrSIOData->images[1] = new_image;
    CurrSIOData->image_count = 2;
    CurrSIOData->curr_display_row = -WND_MAX_ROW;
    return( B_TRUE );
}



STATIC bint verifyHeader( void )
/******************************/
{
    file_handle     fh;
    off_t           header_position;
    off_t           tmp_position;

    fh = CurrSIOData->fh;
    header_position = lseek( fh, - ( (off_t)SIZE_HEADER ), SEEK_END );
    if( header_position == (off_t)-1 ) {
        ErrorMsg( LIT( Smp_File_IO_Err ), CurrSIOData->samp_file_name );
        return( B_FALSE );
    }
    if( read( fh, &CurrSIOData->header, SIZE_HEADER ) != SIZE_HEADER ) {
        ErrorMsg( LIT( Smp_File_IO_Err ), CurrSIOData->samp_file_name );
        return( B_FALSE );
    }
    if( CurrSIOData->header.signature != SAMP_SIGNATURE ) {
        ErrorMsg( LIT( Invalid_Smp_File ), CurrSIOData->samp_file_name );
        return( B_FALSE );
    }
    if( CurrSIOData->header.major_ver != SAMP_MAJOR_VER
     || CurrSIOData->header.minor_ver > SAMP_MINOR_VER ) {
        ErrorMsg( LIT( Incompat_Smp_File ), CurrSIOData->samp_file_name );
        return( B_FALSE );
    }
    tmp_position = lseek( fh, CurrSIOData->header.sample_start, SEEK_SET );
    if( tmp_position == (off_t) -1 ) {
        ErrorMsg( LIT( Smp_File_IO_Err ), CurrSIOData->samp_file_name );
        return( B_FALSE );
    }
    return( B_TRUE );
}



STATIC void procInfoBlock( clicks_t ticks, samp_data *data )
/**********************************************************/
{
    /* is this for backward compatability */
//    if( pref->length >= SIZE_INFO + SIZE_PREFIX &&
//                               data->info.count[ SAMP_CALLGRAPH ].number ) {
//        CallGraph = B_TRUE;     /* sample file includes callgraph records */
//    } else {
//        CallGraph = B_FALSE;    /* sample file doesn't have callgraph info */
//    }
    if( ticks == 0 ) {
        ticks = 1;
    }
    if( CurrSIOData->header.major_ver == 2 && CurrSIOData->header.minor_ver < 2 ) {
        /* pre-MAD sample file */
        CurrSIOData->config = DefSysConfig;
    } else {
        CurrSIOData->config = data->info.config;
        /* sanity check for non-MADified sampler */
        if( CurrSIOData->config.mad == MAD_NIL ) {
            CurrSIOData->config = DefSysConfig;
        }
    }
    SetCurrentMAD( CurrSIOData->config.mad );

    CurrSIOData->timer_rate = data->info.timer_rate;
}



STATIC void procMarkBlock( clicks_t tick, samp_data *data )
/*********************************************************/
{
    mark_data       *new_mark;
    int             name_len;

    name_len = strlen( data->mark.mark_string );
    new_mark = ProfCAlloc( sizeof(mark_data)+name_len );
    new_mark->tick = tick;
    new_mark->thread = data->mark.thread_id;
    /* make sure to handle overlay resolution */
    new_mark->addr.mach.segment = data->mark.addr.segment;
    new_mark->addr.mach.offset = data->mark.addr.offset;
    memcpy( new_mark->name, data->mark.mark_string, name_len );
    if( CurrSIOData->marks == NULL ) {
        new_mark->next = new_mark;
    } else {
        new_mark->next = CurrSIOData->marks->next;
        CurrSIOData->marks->next = new_mark;
    }
    CurrSIOData->marks = new_mark;
}



STATIC void procOverlayBlock( clicks_t tick, samp_data * data )
/*************************************************************/
{
    overlay_data *  new_ovl;

    new_ovl = ProfCAlloc( sizeof(overlay_data) );
    new_ovl->tick = tick;
    new_ovl->section = data->ovl.req_section;
    if( new_ovl->section & OVL_RETURN ) {
        new_ovl->overlay_return = B_TRUE;
        new_ovl->section &= ~OVL_RETURN;
    } else {
        new_ovl->overlay_return = B_FALSE;
    }
    /* make sure to handle overlay resolution */
    new_ovl->req_addr.mach.segment = data->ovl.addr.segment;
    new_ovl->req_addr.mach.offset = data->ovl.addr.offset;
    if( CurrSIOData->ovl_loads == NULL ) {
        new_ovl->next = new_ovl;
    } else {
        new_ovl->next = CurrSIOData->ovl_loads->next;
        CurrSIOData->ovl_loads->next = new_ovl;
    }
    CurrSIOData->ovl_loads = new_ovl;
}



STATIC void procImageBlock( samp_data *data, bint main_exe )
/**********************************************************/
{
    image_info      *new_image;
    int             name_len;
    int             image_index;

    name_len = strlen( data->code.name ) + 1;
    new_image = ProfCAlloc( sizeof(image_info) );
    image_index = CurrSIOData->image_count;
    CurrSIOData->image_count++;
    CurrSIOData->images = ProfRealloc( CurrSIOData->images,
                                     CurrSIOData->image_count*sizeof(pointer));
    CurrSIOData->images[image_index] = new_image;
    CurrSIOData->curr_image = new_image;
    new_image->name = ProfAlloc( name_len );
    memcpy( new_image->name, data->code.name, name_len );
    new_image->overlay_table.mach.segment = data->code.ovl_tab.segment;
    new_image->overlay_table.mach.offset = data->code.ovl_tab.offset;
    new_image->time_stamp = data->code.time_stamp;
    new_image->main_load = main_exe;
}



STATIC void procAddrBlock( uint_16 total_len, samp_data * data )
/**************************************************************/
{
    image_info *    curr_image;
    int             map_count;
    int             new_count;
    int             i;
    int             j;
    map_to_actual   *map;

    total_len -= offsetof( samp_block, d.map );
    new_count = total_len / sizeof( mapping );
    curr_image = CurrSIOData->curr_image;

⌨️ 快捷键说明

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