os/graphics/windowing/windowserver/econs/D_EXC.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <e32std.h>
sl@0
    17
#include <e32svr.h>
sl@0
    18
#include <f32file.h>
sl@0
    19
sl@0
    20
_LIT(KBlankLine, "\r");
sl@0
    21
sl@0
    22
class TDll
sl@0
    23
	{
sl@0
    24
public:
sl@0
    25
	TDll(const TDesC& aName, TUint8* aBase, TInt aSize)
sl@0
    26
		: iName(aName), iBase((TUint)aBase), iSize((TUint)aSize)
sl@0
    27
		{}
sl@0
    28
sl@0
    29
	TFullName iName;
sl@0
    30
	TUint iBase;
sl@0
    31
	TUint iSize;
sl@0
    32
	};
sl@0
    33
sl@0
    34
class TDllList
sl@0
    35
	{
sl@0
    36
public:
sl@0
    37
	static void FindDlls();
sl@0
    38
	static void ResetAndDestroy();
sl@0
    39
	static void ListDlls(TFileText& aTextFile);
sl@0
    40
	static TInt Match(TUint anAddr, const TDll*& aDll);
sl@0
    41
sl@0
    42
private:
sl@0
    43
	static RPointerArray<TDll> iList;
sl@0
    44
	};
sl@0
    45
sl@0
    46
RPointerArray<TDll> TDllList::iList(10);
sl@0
    47
sl@0
    48
void TDllList::ResetAndDestroy()
sl@0
    49
	{
sl@0
    50
	iList.ResetAndDestroy();
sl@0
    51
	}
sl@0
    52
sl@0
    53
void TDllList::ListDlls(TFileText& aTextFile)
sl@0
    54
	{
sl@0
    55
	TBuf<0x100> line;
sl@0
    56
sl@0
    57
	_LIT(KDllFormat, "%08x-%08x %S\r");
sl@0
    58
sl@0
    59
	aTextFile.Write(KBlankLine);
sl@0
    60
	for (TInt i=0; i<iList.Count(); i++)
sl@0
    61
		{
sl@0
    62
		const TDll& dll=*(iList[i]);
sl@0
    63
		line.Format(KDllFormat, dll.iBase, dll.iBase+dll.iSize-1, &dll.iName);
sl@0
    64
		aTextFile.Write(line);
sl@0
    65
		}
sl@0
    66
	}
sl@0
    67
sl@0
    68
TInt TDllList::Match(TUint anAddr, const TDll*& aDll)
sl@0
    69
	{
sl@0
    70
	for (TInt i=0; i<iList.Count(); i++)
sl@0
    71
		{
sl@0
    72
		aDll=iList[i];
sl@0
    73
		if (aDll->iBase<=anAddr && aDll->iBase+aDll->iSize > anAddr)
sl@0
    74
			return KErrNone;
sl@0
    75
		}
sl@0
    76
	return KErrNotFound;
sl@0
    77
	}
sl@0
    78
sl@0
    79
sl@0
    80
void TDllList::FindDlls()
sl@0
    81
//
sl@0
    82
// For each library known to the system, look for a chunk of the same name
sl@0
    83
//
sl@0
    84
	{
sl@0
    85
	TFindLibrary findLib;
sl@0
    86
	TFullName libName;
sl@0
    87
sl@0
    88
	while (findLib.Next(libName)==KErrNone)
sl@0
    89
		{
sl@0
    90
		TFindChunk findChunk(libName);
sl@0
    91
		TFullName chunkName;
sl@0
    92
		if (findChunk.Next(chunkName)!=KErrNone)
sl@0
    93
			continue;
sl@0
    94
		RChunk chunk;
sl@0
    95
		if (chunk.Open(findChunk)!=KErrNone)
sl@0
    96
			continue;
sl@0
    97
		TUint8* base=chunk.Base();
sl@0
    98
		TInt size=chunk.Size();
sl@0
    99
		chunk.Close();
sl@0
   100
		TDll* dllptr=new TDll(libName,base,size);
sl@0
   101
		if (dllptr!=0)
sl@0
   102
			iList.Append(dllptr);
sl@0
   103
		}
sl@0
   104
	}
sl@0
   105
sl@0
   106
_LIT(KFormatStackInfo,"Stack %08x-%08x (? %d?), sp=%08x\r");
sl@0
   107
sl@0
   108
