xref: /petsc/src/sys/error/pstack.c (revision 16ad03002c971217bb7d68d241ce25e1b2bc6bef)
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 
15 #if defined(PETSC_HAVE_SAWS)
16 #include <petscviewersaws.h>
17 
18 static PetscBool amsmemstack = PETSC_FALSE;
19 
20 #undef __FUNCT__
21 #define __FUNCT__ "PetscStackSAWsGrantAccess"
22 /*@C
23    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher
24 
25    Collective on PETSC_COMM_WORLD?
26 
27    Level: developer
28 
29    Concepts: publishing object
30 
31    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
32 
33 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
34 
35 @*/
36 void  PetscStackSAWsGrantAccess(void)
37 {
38   if (amsmemstack) {
39     /* ignore any errors from SAWs */
40     SAWs_Unlock();
41   }
42 }
43 
44 #undef __FUNCT__
45 #define __FUNCT__ "PetscStackSAWsTakeAccess"
46 /*@C
47    PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher
48 
49    Collective on PETSC_COMM_WORLD?
50 
51    Level: developer
52 
53    Concepts: publishing object
54 
55    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
56 
57 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
58 
59 @*/
60 void  PetscStackSAWsTakeAccess(void)
61 {
62   if (amsmemstack) {
63     /* ignore any errors from SAWs */
64     SAWs_Lock();
65   }
66 }
67 
68 PetscErrorCode PetscStackViewSAWs(void)
69 {
70   PetscStack*    petscstackp;
71 
72   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
73   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstackp->function,20,SAWs_READ,SAWs_STRING));
74   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/_current_size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT));
75   amsmemstack = PETSC_TRUE;
76   return 0;
77 }
78 
79 #undef __FUNCT__
80 #define __FUNCT__ "PetscStackSAWsViewOff"
81 PetscErrorCode PetscStackSAWsViewOff(void)
82 {
83   PetscFunctionBegin;
84   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
85   amsmemstack = PETSC_FALSE;
86   PetscFunctionReturn(0);
87 }
88 
89 #endif
90 
91 PetscErrorCode PetscStackCreate(void)
92 {
93   PetscStack *petscstack_in;
94   if (PetscStackActive()) return 0;
95 
96   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
97   petscstack_in->currentsize = 0;
98   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
99 
100 #if defined(PETSC_HAVE_SAWS)
101   {
102   PetscBool flg = PETSC_FALSE;
103   PetscOptionsHasName(NULL,"-stack_view",&flg);
104   if (flg) PetscStackViewSAWs();
105   }
106 #endif
107   return 0;
108 }
109 
110 
111 #undef __FUNCT__
112 #define __FUNCT__ "PetscStackView"
113 PetscErrorCode  PetscStackView(FILE *file)
114 {
115   int        i;
116   PetscStack *petscstackp;
117 
118   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
119   if (!file) file = PETSC_STDOUT;
120 
121   if (file == PETSC_STDOUT) {
122     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
123     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
124     (*PetscErrorPrintf)("      is given.\n");
125     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]);
126   } else {
127     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
128     fprintf(file,"      INSTEAD the line number of the start of the function\n");
129     fprintf(file,"      is given.\n");
130     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]);
131   }
132   return 0;
133 }
134 
135 PetscErrorCode PetscStackDestroy(void)
136 {
137   if (PetscStackActive()) {
138     PetscStack *petscstack_in;
139     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
140     free(petscstack_in);
141     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
142     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
143   }
144   return 0;
145 }
146 
147 #undef __FUNCT__
148 #define __FUNCT__ "PetscStackCopy"
149 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
150 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
151 {
152   int i;
153 
154   if (!sint) sout->currentsize = 0;
155   else {
156     for (i=0; i<sint->currentsize; i++) {
157       sout->function[i]     = sint->function[i];
158       sout->file[i]         = sint->file[i];
159       sout->directory[i]    = sint->directory[i];
160       sout->line[i]         = sint->line[i];
161       sout->petscroutine[i] = sint->petscroutine[i];
162     }
163     sout->currentsize = sint->currentsize;
164   }
165   return 0;
166 }
167 
168 #undef __FUNCT__
169 #define __FUNCT__ "PetscStackPrint"
170 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
171 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
172 {
173   int i;
174 
175   if (!sint) return(0);
176   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]);
177   return 0;
178 }
179 
180 #else
181 
182 #undef __FUNCT__
183 #define __FUNCT__ "PetscStackCreate"
184 PetscErrorCode  PetscStackCreate(void)
185 {
186   PetscFunctionBegin;
187   PetscFunctionReturn(0);
188 }
189 #undef __FUNCT__
190 #define __FUNCT__ "PetscStackView"
191 PetscErrorCode  PetscStackView(FILE *file)
192 {
193   PetscFunctionBegin;
194   PetscFunctionReturn(0);
195 }
196 #undef __FUNCT__
197 #define __FUNCT__ "PetscStackDestroy"
198 PetscErrorCode  PetscStackDestroy(void)
199 {
200   PetscFunctionBegin;
201   PetscFunctionReturn(0);
202 }
203 
204 #if defined(PETSC_HAVE_SAWS)     /* SAWs stack functions do nothing in optimized mode */
205 void PetscStackSAWsGrantAccess(void) {}
206 void PetscStackSAWsTakeAccess(void) {}
207 
208 PetscErrorCode PetscStackViewSAWs(void)
209 {
210   return 0;
211 }
212 
213 #undef __FUNCT__
214 #define __FUNCT__ "PetscStackSAWsViewOff"
215 PetscErrorCode  PetscStackSAWsViewOff(void)
216 {
217   PetscFunctionBegin;
218   PetscFunctionReturn(0);
219 }
220 #endif
221 
222 #endif
223 
224