runtime: runtime.Caller should succeed even without debug info.
[gcc.git] / libgo / runtime / runtime.h
1 /* runtime.h -- runtime support for Go.
2
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 #include "config.h"
8
9 #include "go-assert.h"
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <ucontext.h>
21
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
25
26 #include "array.h"
27 #include "go-alloc.h"
28 #include "go-panic.h"
29 #include "go-string.h"
30
31 /* This file supports C files copied from the 6g runtime library.
32 This is a version of the 6g runtime.h rewritten for gccgo's version
33 of the code. */
34
35 typedef signed int int8 __attribute__ ((mode (QI)));
36 typedef unsigned int uint8 __attribute__ ((mode (QI)));
37 typedef signed int int16 __attribute__ ((mode (HI)));
38 typedef unsigned int uint16 __attribute__ ((mode (HI)));
39 typedef signed int int32 __attribute__ ((mode (SI)));
40 typedef unsigned int uint32 __attribute__ ((mode (SI)));
41 typedef signed int int64 __attribute__ ((mode (DI)));
42 typedef unsigned int uint64 __attribute__ ((mode (DI)));
43 typedef float float32 __attribute__ ((mode (SF)));
44 typedef double float64 __attribute__ ((mode (DF)));
45 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
46
47 /* Defined types. */
48
49 typedef uint8 bool;
50 typedef uint8 byte;
51 typedef struct Func Func;
52 typedef struct G G;
53 typedef union Lock Lock;
54 typedef struct M M;
55 typedef union Note Note;
56 typedef struct SigTab SigTab;
57 typedef struct MCache MCache;
58 typedef struct FixAlloc FixAlloc;
59 typedef struct Hchan Hchan;
60 typedef struct Timers Timers;
61 typedef struct Timer Timer;
62
63 typedef struct __go_open_array Slice;
64 typedef struct __go_string String;
65 typedef struct __go_interface Iface;
66 typedef struct __go_empty_interface Eface;
67 typedef struct __go_type_descriptor Type;
68 typedef struct __go_defer_stack Defer;
69 typedef struct __go_panic_stack Panic;
70
71 typedef struct __go_func_type FuncType;
72 typedef struct __go_map_type MapType;
73
74 typedef struct Traceback Traceback;
75
76 /*
77 * per-cpu declaration.
78 */
79 extern M* runtime_m(void);
80 extern G* runtime_g(void);
81
82 extern M runtime_m0;
83 extern G runtime_g0;
84
85 /*
86 * defined constants
87 */
88 enum
89 {
90 // G status
91 //
92 // If you add to this list, add to the list
93 // of "okay during garbage collection" status
94 // in mgc0.c too.
95 Gidle,
96 Grunnable,
97 Grunning,
98 Gsyscall,
99 Gwaiting,
100 Gmoribund,
101 Gdead,
102 };
103 enum
104 {
105 true = 1,
106 false = 0,
107 };
108
109 /*
110 * structures
111 */
112 union Lock
113 {
114 uint32 key; // futex-based impl
115 M* waitm; // linked list of waiting M's (sema-based impl)
116 };
117 union Note
118 {
119 uint32 key; // futex-based impl
120 M* waitm; // waiting M (sema-based impl)
121 };
122 struct G
123 {
124 Defer* defer;
125 Panic* panic;
126 void* exception; // current exception being thrown
127 bool is_foreign; // whether current exception from other language
128 void *gcstack; // if status==Gsyscall, gcstack = stackbase to use during gc
129 uintptr gcstack_size;
130 void* gcnext_segment;
131 void* gcnext_sp;
132 void* gcinitial_sp;
133 ucontext_t gcregs;
134 byte* entry; // initial function
135 G* alllink; // on allg
136 void* param; // passed parameter on wakeup
137 bool fromgogo; // reached from gogo
138 int16 status;
139 int32 goid;
140 uint32 selgen; // valid sudog pointer
141 const char* waitreason; // if status==Gwaiting
142 G* schedlink;
143 bool readyonstop;
144 bool ispanic;
145 M* m; // for debuggers, but offset not hard-coded
146 M* lockedm;
147 M* idlem;
148 int32 sig;
149 int32 writenbuf;
150 byte* writebuf;
151 uintptr sigcode0;
152 uintptr sigcode1;
153 // uintptr sigpc;
154 uintptr gopc; // pc of go statement that created this goroutine
155
156 int32 ncgo;
157 struct cgoalloc *cgoalloc;
158
159 Traceback* traceback;
160
161 ucontext_t context;
162 void* stack_context[10];
163 };
164
165 struct M
166 {
167 G* g0; // goroutine with scheduling stack
168 G* gsignal; // signal-handling G
169 G* curg; // current running goroutine
170 int32 id;
171 int32 mallocing;
172 int32 gcing;
173 int32 locks;
174 int32 nomemprof;
175 int32 waitnextg;
176 int32 dying;
177 int32 profilehz;
178 int32 helpgc;
179 uint32 fastrand;
180 uint64 ncgocall;
181 Note havenextg;
182 G* nextg;
183 M* alllink; // on allm
184 M* schedlink;
185 MCache *mcache;
186 G* lockedg;
187 G* idleg;
188 uintptr createstack[32]; // Stack that created this thread.
189 M* nextwaitm; // next M waiting for lock
190 uintptr waitsema; // semaphore for parking on locks
191 uint32 waitsemacount;
192 uint32 waitsemalock;
193 };
194
195 struct SigTab
196 {
197 int32 sig;
198 int32 flags;
199 };
200 enum
201 {
202 SigNotify = 1<<0, // let signal.Notify have signal, even if from kernel
203 SigKill = 1<<1, // if signal.Notify doesn't take it, exit quietly
204 SigThrow = 1<<2, // if signal.Notify doesn't take it, exit loudly
205 SigPanic = 1<<3, // if the signal is from the kernel, panic
206 SigDefault = 1<<4, // if the signal isn't explicitly requested, don't monitor it
207 };
208
209 #ifndef NSIG
210 #define NSIG 32
211 #endif
212
213 // NOTE(rsc): keep in sync with extern.go:/type.Func.
214 // Eventually, the loaded symbol table should be closer to this form.
215 struct Func
216 {
217 String name;
218 uintptr entry; // entry pc
219 };
220
221 /* Macros. */
222
223 #ifdef GOOS_windows
224 enum {
225 Windows = 1
226 };
227 #else
228 enum {
229 Windows = 0
230 };
231 #endif
232
233 struct Timers
234 {
235 Lock;
236 G *timerproc;
237 bool sleeping;
238 bool rescheduling;
239 Note waitnote;
240 Timer **t;
241 int32 len;
242 int32 cap;
243 };
244
245 // Package time knows the layout of this structure.
246 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
247 struct Timer
248 {
249 int32 i; // heap index
250
251 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
252 // each time calling f(now, arg) in the timer goroutine, so f must be
253 // a well-behaved function and not block.
254 int64 when;
255 int64 period;
256 void (*f)(int64, Eface);
257 Eface arg;
258 };
259
260 /*
261 * defined macros
262 * you need super-gopher-guru privilege
263 * to add this list.
264 */
265 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
266 #define nil ((void*)0)
267 #define USED(v) ((void) v)
268
269 /*
270 * external data
271 */
272 G* runtime_allg;
273 G* runtime_lastg;
274 M* runtime_allm;
275 extern int32 runtime_gomaxprocs;
276 extern bool runtime_singleproc;
277 extern uint32 runtime_panicking;
278 extern int32 runtime_gcwaiting; // gc is waiting to run
279 int32 runtime_ncpu;
280
281 /*
282 * common functions and data
283 */
284 int32 runtime_findnull(const byte*);
285 void runtime_dump(byte*, int32);
286
287 /*
288 * very low level c-called
289 */
290 void runtime_args(int32, byte**);
291 void runtime_osinit();
292 void runtime_goargs(void);
293 void runtime_goenvs(void);
294 void runtime_goenvs_unix(void);
295 void runtime_throw(const char*) __attribute__ ((noreturn));
296 void runtime_panicstring(const char*) __attribute__ ((noreturn));
297 void runtime_prints(const char*);
298 void runtime_printf(const char*, ...);
299 void* runtime_mal(uintptr);
300 void runtime_schedinit(void);
301 void runtime_initsig(void);
302 void runtime_sigenable(uint32 sig);
303 int32 runtime_gotraceback(void);
304 void runtime_goroutineheader(G*);
305 void runtime_goroutinetrailer(G*);
306 void runtime_traceback();
307 void runtime_tracebackothers(G*);
308 void runtime_printtrace(uintptr*, int32);
309 String runtime_gostringnocopy(const byte*);
310 void* runtime_mstart(void*);
311 G* runtime_malg(int32, byte**, size_t*);
312 void runtime_minit(void);
313 void runtime_mallocinit(void);
314 void runtime_gosched(void);
315 void runtime_tsleep(int64);
316 M* runtime_newm(void);
317 void runtime_goexit(void);
318 void runtime_entersyscall(void) __asm__("syscall.Entersyscall");
319 void runtime_exitsyscall(void) __asm__("syscall.Exitsyscall");
320 void siginit(void);
321 bool __go_sigsend(int32 sig);
322 int32 runtime_callers(int32, uintptr*, int32);
323 int64 runtime_nanotime(void);
324 int64 runtime_cputicks(void);
325
326 void runtime_stoptheworld(void);
327 void runtime_starttheworld(bool);
328 extern uint32 runtime_worldsema;
329 G* __go_go(void (*pfn)(void*), void*);
330
331 /*
332 * mutual exclusion locks. in the uncontended case,
333 * as fast as spin locks (just a few user-level instructions),
334 * but on the contention path they sleep in the kernel.
335 * a zeroed Lock is unlocked (no need to initialize each lock).
336 */
337 void runtime_lock(Lock*);
338 void runtime_unlock(Lock*);
339
340 /*
341 * sleep and wakeup on one-time events.
342 * before any calls to notesleep or notewakeup,
343 * must call noteclear to initialize the Note.
344 * then, exactly one thread can call notesleep
345 * and exactly one thread can call notewakeup (once).
346 * once notewakeup has been called, the notesleep
347 * will return. future notesleep will return immediately.
348 * subsequent noteclear must be called only after
349 * previous notesleep has returned, e.g. it's disallowed
350 * to call noteclear straight after notewakeup.
351 *
352 * notetsleep is like notesleep but wakes up after
353 * a given number of nanoseconds even if the event
354 * has not yet happened. if a goroutine uses notetsleep to
355 * wake up early, it must wait to call noteclear until it
356 * can be sure that no other goroutine is calling
357 * notewakeup.
358 */
359 void runtime_noteclear(Note*);
360 void runtime_notesleep(Note*);
361 void runtime_notewakeup(Note*);
362 void runtime_notetsleep(Note*, int64);
363
364 /*
365 * low-level synchronization for implementing the above
366 */
367 uintptr runtime_semacreate(void);
368 int32 runtime_semasleep(int64);
369 void runtime_semawakeup(M*);
370 // or
371 void runtime_futexsleep(uint32*, uint32, int64);
372 void runtime_futexwakeup(uint32*, uint32);
373
374 /*
375 * low level C-called
376 */
377 #define runtime_mmap mmap
378 #define runtime_munmap munmap
379 #define runtime_madvise madvise
380 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
381 #define runtime_getcallerpc(p) __builtin_return_address(0)
382
383 #ifdef __rtems__
384 void __wrap_rtems_task_variable_add(void **);
385 #endif
386
387 /*
388 * Names generated by gccgo.
389 */
390 #define runtime_printbool __go_print_bool
391 #define runtime_printfloat __go_print_double
392 #define runtime_printint __go_print_int64
393 #define runtime_printiface __go_print_interface
394 #define runtime_printeface __go_print_empty_interface
395 #define runtime_printstring __go_print_string
396 #define runtime_printpointer __go_print_pointer
397 #define runtime_printuint __go_print_uint64
398 #define runtime_printslice __go_print_slice
399 #define runtime_printcomplex __go_print_complex
400
401 /*
402 * runtime go-called
403 */
404 void runtime_printbool(_Bool);
405 void runtime_printfloat(double);
406 void runtime_printint(int64);
407 void runtime_printiface(Iface);
408 void runtime_printeface(Eface);
409 void runtime_printstring(String);
410 void runtime_printpc(void*);
411 void runtime_printpointer(void*);
412 void runtime_printuint(uint64);
413 void runtime_printhex(uint64);
414 void runtime_printslice(Slice);
415 void runtime_printcomplex(__complex double);
416
417 struct __go_func_type;
418 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
419 void **, void **)
420 asm ("reflect.call");
421
422 /* Functions. */
423 #define runtime_panic __go_panic
424 #define runtime_write(d, v, n) write((d), (v), (n))
425 #define runtime_malloc(s) __go_alloc(s)
426 #define runtime_free(p) __go_free(p)
427 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
428 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
429 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
430 #define runtime_exit(s) exit(s)
431 MCache* runtime_allocmcache(void);
432 void free(void *v);
433 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
434 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
435 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
436 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
437 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
438 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
439 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
440 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
441
442 struct __go_func_type;
443 bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
444 #define runtime_getcallersp(p) __builtin_frame_address(1)
445 int32 runtime_mcount(void);
446 int32 runtime_gcount(void);
447 void runtime_dopanic(int32) __attribute__ ((noreturn));
448 void runtime_startpanic(void);
449 void runtime_ready(G*);
450 const byte* runtime_getenv(const char*);
451 int32 runtime_atoi(const byte*);
452 uint32 runtime_fastrand1(void);
453
454 void runtime_sigprof();
455 void runtime_resetcpuprofiler(int32);
456 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
457 void runtime_usleep(uint32);
458
459 /*
460 * runtime c-called (but written in Go)
461 */
462 void runtime_printany(Eface)
463 __asm__("runtime.Printany");
464 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
465 __asm__("runtime.NewTypeAssertionError");
466 void runtime_newErrorString(String, Eface*)
467 __asm__("runtime.NewErrorString");
468
469 /*
470 * wrapped for go users
471 */
472 bool runtime_isInf(float64 f, int32 sign);
473 #define runtime_isNaN(f) __builtin_isnan(f)
474 void runtime_semacquire(uint32 volatile *);
475 void runtime_semrelease(uint32 volatile *);
476 int32 runtime_gomaxprocsfunc(int32 n);
477 void runtime_procyield(uint32);
478 void runtime_osyield(void);
479 void runtime_LockOSThread(void) __asm__("runtime.LockOSThread");
480 void runtime_UnlockOSThread(void) __asm__("runtime.UnlockOSThread");
481
482 bool runtime_showframe(const unsigned char*);
483
484 uintptr runtime_memlimit(void);
485
486 // If appropriate, ask the operating system to control whether this
487 // thread should receive profiling signals. This is only necessary on OS X.
488 // An operating system should not deliver a profiling signal to a
489 // thread that is not actually executing (what good is that?), but that's
490 // what OS X prefers to do. When profiling is turned on, we mask
491 // away the profiling signal when threads go to sleep, so that OS X
492 // is forced to deliver the signal to a thread that's actually running.
493 // This is a no-op on other systems.
494 void runtime_setprof(bool);
495
496 void runtime_time_scan(void (*)(byte*, int64));
497 void runtime_trampoline_scan(void (*)(byte *, int64));
498
499 void runtime_setsig(int32, bool, bool);
500 #define runtime_setitimer setitimer
501
502 void runtime_check(void);
503
504 // A list of global variables that the garbage collector must scan.
505 struct root_list {
506 struct root_list *next;
507 struct root {
508 void *decl;
509 size_t size;
510 } roots[];
511 };
512
513 void __go_register_gc_roots(struct root_list*);
514
515 // Size of stack space allocated using Go's allocator.
516 // This will be 0 when using split stacks, as in that case
517 // the stacks are allocated by the splitstack library.
518 extern uintptr runtime_stacks_sys;
519
520 struct backtrace_state;
521 extern struct backtrace_state *__go_get_backtrace_state(void);
522 extern _Bool __go_file_line(uintptr, String*, String*, int *);
523
524 int32 getproccount(void);