xref: /petsc/src/sys/error/pstack.c (revision 9f7953f812aad28c4b42ada027707cec225eff2f) !
1 
2 #include <petscsys.h>        /*I  "petscsys.h"   I*/
3 
4 #if defined(PETSC_USE_DEBUG)
5 
6 #if defined(PETSC_HAVE_PTHREADCLASSES)
7 #if defined(PETSC_PTHREAD_LOCAL)
8 PETSC_PTHREAD_LOCAL PetscStack *petscstack = 0;
9 #endif
10 #else
11 PetscStack *petscstack = 0;
12 #endif
13 
14 #undef __FUNCT__
15 #define __FUNCT__ "PetscStackPublish"
16 PetscErrorCode  PetscStackPublish(void)
17 {
18   PetscFunctionBegin;
19   PetscFunctionReturn(0);
20 }
21 
22 #undef __FUNCT__
23 #define __FUNCT__ "PetscStackDepublish"
24 PetscErrorCode  PetscStackDepublish(void)
25 {
26   PetscFunctionBegin;
27   PetscFunctionReturn(0);
28 }
29 
30 PetscErrorCode PetscStackCreate(void)
31 {
32   PetscStack *petscstack_in;
33   if (PetscStackActive()) return 0;
34 
35   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
36   petscstack_in->currentsize = 0;
37   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
38   return 0;
39 }
40 
41 #undef __FUNCT__
42 #define __FUNCT__ "PetscStackView"
43 PetscErrorCode  PetscStackView(FILE *file)
44 {
45   int        i;
46   PetscStack *petscstackp;
47 
48   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
49   if (!file) file = PETSC_STDOUT;
50 
51   if (file == PETSC_STDOUT) {
52     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
53     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
54     (*PetscErrorPrintf)("      is given.\n");
55     for (i=petscstackp->currentsize-1; i>=0; i--) (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
56   } else {
57     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
58     fprintf(file,"      INSTEAD the line number of the start of the function\n");
59     fprintf(file,"      is given.\n");
60     for (i=petscstackp->currentsize-1; i>=0; i--) fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
61   }
62   return 0;
63 }
64 
65 PetscErrorCode PetscStackDestroy(void)
66 {
67   if (PetscStackActive()) {
68     PetscStack *petscstack_in;
69     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
70     free(petscstack_in);
71     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
72     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
73   }
74   return 0;
75 }
76 
77 #undef __FUNCT__
78 #define __FUNCT__ "PetscStackCopy"
79 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
80 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
81 {
82   int i;
83 
84   if (!sint) sout->currentsize = 0;
85   else {
86     for (i=0; i<sint->currentsize; i++) {
87       sout->function[i]     = sint->function[i];
88       sout->file[i]         = sint->file[i];
89       sout->directory[i]    = sint->directory[i];
90       sout->line[i]         = sint->line[i];
91       sout->petscroutine[i] = sint->petscroutine[i];
92     }
93     sout->currentsize = sint->currentsize;
94   }
95   return 0;
96 }
97 
98 #undef __FUNCT__
99 #define __FUNCT__ "PetscStackPrint"
100 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
101 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
102 {
103   int i;
104 
105   if (!sint) return(0);
106   for (i=sint->currentsize-2; i>=0; i--) fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
107   return 0;
108 }
109 
110 #else
111 #undef __FUNCT__
112 #define __FUNCT__ "PetscStackPublish"
113 PetscErrorCode  PetscStackPublish(void)
114 {
115   PetscFunctionBegin;
116   PetscFunctionReturn(0);
117 }
118 #undef __FUNCT__
119 #define __FUNCT__ "PetscStackDepublish"
120 PetscErrorCode  PetscStackDepublish(void)
121 {
122   PetscFunctionBegin;
123   PetscFunctionReturn(0);
124 }
125 #undef __FUNCT__
126 #define __FUNCT__ "PetscStackCreate"
127 PetscErrorCode  PetscStackCreate(void)
128 {
129   PetscFunctionBegin;
130   PetscFunctionReturn(0);
131 }
132 #undef __FUNCT__
133 #define __FUNCT__ "PetscStackView"
134 PetscErrorCode  PetscStackView(FILE *file)
135 {
136   PetscFunctionBegin;
137   PetscFunctionReturn(0);
138 }
139 #undef __FUNCT__
140 #define __FUNCT__ "PetscStackDestroy"
141 PetscErrorCode  PetscStackDestroy(void)
142 {
143   PetscFunctionBegin;
144   PetscFunctionReturn(0);
145 }
146 
147 #endif
148 
149