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