xref: /petsc/src/sys/error/pstack.c (revision 7d5f7e0c2c69ab2af36385a09fab7748470422fa)
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_HAVE_PTHREADCLASSES)
8 #if defined(PETSC_PTHREAD_LOCAL)
9 PETSC_PTHREAD_LOCAL PetscStack *petscstack = 0;
10 #endif
11 #else
12 PetscStack *petscstack = 0;
13 #endif
14 
15 #undef __FUNCT__
16 #define __FUNCT__ "PetscStackPublish"
17 PetscErrorCode  PetscStackPublish(void)
18 {
19   PetscFunctionBegin;
20   PetscFunctionReturn(0);
21 }
22 
23 #undef __FUNCT__
24 #define __FUNCT__ "PetscStackDepublish"
25 PetscErrorCode  PetscStackDepublish(void)
26 {
27   PetscFunctionBegin;
28   PetscFunctionReturn(0);
29 }
30 
31 PetscErrorCode PetscStackCreate(void)
32 {
33   PetscStack *petscstack_in;
34   if (PetscStackActive()) return 0;
35 
36   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
37   petscstack_in->currentsize = 0;
38   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
39   return 0;
40 }
41 
42 #undef __FUNCT__
43 #define __FUNCT__ "PetscStackView"
44 PetscErrorCode  PetscStackView(FILE *file)
45 {
46   int        i;
47   PetscStack *petscstackp;
48 
49   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
50   if (!file) file = PETSC_STDOUT;
51 
52   if (file == PETSC_STDOUT) {
53     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
54     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
55     (*PetscErrorPrintf)("      is given.\n");
56     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]);
57   } else {
58     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
59     fprintf(file,"      INSTEAD the line number of the start of the function\n");
60     fprintf(file,"      is given.\n");
61     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]);
62   }
63   return 0;
64 }
65 
66 PetscErrorCode PetscStackDestroy(void)
67 {
68   if (PetscStackActive()) {
69     PetscStack *petscstack_in;
70     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
71     free(petscstack_in);
72     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
73     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
74   }
75   return 0;
76 }
77 
78 #undef __FUNCT__
79 #define __FUNCT__ "PetscStackCopy"
80 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
81 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
82 {
83   int i;
84 
85   if (!sint) sout->currentsize = 0;
86   else {
87     for (i=0; i<sint->currentsize; i++) {
88       sout->function[i]     = sint->function[i];
89       sout->file[i]         = sint->file[i];
90       sout->directory[i]    = sint->directory[i];
91       sout->line[i]         = sint->line[i];
92       sout->petscroutine[i] = sint->petscroutine[i];
93     }
94     sout->currentsize = sint->currentsize;
95   }
96   return 0;
97 }
98 
99 #undef __FUNCT__
100 #define __FUNCT__ "PetscStackPrint"
101 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
102 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
103 {
104   int i;
105 
106   if (!sint) return(0);
107   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]);
108   return 0;
109 }
110 
111 #else
112 #undef __FUNCT__
113 #define __FUNCT__ "PetscStackPublish"
114 PetscErrorCode  PetscStackPublish(void)
115 {
116   PetscFunctionBegin;
117   PetscFunctionReturn(0);
118 }
119 #undef __FUNCT__
120 #define __FUNCT__ "PetscStackDepublish"
121 PetscErrorCode  PetscStackDepublish(void)
122 {
123   PetscFunctionBegin;
124   PetscFunctionReturn(0);
125 }
126 #undef __FUNCT__
127 #define __FUNCT__ "PetscStackCreate"
128 PetscErrorCode  PetscStackCreate(void)
129 {
130   PetscFunctionBegin;
131   PetscFunctionReturn(0);
132 }
133 #undef __FUNCT__
134 #define __FUNCT__ "PetscStackView"
135 PetscErrorCode  PetscStackView(FILE *file)
136 {
137   PetscFunctionBegin;
138   PetscFunctionReturn(0);
139 }
140 #undef __FUNCT__
141 #define __FUNCT__ "PetscStackDestroy"
142 PetscErrorCode  PetscStackDestroy(void)
143 {
144   PetscFunctionBegin;
145   PetscFunctionReturn(0);
146 }
147 
148 #endif
149 
150