xref: /petsc/src/sys/error/pstack.c (revision ca93e9546dc92e90e21611678f80a2cde2568e43)
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 SAWs_Directory amsmemstack = NULL;
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_Directory(amsmemstack);
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_Directory(amsmemstack);
63   }
64 }
65 
66 PetscErrorCode PetscStackViewSAWs(void)
67 {
68   SAWs_Directory mem;
69   PetscStack*    petscstackp;
70 
71   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
72   PetscStackCallSAWs(SAWs_Add_Directory,(SAWs_ROOT_DIRECTORY,"Stack",&mem));
73   PetscStackCallSAWs(SAWs_Add_Variable,(mem,"functions",petscstackp->function,10,SAWs_READ,SAWs_STRING));
74   PetscStackCallSAWs(SAWs_Add_Variable,(mem,"current size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT));
75   amsmemstack = mem;
76   return 0;
77 }
78 
79 #undef __FUNCT__
80 #define __FUNCT__ "PetscStackSAWsViewOff"
81 PetscErrorCode PetscStackSAWsViewOff(void)
82 {
83   PetscErrorCode ierr;
84 
85   PetscFunctionBegin;
86   if (!amsmemstack) PetscFunctionReturn(0);
87   ierr        = SAWs_Destroy_Directory(&amsmemstack);CHKERRQ(ierr);
88   PetscFunctionReturn(0);
89 }
90 
91 #endif
92 
93 PetscErrorCode PetscStackCreate(void)
94 {
95   PetscStack *petscstack_in;
96   if (PetscStackActive()) return 0;
97 
98   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
99   petscstack_in->currentsize = 0;
100   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
101 
102 #if defined(PETSC_HAVE_SAWS)
103   {
104   PetscBool flg = PETSC_FALSE;
105   PetscOptionsHasName(NULL,"-stack_view",&flg);
106   if (flg) PetscStackViewSAWs();
107   }
108 #endif
109   return 0;
110 }
111 
112 
113 #undef __FUNCT__
114 #define __FUNCT__ "PetscStackView"
115 PetscErrorCode  PetscStackView(FILE *file)
116 {
117   int        i;
118   PetscStack *petscstackp;
119 
120   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
121   if (!file) file = PETSC_STDOUT;
122 
123   if (file == PETSC_STDOUT) {
124     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
125     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
126     (*PetscErrorPrintf)("      is given.\n");
127     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]);
128   } else {
129     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
130     fprintf(file,"      INSTEAD the line number of the start of the function\n");
131     fprintf(file,"      is given.\n");
132     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]);
133   }
134   return 0;
135 }
136 
137 PetscErrorCode PetscStackDestroy(void)
138 {
139   if (PetscStackActive()) {
140     PetscStack *petscstack_in;
141     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
142     free(petscstack_in);
143     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
144     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
145   }
146   return 0;
147 }
148 
149 #undef __FUNCT__
150 #define __FUNCT__ "PetscStackCopy"
151 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
152 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
153 {
154   int i;
155 
156   if (!sint) sout->currentsize = 0;
157   else {
158     for (i=0; i<sint->currentsize; i++) {
159       sout->function[i]     = sint->function[i];
160       sout->file[i]         = sint->file[i];
161       sout->directory[i]    = sint->directory[i];
162       sout->line[i]         = sint->line[i];
163       sout->petscroutine[i] = sint->petscroutine[i];
164     }
165     sout->currentsize = sint->currentsize;
166   }
167   return 0;
168 }
169 
170 #undef __FUNCT__
171 #define __FUNCT__ "PetscStackPrint"
172 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
173 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
174 {
175   int i;
176 
177   if (!sint) return(0);
178   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]);
179   return 0;
180 }
181 
182 #else
183 
184 #undef __FUNCT__
185 #define __FUNCT__ "PetscStackCreate"
186 PetscErrorCode  PetscStackCreate(void)
187 {
188   PetscFunctionBegin;
189   PetscFunctionReturn(0);
190 }
191 #undef __FUNCT__
192 #define __FUNCT__ "PetscStackView"
193 PetscErrorCode  PetscStackView(FILE *file)
194 {
195   PetscFunctionBegin;
196   PetscFunctionReturn(0);
197 }
198 #undef __FUNCT__
199 #define __FUNCT__ "PetscStackDestroy"
200 PetscErrorCode  PetscStackDestroy(void)
201 {
202   PetscFunctionBegin;
203   PetscFunctionReturn(0);
204 }
205 
206 #if defined(PETSC_HAVE_SAWS)     /* SAWs stack functions do nothing in optimized mode */
207 void PetscStackSAWsGrantAccess(void) {}
208 void PetscStackSAWsTakeAccess(void) {}
209 
210 PetscErrorCode PetscStackViewSAWs(void)
211 {
212   return 0;
213 }
214 
215 #undef __FUNCT__
216 #define __FUNCT__ "PetscStackSAWsViewOff"
217 PetscErrorCode  PetscStackSAWsViewOff(void)
218 {
219   PetscFunctionBegin;
220   PetscFunctionReturn(0);
221 }
222 #endif
223 
224 #endif
225 
226