HBufC8* GrabStack(const TDesC& /*aLine1*/, TThreadId aId, TUint aSp, TInt& aStackBase, TInt& aStackSize)
sl@0
   109
	{
sl@0
   110
	TInt err=KErrNone;
sl@0
   111
#if 0
sl@0
   112
	// Defect in E32 162 which means that RThread::GetRamSizes will always reset the machine!
sl@0
   113
	RThread thread;
sl@0
   114
	if (thread.Open(aId)!=KErrNone)
sl@0
   115
		return 0;
sl@0
   116
sl@0
   117
	TInt heapsize=0;
sl@0
   118
	err=thread.GetRamSizes(heapsize,aStackSize);
sl@0
   119
	thread.Close();
sl@0
   120
	
sl@0
   121
	if (err!=KErrNone)
sl@0
   122
		return 0;
sl@0
   123
sl@0
   124
	// Sanity check
sl@0
   125
	aStackBase = aSp & ~((1024*1024)-1);
sl@0
   126
	if (aStackSize<0 || aStackSize>=1024*1024 || aSp>(TUint)(aStackBase+aStackSize))
sl@0
   127
		{
sl@0
   128
		aStackBase=0;	// indicates a daft stack pointer
sl@0
   129
		return 0;
sl@0
   130
		}
sl@0
   131
#else
sl@0
   132
	// Sanity check & guess at stack size
sl@0
   133
	aStackBase = aSp & ~((1024*1024)-1);
sl@0
   134
	aStackSize = (aSp - aStackBase + 4096) & ~4095;		// round up to a multiple of the page size
sl@0
   135
	if (aStackBase+aStackSize-aSp < 200)
sl@0
   136
			aStackSize += 4096;		// seems too small - risk another page!
sl@0
   137
	if (aStackSize<0 || aStackSize>=1024*1024)
sl@0
   138
		{
sl@0
   139
		aStackBase=0;	// indicates a daft stack pointer
sl@0
   140
		return 0;
sl@0
   141
		}
sl@0
   142
	aStackSize-=4;	// a clue to the wise that this is a guess...
sl@0
   143
#endif
sl@0
   144
sl@0
   145
	// Ask the user if we want to risk grabbing the stack...
sl@0
   146
sl@0
   147
	TBuf<0x100> line2;
sl@0
   148
	line2.Format(KFormatStackInfo, aStackBase, aStackBase+aStackSize-1, aStackSize, aSp);
sl@0
   149
    
sl@0
   150
	// OK - let stack grabbing commence
sl@0
   151
	HBufC8* stackbuf = HBufC8::New(aStackSize);
sl@0
   152
	if (stackbuf==0)
sl@0
   153
		return 0;
sl@0
   154
sl@0
   155
	TPtr8 stackdes(stackbuf->Des());
sl@0
   156
	err=RDebug::ReadMemory(aId,aStackBase,stackdes,aStackSize);
sl@0
   157
	if (err!=KErrNone)
sl@0
   158
		{
sl@0
   159
		delete stackbuf;
sl@0
   160
		stackbuf=0;
sl@0
   161
		}
sl@0
   162
	return stackbuf;
sl@0
   163
	}
sl@0
   164
sl@0
   165
GLDEF_C TInt E32Main()
sl@0
   166
//
sl@0
   167
// Reporting more detail of KERN-EXEC 3 errors
sl@0
   168
//
sl@0
   169
// Warning: This code uses fragile assumptions which may not be true
sl@0
   170
// in future releases of EPOC
sl@0
   171
//
sl@0
   172
// 1) EXEs are located at 0x20000000
sl@0
   173
// 2) the map file for an EXE puts the start of text at 400010
sl@0
   174
// 3) the map file for a DLL puts the start of text at 10000010
sl@0
   175
// 4) the EPOC ROM lives at address 0x50000000
sl@0
   176
// 5) EPOC stacks start at a megabyte boundary
sl@0
   177
