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