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