//
sl@0
   178
	{
sl@0
   179
	TBuf<0x100> line1;
sl@0
   180
	TBuf<0x100> line2;
sl@0
   181
	SDebugInfo info;
sl@0
   182
	struct SRegisterInfo reginfo;
sl@0
   183
	TUint pc = 0;
sl@0
   184
	TUint regs[16];
sl@0
   185
	const TDll* faultDll = NULL;
sl@0
   186
sl@0
   187
	_LIT(KInfo1, "D_EXC started");
sl@0
   188
	User::InfoPrint(KInfo1);
sl@0
   189
sl@0
   190
	TRequestStatus stat(0);
sl@0
   191
//	FOREVER
sl@0
   192
	for (TInt rep=0; rep<2; rep++)	// die after two exceptions
sl@0
   193
		{
sl@0
   194
		TInt err = KErrNone;
sl@0
   195
		// wait for any thread to panic...
sl@0
   196
sl@0
   197
sl@0
   198
		err=RDebug::GetException(info,stat);
sl@0
   199
		if (err!=KErrNone)
sl@0
   200
			{
sl@0
   201
			_LIT(KInfo2, "RDebug failure");
sl@0
   202
			User::Panic(KInfo2, err);
sl@0
   203
			}
sl@0
   204
sl@0
   205
		User::WaitForRequest(stat);
sl@0
   206
sl@0
   207
		_LIT(KFormatPanic, "%S panic %S %d\r");
sl@0
   208
		_LIT(KFormatException, "%S exception type %d\r");
sl@0
   209
		_LIT(KFormatBreakpoint, "%S has breakpoint %d\r");
sl@0
   210
sl@0
   211
sl@0
   212
		switch (info.iDebugType)
sl@0
   213
			{
sl@0
   214
		case EPanic:
sl@0
   215
			line1.Format(KFormatPanic,&info.iName,&info.iCategory, info.iPanicType);
sl@0
   216
			break;
sl@0
   217
		case EException:
sl@0
   218
			line1.Format(KFormatException,&info.iName,info.iPanicType);
sl@0
   219
			break;
sl@0
   220
		case EBreakPoint:
sl@0
   221
			line1.Format(KFormatBreakpoint,&info.iName,info.iPanicType);
sl@0
   222
			break;
sl@0
   223
		default:
sl@0
   224
			continue;
sl@0
   225
			}
sl@0
   226
sl@0
   227
		// assume that it's KERN-EXEC 3 and try to use the 
sl@0
   228
		// full RDebug support to locate the faulting instruction
sl@0
   229
sl@0
   230
		HBufC8* stack = NULL;
sl@0
   231
		TInt stackbase=0;
sl@0
   232
		TInt stacksize=0;
sl@0
   233
sl@0
   234
		const TInt KStackPointerReg = 13;
sl@0
   235
sl@0
   236
		pc = 0x00000001;	// illegal value
sl@0
   237
sl@0
   238
sl@0
   239
		err = RDebug::Open();
sl@0
   240
		if (err==KErrNone)
sl@0
   241
			{
sl@0
   242
			err = RDebug::RegisterInfo(reginfo);
sl@0
   243
			if (!err)
sl@0
   244
				{
sl@0
   245
				RDebug::GetRegister(info.iId,reginfo.iNumberOfPcRegister, pc);
sl@0
   246
				for (int i=0; i<16; i++)
sl@0
   247
				    {
sl@0
   248
					RDebug::GetRegister(info.iId, i, regs[i]);
sl@0
   249
				    }
sl@0
   250
				}
sl@0
   251
sl@0
   252
			TDllList::FindDlls();
sl@0
   253
sl@0
   254
			stack=GrabStack(line1, info.iId, regs[KStackPointerReg], stackbase, stacksize);
sl@0
   255
sl@0
   256
			RDebug::Close();
sl@0
   257
			}
sl@0
   258
sl@0
   259
sl@0
   260
		_LIT(KFormatEXE,	"pc=%08x, .map equivalent %08x\r");
sl@0
   261
		_LIT(KFormatROM,	"pc=%08x, in ROM\r");
sl@0
   262
		_LIT(KFormatDll,	"pc=%08x, in DLL %S, .map equivalent %08x\r");
sl@0
   263
		_LIT(KFormatOther,	"pc=%08x, iCodeAddr=%08x\r");
sl@0
   264
		_LIT(KFormatError,	"(Unable to determine pc)\r");
sl@0
   265
sl@0
   266
sl@0
   267
		if ((pc&3) == 0)
sl@0
   268
			{
sl@0
   269
			if (pc >= 0x20000000 && pc < 0x30000000)
sl@0
   270
			    {
sl@0
   271
				line2.Format(KFormatEXE, pc, pc-0x20000000+0x400010);
sl@0
   272
			    }
sl@0
   273
			else if (pc >= 0x50000000 && pc < 0x60000000)
sl@0
   274
				{
sl@0
   275
                line2.Format(KFormatROM, pc);
sl@0
   276
				}
sl@0
   277
			else if (TDllList::Match(pc, faultDll)==KErrNone)
sl@0
   278
			    {
sl@0
   279
				line2.Format(KFormatDll, pc, &faultDll->iName, pc-(faultDll->iBase)+0x10000010);
sl@0
   280
			    }
sl@0
   281
			else
sl@0
   282
			    {
sl@0
   283
				line2.Format(KFormatOther, pc, info.iCodeAddr);
sl@0
   284
			    }
sl@0
   285
			}
sl@0
   286
		else
sl@0
   287
		    {
sl@0
   288
			line2.Copy(KFormatError);
sl@0
   289
		    }
sl@0
   290
		
sl@0
   291
		RFs fs;
sl@0
   292
		err = fs.Connect();
sl@0
   293
		if (err!=KErrNone)
sl@0
   294
		    {
sl@0
   295
			break;
sl@0
   296
		    }
sl@0
   297
sl@0
   298
		_LIT(KFormatFilename,"d:\\d_exc_%d.txt");
sl@0
   299
		_LIT(KFormatStackname,"d:\\d_exc_%d.stk");
sl@0
   300
sl@0
   301
		// Report the basic information about registers, DLLs etc
sl@0
   302
sl@0
   303
		TFileName name;
sl@0
   304
		name.Format(KFormatFilename, *(TUint*)&info.iId);
sl@0
   305
sl@0
   306
		RFile file;
sl@0
   307
		err=file.Replace(fs, name, EFileWrite+EFileShareAny+EFileStreamText);
sl@0
   308
		if (err!=KErrNone)
sl@0
   309
		    {
sl@0
   310
            fs.Close();
sl@0
   311
            break;
sl@0
   312
		    }
sl@0
   313
sl@0
   314
		TFileText textfile;
sl@0
   315
		textfile.Set(file);
sl@0
   316
sl@0
   317
		textfile.Write(line1);
sl@0
   318
		textfile.Write(line2);
sl@0
   319
		textfile.Write(KBlankLine);
sl@0
   320
sl@0
   321
		_LIT(KFormatRegisters,"r%02d=%08x %08x %08x %08x\r");
sl@0
   322
sl@0
   323
		line2.Format(KFormatRegisters, 0, regs[0], regs[1], regs[2], regs[3]);
sl@0
   324
		textfile.Write(line2);
sl@0
   325
		line2.Format(KFormatRegisters, 4, regs[4], regs[5], regs[6], regs[7]);
sl@0
   326
		textfile.Write(line2);
sl@0
   327
		line2.Format(KFormatRegisters, 8, regs[8], regs[9], regs[10], regs[11]);
sl@0
   328
		textfile.Write(line2);
sl@0
   329
		line2.Format(KFormatRegisters, 12,regs[12], regs[13], regs[14], regs[15]);
sl@0
   330
		textfile.Write(line2);
sl@0
   331
sl@0
   332
		if (stackbase!=0)
sl@0
   333
			{
sl@0
   334
			line2.Format(KFormatStackInfo, stackbase, stackbase+stacksize-1, stacksize, regs[KStackPointerReg]);
sl@0
   335
			textfile.Write(line2);
sl@0
   336
			}
sl@0
   337
sl@0
   338
		TDllList::ListDlls(textfile);
sl@0
   339
		TDllList::ResetAndDestroy();
sl@0
   340
sl@0
   341
		file.Close();
sl@0
   342
sl@0
   343
		// Dump the stack as binary data
sl@0
   344
sl@0
   345
		if (stack)
sl@0
   346
			{
sl@0
   347
			name.Format(KFormatStackname, *(TUint*)&info.iId);
sl@0
   348
			err=file.Replace(fs, name, EFileWrite+EFileShareAny+EFileStreamText);
sl@0
   349
			if (err==KErrNone)
sl@0
   350
				{
sl@0
   351
				file.Write(*stack);
sl@0
   352
				file.Close();
sl@0
   353
				}
sl@0
   354
			}
sl@0
   355
		delete stack;
sl@0
   356
sl@0
   357
		fs.Close();
sl@0
   358
		}
sl@0
   359
	return stat.Int();
sl@0
   360
	}
sl@0
   361
sl@0
   362