1999-10-08 04:11:39 +00:00
|
|
|
/*
|
|
|
|
gc_fragments.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gc_fragments.h"
|
|
|
|
#include "sym_file.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "MetroNubUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct CodeLocation {
|
|
|
|
CodeLocation* mNext;
|
|
|
|
char* mCodeAddr;
|
|
|
|
UInt32 mFileOffset;
|
1999-11-15 03:26:03 +00:00
|
|
|
char mSymbolName[256];
|
1999-10-08 04:11:39 +00:00
|
|
|
char mFileName[256];
|
|
|
|
|
|
|
|
CodeLocation() : mNext(NULL), mCodeAddr(NULL), mFileOffset(0)
|
|
|
|
{
|
|
|
|
mFileName[0] = '\0';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const kCodeLocationCacheSize = 256;
|
|
|
|
|
|
|
|
struct CodeLocationCache {
|
|
|
|
CodeLocation mEntries[kCodeLocationCacheSize];
|
|
|
|
CodeLocation* mLocations;
|
|
|
|
CodeLocation** mLastLink;
|
|
|
|
|
|
|
|
CodeLocationCache();
|
|
|
|
|
|
|
|
CodeLocation* findLocation(char* codeAddr);
|
1999-11-15 03:26:03 +00:00
|
|
|
void saveLocation(char* codeAddr, char symbolName[256], char fileName[256], UInt32 fileOffset);
|
1999-10-08 04:11:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CodeLocationCache::CodeLocationCache()
|
|
|
|
: mLocations(NULL), mLastLink(&mLocations)
|
|
|
|
{
|
|
|
|
// link all of the locations together in a list.
|
|
|
|
int offset = kCodeLocationCacheSize;
|
|
|
|
while (offset > 0) {
|
|
|
|
CodeLocation* location = &mEntries[--offset];
|
|
|
|
location->mNext = mLocations;
|
|
|
|
mLocations = location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeLocation* CodeLocationCache::findLocation(char* codeAddr)
|
|
|
|
{
|
|
|
|
CodeLocation** link = &mLocations;
|
|
|
|
CodeLocation* location = *link;
|
|
|
|
while (location != NULL && location->mCodeAddr != NULL) {
|
|
|
|
if (location->mCodeAddr == codeAddr) {
|
|
|
|
// move it to the head of the list.
|
|
|
|
if (location != mLocations) {
|
|
|
|
*link = location->mNext;
|
|
|
|
location->mNext = mLocations;
|
|
|
|
mLocations = location;
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
link = &location->mNext;
|
|
|
|
location = *link;
|
|
|
|
// maintain pointer to the last element in list for fast insertion.
|
|
|
|
if (location != NULL)
|
|
|
|
mLastLink = link;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-11-15 03:26:03 +00:00
|
|
|
void CodeLocationCache::saveLocation(char* codeAddr, char symbolName[256], char fileName[256], UInt32 fileOffset)
|
1999-10-08 04:11:39 +00:00
|
|
|
{
|
|
|
|
CodeLocation** link = mLastLink;
|
|
|
|
CodeLocation* location = *link;
|
|
|
|
mLastLink = &mLocations;
|
|
|
|
|
|
|
|
// move it to the head of the list.
|
|
|
|
if (location != mLocations) {
|
|
|
|
*link = location->mNext;
|
|
|
|
location->mNext = mLocations;
|
|
|
|
mLocations = location;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the specified location.
|
|
|
|
location->mCodeAddr = codeAddr;
|
|
|
|
location->mFileOffset = fileOffset;
|
1999-11-15 03:26:03 +00:00
|
|
|
::strcpy(location->mSymbolName, symbolName);
|
1999-10-08 04:11:39 +00:00
|
|
|
::strcpy(location->mFileName, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CodeFragment {
|
|
|
|
CodeFragment* mNext;
|
|
|
|
char* mDataStart;
|
|
|
|
char* mDataEnd;
|
|
|
|
char* mCodeStart;
|
|
|
|
char* mCodeEnd;
|
|
|
|
FSSpec mFragmentSpec;
|
|
|
|
CodeLocationCache mLocations;
|
|
|
|
sym_file* mSymbols;
|
|
|
|
|
|
|
|
CodeFragment(char* dataStart, char* dataEnd,
|
|
|
|
char* codeStart, char* codeEnd,
|
|
|
|
const FSSpec* fragmentSpec);
|
|
|
|
|
2000-04-13 06:40:16 +00:00
|
|
|
~CodeFragment();
|
|
|
|
|
|
|
|
void* operator new(size_t n) { return ::GC_malloc(n); }
|
|
|
|
void operator delete(void* ptr) { return ::GC_free(ptr); }
|
1999-10-08 04:11:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CodeFragment::CodeFragment(char* dataStart, char* dataEnd,
|
|
|
|
char* codeStart, char* codeEnd,
|
|
|
|
const FSSpec* fragmentSpec)
|
|
|
|
: mDataStart(dataStart), mDataEnd(dataEnd),
|
|
|
|
mCodeStart(codeStart), mCodeEnd(codeEnd),
|
|
|
|
mFragmentSpec(*fragmentSpec), mSymbols(NULL), mNext(NULL)
|
|
|
|
{
|
|
|
|
// need to eagerly open symbols file eagerly, otherwise we're in the middle of a GC!
|
|
|
|
FSSpec symSpec = mFragmentSpec;
|
|
|
|
UInt8 len = symSpec.name[0];
|
|
|
|
symSpec.name[++len] = '.';
|
|
|
|
symSpec.name[++len] = 'x';
|
|
|
|
symSpec.name[++len] = 'S';
|
|
|
|
symSpec.name[++len] = 'Y';
|
|
|
|
symSpec.name[++len] = 'M';
|
|
|
|
symSpec.name[0] = len;
|
|
|
|
|
|
|
|
mSymbols = open_sym_file(&symSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeFragment::~CodeFragment()
|
|
|
|
{
|
|
|
|
if (mSymbols) {
|
|
|
|
close_sym_file(mSymbols);
|
|
|
|
mSymbols = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CodeFragment* theFragments = NULL;
|
|
|
|
|
|
|
|
static CodeFragment** find_fragment(char* codeAddr)
|
|
|
|
{
|
|
|
|
CodeFragment** link = &theFragments;
|
|
|
|
CodeFragment* fragment = *link;
|
|
|
|
while (fragment != NULL) {
|
|
|
|
if (codeAddr >= fragment->mCodeStart && codeAddr < fragment->mCodeEnd)
|
|
|
|
return link;
|
|
|
|
link = &fragment->mNext;
|
|
|
|
fragment = *link;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GC_register_fragment(char* dataStart, char* dataEnd,
|
|
|
|
char* codeStart, char* codeEnd,
|
|
|
|
const FSSpec* fragmentSpec)
|
|
|
|
{
|
|
|
|
// register the roots.
|
|
|
|
GC_add_roots(dataStart, dataEnd);
|
|
|
|
|
|
|
|
// create an entry for this fragment.
|
|
|
|
CodeFragment* fragment = new CodeFragment(dataStart, dataEnd,
|
|
|
|
codeStart, codeEnd,
|
|
|
|
fragmentSpec);
|
|
|
|
if (fragment != NULL) {
|
|
|
|
fragment->mNext = theFragments;
|
|
|
|
theFragments = fragment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GC_unregister_fragment(char* dataStart, char* dataEnd,
|
|
|
|
char* codeStart, char* codeEnd)
|
|
|
|
{
|
|
|
|
// try not to crash when running under the MW debugger.
|
|
|
|
if (!AmIBeingMWDebugged()) {
|
|
|
|
CodeFragment** link = find_fragment(codeStart);
|
|
|
|
if (link != NULL) {
|
|
|
|
CodeFragment* fragment = *link;
|
|
|
|
*link = fragment->mNext;
|
|
|
|
delete fragment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the roots.
|
|
|
|
GC_remove_roots(dataStart, dataEnd);
|
|
|
|
}
|
|
|
|
|
1999-11-15 03:26:03 +00:00
|
|
|
int GC_address_to_source(char* codeAddr, char symbolName[256], char fileName[256], UInt32* fileOffset)
|
1999-10-08 04:11:39 +00:00
|
|
|
{
|
|
|
|
CodeFragment** link = find_fragment(codeAddr);
|
|
|
|
if (link != NULL) {
|
|
|
|
CodeFragment* fragment = *link;
|
|
|
|
// always move this fragment to the head of the list, to speed up searches.
|
|
|
|
if (theFragments != fragment) {
|
|
|
|
*link = fragment->mNext;
|
|
|
|
fragment->mNext = theFragments;
|
|
|
|
theFragments = fragment;
|
|
|
|
}
|
|
|
|
// see if this is a cached location.
|
|
|
|
CodeLocation* location = fragment->mLocations.findLocation(codeAddr);
|
|
|
|
if (location != NULL) {
|
1999-11-15 03:26:03 +00:00
|
|
|
::strcpy(symbolName, location->mSymbolName);
|
1999-10-08 04:11:39 +00:00
|
|
|
::strcpy(fileName, location->mFileName);
|
|
|
|
*fileOffset = location->mFileOffset;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sym_file* symbols = fragment->mSymbols;
|
|
|
|
if (symbols != NULL) {
|
1999-11-15 03:26:03 +00:00
|
|
|
if (get_source(symbols, UInt32(codeAddr - fragment->mCodeStart), symbolName, fileName, fileOffset)) {
|
1999-10-08 04:11:39 +00:00
|
|
|
// save this location in the per-fragment cache.
|
1999-11-15 03:26:03 +00:00
|
|
|
fragment->mLocations.saveLocation(codeAddr, symbolName, fileName, *fileOffset);
|
1999-10-08 04:11:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|