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 = true; new_ovl->section &= ~OVL_RETURN; } else { new_ovl->overlay_return = 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 procRemapBlock( clicks_t tick, uint_16 total_len, samp_data *data ) /************************************************************/ { remap_data *new_remap; int count; int index; index = 0; total_len -= offsetof( samp_block, d.remap ); count = total_len / sizeof( remapping ); while( count-- > 0 ) { new_remap = ProfCAlloc( sizeof(remap_data) ); if( CurrSIOData->remaps == NULL ) { new_remap->next = new_remap; } else { new_remap->next = CurrSIOData->remaps->next; CurrSIOData->remaps->next = new_remap; } CurrSIOData->remaps = new_remap; new_remap->tick = tick; new_remap->section = data->remap.data[index].section; new_remap->segment = data->remap.data[index].segment; index++; } }
STATIC void initRoutineInfo( file_info *curr_file ) /*************************************************/ { rtn_info *new_rtn; int name_len; ClearRoutineInfo( curr_file ); name_len = strlen( LIT( Unknown_Routine ) ) + 1; new_rtn = ProfCAlloc( sizeof( rtn_info ) + name_len ); memcpy( new_rtn->name, LIT( Unknown_Routine ), name_len ); new_rtn->unknown_routine = true; curr_file->routine = ProfCAlloc( 2 * sizeof( pointer ) ); curr_file->routine[0] = new_rtn; name_len = strlen( LIT( Gathered_Routines ) ) + 1; new_rtn = ProfCAlloc( sizeof( rtn_info ) + name_len ); memcpy( new_rtn->name, LIT( Gathered_Routines ), name_len ); new_rtn->ignore_gather = true; new_rtn->gather_routine = true; curr_file->routine[1] = new_rtn; curr_file->rtn_count = 2; }
STATIC void initModuleInfo( image_info *curr_image ) /**************************************************/ { mod_info *new_mod; int name_len; ClearModuleInfo( curr_image ); name_len = strlen( LIT( Unknown_Module ) ) + 1; new_mod = ProfCAlloc( sizeof( mod_info ) + name_len ); memcpy( new_mod->name, LIT( Unknown_Module ), name_len ); new_mod->unknown_module = true; initFileInfo( new_mod ); curr_image->module = ProfCAlloc( 2 * sizeof( pointer ) ); curr_image->module[0] = new_mod; name_len = strlen( LIT( Gathered_Modules ) ) + 1; new_mod = ProfCAlloc( sizeof( mod_info ) + name_len ); memcpy( new_mod->name, LIT( Gathered_Modules ), name_len ); new_mod->ignore_gather = true; new_mod->gather_module = true; initFileInfo( new_mod ); curr_image->module[1] = new_mod; curr_image->mod_count = 2; }
STATIC void initFileInfo( mod_info *curr_mod ) /********************************************/ { file_info *new_file; int file_len; ClearFileInfo( curr_mod ); file_len = strlen( LIT( Unknown_File ) ) + 1; new_file = ProfCAlloc( sizeof( file_info ) + file_len ); memcpy( new_file->name, LIT( Unknown_File ), file_len ); new_file->unknown_file = true; initRoutineInfo( new_file ); curr_mod->mod_file = ProfCAlloc( 2 * sizeof( pointer ) ); curr_mod->mod_file[0] = new_file; file_len = strlen( LIT( Gathered_Files ) ) + 1; new_file = ProfCAlloc( sizeof( file_info )+file_len ); memcpy( new_file->name, LIT( Gathered_Files ), file_len ); new_file->ignore_gather = true; new_file->gather_file = true; initRoutineInfo( new_file ); curr_mod->mod_file[1] = new_file; curr_mod->file_count = 2; }
extern wp_srcfile * WPSourceOpen( sio_data * curr_sio, bint quiet ) /*****************************************************************/ { file_info * curr_file; rtn_info * curr_rtn; wp_srcfile * wpsrc_file; void * src_file; mod_info * curr_mod; cue_handle * ch; location_list ll; int line; curr_mod = curr_sio->curr_mod; curr_file = curr_sio->curr_file; curr_rtn = curr_sio->curr_rtn; if( curr_file->unknown_file ) { src_file = NULL; } else { src_file = FOpenSource( curr_file->name, curr_mod->mh, curr_file->fid ); } if( src_file == NULL ) { curr_sio->src_file = NULL; if( !quiet ) { if( curr_file->unknown_file ) { ErrorMsg( LIT( Src_File_Not_Known ) ); } else { ErrorMsg( LIT( Src_File_Not_Found ), curr_file->name ); } } return( NULL ); } wpsrc_file = ProfCAlloc( sizeof( wp_srcfile ) ); wpsrc_file->src_file = src_file; curr_sio->src_file = wpsrc_file; if( SymLocation( curr_rtn->sh, NULL, &ll ) == DS_OK ) { ch = alloca( DIPHandleSize( HK_CUE ) ); AddrCue( curr_mod->mh, ll.e[0].u.addr, ch ); wpsrc_file->rtn_line = CueLine( ch ); } setSrcLineData( wpsrc_file, curr_sio, curr_mod, curr_file, curr_rtn ); line = 1; for( ;; ) { WPSourceGetLine( curr_sio->sample_window, line ); if( wpsrc_file->src_eof ) break; line++; } wpsrc_file->src_rows = line - 1; return( wpsrc_file ); }
STATIC bool 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( 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 = 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 = true; new_image->gather_image = true; CurrSIOData->images[1] = new_image; CurrSIOData->image_count = 2; CurrSIOData->curr_display_row = -WND_MAX_ROW; return( true ); }
STATIC walk_result loadModuleInfo( mod_handle mh, void *_curr_image ) /*******************************************************************/ { image_info *curr_image = _curr_image; mod_info *new_mod; int mod_count; int name_len; name_len = DIPModName( mh, NULL, 0 ); new_mod = ProfCAlloc( sizeof( mod_info ) + name_len ); DIPModName( mh, new_mod->name, name_len + 1 ); new_mod->mh = mh; mod_count = curr_image->mod_count; curr_image->mod_count++; curr_image->module = ProfRealloc( curr_image->module, curr_image->mod_count * sizeof( pointer ) ); curr_image->module[mod_count] = new_mod; initFileInfo( new_mod ); DIPWalkSymList( SS_MODULE, &mh, &loadRoutineInfo, new_mod ); return( WR_CONTINUE ); }
STATIC file_info *loadFileInfo( mod_info *curr_mod, sym_handle *sym ) /********************************************************************/ { file_info *sym_file; cue_handle *ch; cue_fileid fid; int file_count; int count; location_list ll; if( SymLocation( sym, NULL, &ll ) != DS_OK ) { return( curr_mod->mod_file[0] ); } ch = alloca( DIPHandleSize( HK_CUE ) ); switch( AddrCue( curr_mod->mh, ll.e[0].u.addr, ch ) ) { case SR_NONE: case SR_FAIL: return( curr_mod->mod_file[0] ); } fid = CueFileId( ch ); file_count = curr_mod->file_count; count = 0; while( count < file_count ) { sym_file = curr_mod->mod_file[count]; if( sym_file->fid == fid ) { return( sym_file ); } count++; } curr_mod->file_count++; curr_mod->mod_file = ProfRealloc( curr_mod->mod_file, curr_mod->file_count * sizeof( pointer ) ); count = CueFile( ch, NULL, 0 ) + 1; sym_file = ProfCAlloc( sizeof( file_info ) + count ); sym_file->fid = fid; CueFile( ch, sym_file->name, count ); initRoutineInfo( sym_file ); curr_mod->mod_file[file_count] = sym_file; return( sym_file ); }
STATIC walk_result loadRoutineInfo( sym_walk_info swi, sym_handle *sym, void *_new_mod ) /*********************************************************************/ { mod_info *new_mod = _new_mod; sym_info sinfo; file_info *sym_file; rtn_info *new_rtn; int rtn_count; int name_len; int sym_size; int demangle_type; if( swi != SWI_SYMBOL ) { return( WR_CONTINUE ); } SymInfo( sym, NULL, &sinfo ); if( sinfo.kind != SK_CODE && sinfo.kind != SK_PROCEDURE ) { return( WR_CONTINUE ); } sym_file = loadFileInfo( new_mod, sym ); name_len = SymName( sym, NULL, SN_DEMANGLED, NULL, 0 ); if( name_len == 0 ) { name_len = SymName( sym, NULL, SN_SOURCE, NULL, 0 ); demangle_type = SN_SOURCE; } else { demangle_type = SN_DEMANGLED; } new_rtn = ProfCAlloc( sizeof( rtn_info ) + name_len ); SymName( sym, NULL, demangle_type, new_rtn->name, name_len + 1 ); sym_size = DIPHandleSize( HK_SYM ); new_rtn->sh = ProfAlloc( sym_size ); memcpy( new_rtn->sh, sym, sym_size ); rtn_count = sym_file->rtn_count; sym_file->rtn_count++; sym_file->routine = ProfRealloc( sym_file->routine, sym_file->rtn_count * sizeof( pointer ) ); sym_file->routine[rtn_count] = new_rtn; return( WR_CONTINUE ); }
STATIC void procImageBlock( samp_data *data, bool 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 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 calcAggregates( void ) /********************************/ { unsigned index; unsigned index2; int cmp_result; unsigned *sorted_idx; address ***sorted_vect; massgd_sample_addr **massgd_data; // unsigned mbuckets; unsigned curr_mbucket; unsigned curr_midx; thread_data *thd; unsigned buckets; unsigned base; unsigned best; unsigned end; massgd_sample_addr *curr; ClearMassaged( CurrSIOData ); buckets = 0; for( thd = CurrSIOData->samples; thd != NULL; thd = thd->next ) { buckets += RAW_BUCKET_IDX( thd->end_time - thd->start_time ) + 1; } sorted_idx = ProfCAlloc( buckets * sizeof( *sorted_idx ) ); sorted_vect = ProfAlloc( buckets * sizeof(*thd->raw_bucket) ); base = 0; for( thd = CurrSIOData->samples; thd != NULL; thd = thd->next ) { end = RAW_BUCKET_IDX( thd->end_time - thd->start_time ) + 1; for( index = 0; index < end; ++index, ++base ) { sorted_vect[base] = ProfAlloc( MAX_RAW_BUCKET_INDEX * sizeof( **sorted_vect ) ); for( index2 = 0; index2 < MAX_RAW_BUCKET_INDEX; ++index2 ) { sorted_vect[base][index2] = &thd->raw_bucket[index][index2]; } qsort( sorted_vect[base], MAX_RAW_BUCKET_INDEX, sizeof(**sorted_vect), rawSampCmp ); } } /* skip over all the 0:0 samples */ for( index = 0; index < buckets; ++index ) { index2 = 0; for( ;; ) { if( index2 >= MAX_RAW_BUCKET_INDEX ) break; if( !(sorted_vect[index][index2]->mach.segment == 0 && sorted_vect[index][index2]->mach.offset == 0) ) break; ++index2; } sorted_idx[index] = index2; } curr = NULL; curr_mbucket = 0; curr_midx = -1; // mbuckets = 1; massgd_data = ProfAlloc( sizeof( *massgd_data ) ); massgd_data[0] = ProfCAlloc( MAX_MASSGD_BUCKET_SIZE ); for( ;; ) { best = -1U; for( index = 0; index < buckets; ++index ) { index2 = sorted_idx[index]; if( index2 >= MAX_RAW_BUCKET_INDEX ) continue; if( best == -1U ) best = index; cmp_result = AddrCmp( sorted_vect[index][index2], sorted_vect[best][sorted_idx[best]] ); if( cmp_result < 0 ) best = index; } if( best == -1U ) break; if( curr == NULL || AddrCmp( sorted_vect[best][sorted_idx[best]], curr->raw ) != 0 ) { if( ++curr_midx >= MAX_MASSGD_BUCKET_INDEX ) { ++curr_mbucket; massgd_data = ProfRealloc( massgd_data, (curr_mbucket+1) * sizeof(*massgd_data) ); massgd_data[curr_mbucket] = ProfCAlloc( MAX_MASSGD_BUCKET_SIZE ); curr_midx = 0; } curr = &massgd_data[curr_mbucket][curr_midx]; curr->raw = sorted_vect[best][sorted_idx[best]]; } curr->hits++; sorted_idx[best]++; } CurrSIOData->massaged_sample = massgd_data; CurrSIOData->number_massaged = 1 + curr_midx + (curr_mbucket * (unsigned long)MAX_MASSGD_BUCKET_INDEX); CurrSIOData->massaged_mapped = true; for( index = 0; index < buckets; ++index ) { ProfFree( sorted_vect[index] ); } ProfFree( sorted_vect ); ProfFree( sorted_idx ); }
STATIC bool procSampleBlock( clicks_t tick, uint_16 total_len, samp_data *data ) /*************************************************************/ { thread_id thread; unsigned data_index; unsigned index; unsigned index2; unsigned count; unsigned buckets; clicks_t end_tick; ldiv_t div_result; thread_data *thd; thread_data **owner; address *samp; thread = data->sample.thread_id; total_len -= offsetof( samp_block, d.sample ); count = total_len / sizeof( samp_address ); CurrSIOData->total_samples += count; end_tick = tick + count; owner = &CurrSIOData->samples; for( ;; ) { thd = *owner; if( thd == NULL ) { thd = ProfAlloc( sizeof( *thd ) ); *owner = thd; thd->next = NULL; thd->thread = thread; thd->start_time = tick; thd->end_time = end_tick; buckets = RAW_BUCKET_IDX( count ) + 1; thd->raw_bucket = ProfAlloc( buckets * sizeof( *thd->raw_bucket ) ); for( index = 0; index < buckets; ++index ) { thd->raw_bucket[index] = ProfCAlloc( MAX_RAW_BUCKET_SIZE ); } break; } if( thd->thread == thread ) break; owner = &thd->next; } if( end_tick > thd->end_time ) { index = RAW_BUCKET_IDX( thd->end_time - thd->start_time ) + 1; buckets = RAW_BUCKET_IDX( end_tick - thd->start_time ) + 1; if( buckets > index ) { thd->raw_bucket = ProfRealloc( thd->raw_bucket, buckets * sizeof( *thd->raw_bucket ) ); for( ; index < buckets; ++index ) { thd->raw_bucket[index] = ProfCAlloc( MAX_RAW_BUCKET_SIZE ); } } thd->end_time = end_tick; } div_result = ldiv( tick - thd->start_time, MAX_RAW_BUCKET_INDEX ); index = div_result.quot; index2 = div_result.rem; data_index = 0; while( count > 0 ) { samp = &thd->raw_bucket[index][index2]; samp->mach.segment = data->sample.sample[data_index].segment; samp->mach.offset = data->sample.sample[data_index].offset; if( ++index2 >= MAX_RAW_BUCKET_INDEX ) { index2 = 0; ++index; } ++data_index; --count; } return( true ); }
wp_asmfile *WPAsmOpen( sio_data * curr_sio, int src_row, bool quiet ) /*******************************************************************/ { wp_asmfile * wpasm_file; cue_handle * ch; cue_handle * ch2; mod_info * curr_mod; file_info * curr_file; massgd_sample_addr * samp_data; wp_asmline * asm_line; mod_handle mh; file_handle fh; address addr; cue_fileid fid; search_result cue_find; int rows; int asm_group; int asm_row; int file_index; int addr_cmp; clicks_t addr_tick_index; quiet=quiet; ch = alloca( DIPHandleSize( HK_CUE ) ); ch2 = alloca( DIPHandleSize( HK_CUE ) ); curr_file = curr_sio->curr_file; curr_mod = curr_sio->curr_mod; if( curr_file->fid == 0 || LineCue( curr_mod->mh, curr_sio->curr_file->fid, src_row, 0, ch2 ) == SR_NONE ) { ch2 = NULL; } fh = ExeOpen( curr_sio->curr_image->name ); if( fh == -1 ) { ErrorMsg( LIT( Exe_Not_Found ), curr_sio->curr_image->name ); return( NULL ); } wpasm_file = ProfCAlloc( sizeof(wp_asmfile) ); curr_sio->asm_file = wpasm_file; wpasm_file->asm_buff = ProfAlloc( MAX_ASM_BUFF_LEN ); wpasm_file->asm_buff_len = MAX_ASM_BUFF_LEN; SetNumBytes( 0 ); SetExeFile( fh, false ); wpasm_file->fh = fh; addr = ModAddr( curr_mod->mh ); SetExeOffset( addr ); wpasm_file->max_time = 0; addr_tick_index = curr_mod->first_tick_index - 1; samp_data = WPGetMassgdSampData( curr_sio, addr_tick_index++ ); wpasm_file->asm_data = ProfAlloc( sizeof(wp_asm_groups) ); wpasm_file->asm_data[0].asm_lines = ProfAlloc( MAX_ASM_LINE_SIZE ); wpasm_file->asm_groups = 0; rows = 0; for( ;; ) { mh = curr_mod->mh; if( EndOfSegment() || AddrMod( addr, &mh ) == SR_NONE || mh != curr_mod->mh ) break; cue_find = (AddrCue( curr_mod->mh, addr, ch ) == SR_EXACT); if( ch2 != NULL && CueCmp( ch, ch2 ) == 0 ) { wpasm_file->entry_line = rows; ch2 = NULL; } asm_line = WPGetAsmLoc( wpasm_file, rows, &asm_group, &asm_row ); if( cue_find ) { asm_line->source_line = true; asm_line->u.src.line = CueLine( ch ); asm_line->u.src.src_file = NULL; if( !curr_file->unknown_file ) { fid = CueFileId( ch ); file_index = 0; while( file_index < curr_mod->file_count ) { curr_file = curr_mod->mod_file[file_index]; if( curr_file->fid == fid ) { asm_line->u.src.src_file = FOpenSource( curr_file->name, mh, fid ); break; } file_index++; } } rows++; asm_line = WPGetAsmLoc( wpasm_file, rows, &asm_group, &asm_row ); } asm_line = &wpasm_file->asm_data[asm_group].asm_lines[asm_row]; asm_line->source_line = false; asm_line->u.asm_line.addr = addr; asm_line->u.asm_line.tick_count = 0; for( ;; ) { if( samp_data == NULL ) break; addr_cmp = AddrCmp( &addr, samp_data->raw ); if( addr_cmp < 0 ) break; if( addr_cmp == 0 ) { asm_line->u.asm_line.tick_count = samp_data->hits; if( asm_line->u.asm_line.tick_count > wpasm_file->max_time ) { wpasm_file->max_time = asm_line->u.asm_line.tick_count; } } samp_data = WPGetMassgdSampData( curr_sio, addr_tick_index++ ); } rows++; CodeAdvance( &addr ); } WPGetAsmLoc( wpasm_file, rows, &asm_group, &asm_row ); wpasm_file->asm_data[asm_group].asm_lines = ProfRealloc( wpasm_file->asm_data[asm_group].asm_lines, sizeof(wp_asmline)*(asm_row+1) ); wpasm_file->asm_rows = rows; return( wpasm_file ); }