1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
6
#include "arch_GOARCH.h"
10
// Code related to defer, panic and recover.
12
uint32 runtime·panicking;
20
// Allocate a Defer, usually as part of the larger frame of deferred functions.
21
// Each defer must be released with both popdefer and freedefer.
30
total = sizeof(*d) + ROUND(siz, sizeof(uintptr)) - sizeof(d->args);
31
if(c == nil || total > DeferChunkSize - c->off) {
32
if(total > DeferChunkSize / 2) {
33
// Not worth putting in any chunk.
34
// Allocate a separate block.
35
d = runtime·malloc(total);
44
// Cannot fit in current chunk.
45
// Switch to next chunk, allocating if necessary.
48
c = runtime·malloc(DeferChunkSize);
55
d = (Defer*)((byte*)c + c->off);
65
// Pop the current defer from the defer stack.
66
// Its contents are still valid until the goroutine begins executing again.
67
// In particular it is safe to call reflect.call(d->fn, d->argp, d->siz) after
78
runtime·throw("runtime: popdefer nil");
81
// Nothing else to do.
84
total = sizeof(*d) + ROUND(d->siz, sizeof(uintptr)) - sizeof(d->args);
86
if(c == nil || (byte*)d+total != (byte*)c+c->off)
87
runtime·throw("runtime: popdefer phase error");
89
if(c->off == sizeof(*c)) {
90
// Chunk now empty, so pop from stack.
91
// Save in dchunknext both to help with pingponging between frames
92
// and to make sure d is still valid on return.
93
if(g->dchunknext != nil)
94
runtime·free(g->dchunknext);
100
// Free the given defer.
101
// For defers in the per-goroutine chunk this just clears the saved arguments.
102
// For large defers allocated on the heap, this frees them.
103
// The defer cannot be used after this call.
111
runtime·memclr((byte*)d->args, d->siz);
115
// Create a new deferred function fn with siz bytes of arguments.
116
// The compiler turns a defer statement into a call to this.
117
// Cannot split the stack because it assumes that the arguments
118
// are available sequentially after &fn; they would not be
119
// copied if a stack split occurred. It's OK for this to call
120
// functions that split the stack.
123
runtime·deferproc(int32 siz, FuncVal *fn, ...)
129
d->pc = runtime·getcallerpc(&siz);
131
d->argp = (byte*)(&fn+2); // skip caller's saved link register
133
d->argp = (byte*)(&fn+1);
134
runtime·memmove(d->args, d->argp, d->siz);
136
// deferproc returns 0 normally.
137
// a deferred func that stops a panic
138
// makes the deferproc return 1.
139
// the code the compiler generates always
140
// checks the return value and jumps to the
141
// end of the function if deferproc returns != 0.
145
// Run a deferred function if there is one.
146
// The compiler inserts a call to this at the end of any
147
// function which calls defer.
148
// If there is a deferred function, this will call runtime·jmpdefer,
149
// which will jump to the deferred function such that it appears
150
// to have been called by the caller of deferreturn at the point
151
// just before deferreturn was called. The effect is that deferreturn
152
// is called again and again until there are no more deferred functions.
153
// Cannot split the stack because we reuse the caller's frame to
154
// call the deferred function.
157
runtime·deferreturn(uintptr arg0)
169
runtime·memmove(argp, d->args, d->siz);
173
runtime·jmpdefer(fn, argp);
176
// Run all deferred functions for the current goroutine.
182
while((d = g->defer) != nil) {
184
reflect·call(d->fn, (byte*)d->args, d->siz);
189
// Print all currently active panics. Used when crashing.
191
printpanics(Panic *p)
194
printpanics(p->link);
195
runtime·printf("\t");
197
runtime·printf("panic: ");
198
runtime·printany(p->arg);
200
runtime·printf(" [recovered]");
201
runtime·printf("\n");
204
static void recovery(G*);
206
// The implementation of the predeclared function panic.
208
runtime·panic(Eface e)
214
p = runtime·mal(sizeof *p);
217
p->stackbase = (byte*)g->stackbase;
224
// take defer off list in case of recursive panic
226
g->ispanic = true; // rock for newstack, where reflect.call ends up
229
reflect·call(d->fn, (byte*)d->args, d->siz);
233
if(g->panic == nil) // must be done with signal
236
// Pass information about recovering frame to recovery.
237
g->sigcode0 = (uintptr)argp;
238
g->sigcode1 = (uintptr)pc;
239
runtime·mcall(recovery);
240
runtime·throw("recovery failed"); // mcall should not return
244
// ran out of deferred calls - old-school panic now
245
runtime·startpanic();
246
printpanics(g->panic);
250
// Unwind the stack after a deferred function calls recover
251
// after a panic. Then arrange to continue running as though
252
// the caller of the deferred function returned normally.
259
// Info about defer passed in G struct.
260
argp = (void*)gp->sigcode0;
261
pc = (void*)gp->sigcode1;
263
// Unwind to the stack frame with d's arguments in it.
264
runtime·unwindstack(gp, argp);
266
// Make the deferproc for this d return again,
267
// this time returning 1. The calling function will
268
// jump to the standard return epilogue.
269
// The -2*sizeof(uintptr) makes up for the
270
// two extra words that are on the stack at
271
// each call to deferproc.
272
// (The pc we're returning to does pop pop
273
// before it tests the return value.)
274
// On the arm there are 2 saved LRs mixed in too.
276
gp->sched.sp = (uintptr)argp - 4*sizeof(uintptr);
278
gp->sched.sp = (uintptr)argp - 2*sizeof(uintptr);
280
runtime·gogo(&gp->sched, 1);
283
// Free stack frames until we hit the last one
284
// or until we find the one that contains the sp.
286
runtime·unwindstack(G *gp, byte *sp)
291
// Must be called from a different goroutine, usually m->g0.
293
runtime·throw("unwindstack on self");
295
while((top = (Stktop*)gp->stackbase) != nil && top->stackbase != nil) {
296
stk = (byte*)gp->stackguard - StackGuard;
297
if(stk <= sp && sp < (byte*)gp->stackbase)
299
gp->stackbase = (uintptr)top->stackbase;
300
gp->stackguard = (uintptr)top->stackguard;
302
runtime·stackfree(stk, top->free);
305
if(sp != nil && (sp < (byte*)gp->stackguard - StackGuard || (byte*)gp->stackbase < sp)) {
306
runtime·printf("recover: %p not in [%p, %p]\n", sp, gp->stackguard - StackGuard, gp->stackbase);
307
runtime·throw("bad unwindstack");
311
// The implementation of the predeclared function recover.
312
// Cannot split the stack because it needs to reliably
313
// find the stack segment of its caller.
316
runtime·recover(byte *argp, Eface ret)
318
Stktop *top, *oldtop;
321
// Must be a panic going on.
322
if((p = g->panic) == nil || p->recovered)
325
// Frame must be at the top of the stack segment,
326
// because each deferred call starts a new stack
327
// segment as a side effect of using reflect.call.
328
// (There has to be some way to remember the
329
// variable argument frame size, and the segment
330
// code already takes care of that for us, so we
333
// As usual closures complicate things: the fp that
334
// the closure implementation function claims to have
335
// is where the explicit arguments start, after the
336
// implicit pointer arguments and PC slot.
337
// If we're on the first new segment for a closure,
338
// then fp == top - top->args is correct, but if
339
// the closure has its own big argument frame and
340
// allocated a second segment (see below),
341
// the fp is slightly above top - top->args.
342
// That condition can't happen normally though
343
// (stack pointers go down, not up), so we can accept
344
// any fp between top and top - top->args as
345
// indicating the top of the segment.
346
top = (Stktop*)g->stackbase;
347
if(argp < (byte*)top - top->argsize || (byte*)top < argp)
350
// The deferred call makes a new segment big enough
351
// for the argument frame but not necessarily big
352
// enough for the function's local frame (size unknown
353
// at the time of the call), so the function might have
354
// made its own segment immediately. If that's the
355
// case, back top up to the older one, the one that
356
// reflect.call would have made for the panic.
358
// The fp comparison here checks that the argument
359
// frame that was copied during the split (the top->args
360
// bytes above top->fp) abuts the old top of stack.
361
// This is a correct test for both closure and non-closure code.
362
oldtop = (Stktop*)top->stackbase;
363
if(oldtop != nil && top->argp == (byte*)oldtop - top->argsize)
366
// Now we have the segment that was created to
367
// run this call. It must have been marked as a panic segment.
371
// Okay, this is the top frame of a deferred call
372
// in response to a panic. It can see the panic argument.
385
runtime·startpanic(void)
387
if(runtime·mheap == 0 || runtime·mheap->cachealloc.size == 0) { // very early
388
runtime·printf("runtime: panic before malloc heap initialized\n");
389
m->mallocing = 1; // tell rest of panic not to try to malloc
390
} else if(m->mcache == nil) // can happen if called from signal handler or throw
391
m->mcache = runtime·allocmcache();
393
runtime·printf("panic during panic\n");
397
runtime·xadd(&runtime·panicking, 1);
398
runtime·lock(&paniclk);
402
runtime·dopanic(int32 unused)
404
static bool didothers;
408
runtime·printf("[signal %x code=%p addr=%p pc=%p]\n",
409
g->sig, g->sigcode0, g->sigcode1, g->sigpc);
411
if(runtime·gotraceback(&crash)){
413
runtime·printf("\n");
414
runtime·goroutineheader(g);
415
runtime·traceback(runtime·getcallerpc(&unused), runtime·getcallersp(&unused), 0, g);
419
runtime·tracebackothers(g);
422
runtime·unlock(&paniclk);
423
if(runtime·xadd(&runtime·panicking, -1) != 0) {
424
// Some other m is panicking too.
425
// Let it print what it needs to print.
426
// Wait forever without chewing up cpu.
427
// It will exit when it's done.
428
static Lock deadlock;
429
runtime·lock(&deadlock);
430
runtime·lock(&deadlock);
440
runtime·panicindex(void)
442
runtime·panicstring("index out of range");
446
runtime·panicslice(void)
448
runtime·panicstring("slice bounds out of range");
452
runtime·throwreturn(void)
454
// can only happen if compiler is broken
455
runtime·throw("no return at end of a typed function - compiler is broken");
459
runtime·throwinit(void)
461
// can only happen with linker skew
462
runtime·throw("recursive call during initialization - linker skew");
466
runtime·throw(int8 *s)
470
runtime·startpanic();
471
runtime·printf("fatal error: %s\n", s);
473
*(int32*)0 = 0; // not reached
474
runtime·exit(1); // even more not reached
478
runtime·panicstring(int8 *s)
483
runtime·printf("panic: %s\n", s);
484
runtime·throw("panic during gc");
486
runtime·newErrorString(runtime·gostringnocopy((byte*)s), &err);