go-panic.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* go-panic.c -- support for the go panic function.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "runtime.h"
  8. #include "arch.h"
  9. #include "malloc.h"
  10. #include "go-alloc.h"
  11. #include "go-defer.h"
  12. #include "go-panic.h"
  13. #include "interface.h"
  14. /* Print the panic stack. This is used when there is no recover. */
  15. static void
  16. __printpanics (struct __go_panic_stack *p)
  17. {
  18. if (p->__next != NULL)
  19. {
  20. __printpanics (p->__next);
  21. runtime_printf ("\t");
  22. }
  23. runtime_printf ("panic: ");
  24. runtime_printany (p->__arg);
  25. if (p->__was_recovered)
  26. runtime_printf (" [recovered]");
  27. runtime_printf ("\n");
  28. }
  29. /* This implements __go_panic which is used for the panic
  30. function. */
  31. void
  32. __go_panic (struct __go_empty_interface arg)
  33. {
  34. G *g;
  35. struct __go_panic_stack *n;
  36. g = runtime_g ();
  37. n = (struct __go_panic_stack *) __go_alloc (sizeof (struct __go_panic_stack));
  38. n->__arg = arg;
  39. n->__next = g->panic;
  40. g->panic = n;
  41. /* Run all the defer functions. */
  42. while (1)
  43. {
  44. struct __go_defer_stack *d;
  45. void (*pfn) (void *);
  46. d = g->defer;
  47. if (d == NULL)
  48. break;
  49. pfn = d->__pfn;
  50. d->__pfn = NULL;
  51. if (pfn != NULL)
  52. {
  53. (*pfn) (d->__arg);
  54. if (n->__was_recovered)
  55. {
  56. /* Some defer function called recover. That means that
  57. we should stop running this panic. */
  58. g->panic = n->__next;
  59. __go_free (n);
  60. /* Now unwind the stack by throwing an exception. The
  61. compiler has arranged to create exception handlers in
  62. each function which uses a defer statement. These
  63. exception handlers will check whether the entry on
  64. the top of the defer stack is from the current
  65. function. If it is, we have unwound the stack far
  66. enough. */
  67. __go_unwind_stack ();
  68. /* __go_unwind_stack should not return. */
  69. abort ();
  70. }
  71. /* Because we executed that defer function by a panic, and
  72. it did not call recover, we know that we are not
  73. returning from the calling function--we are panicing
  74. through it. */
  75. *d->__frame = 0;
  76. }
  77. g->defer = d->__next;
  78. /* This may be called by a cgo callback routine to defer the
  79. call to syscall.CgocallBackDone, in which case we will not
  80. have a memory context. Don't try to free anything in that
  81. case--the GC will release it later. */
  82. if (runtime_m () != NULL)
  83. runtime_freedefer (d);
  84. }
  85. /* The panic was not recovered. */
  86. runtime_startpanic ();
  87. __printpanics (g->panic);
  88. runtime_dopanic (0);
  89. }