1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: sdb.cc,v 1.5 1999-05-17 13:34:03 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: Singular debugger |
---|
7 | */ |
---|
8 | |
---|
9 | #include <unistd.h> // for unlink,fork,execlp,getpid |
---|
10 | #include <sys/wait.h> // for wait |
---|
11 | #include "mod2.h" |
---|
12 | #include "tok.h" |
---|
13 | #include "mmemory.h" |
---|
14 | #include "febase.h" |
---|
15 | #include "ipshell.h" |
---|
16 | #include "ipid.h" |
---|
17 | #include "sdb.h" |
---|
18 | |
---|
19 | int sdb_lines[]={-1,-1,-1,-1,-1,-1,-1,-1}; |
---|
20 | char * sdb_files[6]; |
---|
21 | int sdb_flags=0; |
---|
22 | |
---|
23 | int sdb_checkline(char f) |
---|
24 | { |
---|
25 | int i; |
---|
26 | char ff=f>>1; |
---|
27 | for(i=0;i<7;i++) |
---|
28 | { |
---|
29 | if((ff & 1) && (yylineno==sdb_lines[i])) |
---|
30 | return i+1; |
---|
31 | ff>>=1; |
---|
32 | if (ff==0) return 0; |
---|
33 | } |
---|
34 | return 0; |
---|
35 | } |
---|
36 | |
---|
37 | void sdb_edit(procinfo *pi) |
---|
38 | { |
---|
39 | char * filename = "/tmp/sd000000"; |
---|
40 | sprintf(filename+7,"%d",getpid()); |
---|
41 | FILE *fp=fopen(filename,"w"); |
---|
42 | if (fp==NULL) |
---|
43 | { |
---|
44 | Print("cannot open %s\n",filename); |
---|
45 | return; |
---|
46 | } |
---|
47 | if (pi->language!= LANG_SINGULAR) |
---|
48 | { |
---|
49 | Print("cannot edit type %d\n",pi->language); |
---|
50 | fclose(fp); |
---|
51 | fp=NULL; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | char *editor=getenv("EDITOR"); |
---|
56 | if (editor==NULL) |
---|
57 | editor=getenv("VISUAL"); |
---|
58 | if (editor==NULL) |
---|
59 | editor="vi"; |
---|
60 | |
---|
61 | if (pi->data.s.body==NULL) |
---|
62 | { |
---|
63 | iiGetLibProcBuffer(pi); |
---|
64 | if (pi->data.s.body==NULL) |
---|
65 | { |
---|
66 | PrintS("cannot get the procedure body\n"); |
---|
67 | fclose(fp); |
---|
68 | unlink(filename); |
---|
69 | return; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | fwrite(pi->data.s.body,1,strlen(pi->data.s.body),fp); |
---|
74 | fclose(fp); |
---|
75 | |
---|
76 | int pid=fork(); |
---|
77 | if (pid!=0) |
---|
78 | { |
---|
79 | wait(&pid); |
---|
80 | } |
---|
81 | else if(pid==0) |
---|
82 | { |
---|
83 | execlp(editor,editor,filename,NULL); |
---|
84 | Print("cannot exec %s\n",editor); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | PrintS("cannot fork\n"); |
---|
89 | } |
---|
90 | |
---|
91 | fp=fopen(filename,"r"); |
---|
92 | if (fp==NULL) |
---|
93 | { |
---|
94 | Print("cannot read from %s\n",filename); |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | fseek(fp,0L,SEEK_END); |
---|
99 | long len=ftell(fp); |
---|
100 | fseek(fp,0L,SEEK_SET); |
---|
101 | |
---|
102 | FreeL((ADDRESS)pi->data.s.body); |
---|
103 | pi->data.s.body=(char *)AllocL((int)len+1); |
---|
104 | myfread( pi->data.s.body, len, 1, fp); |
---|
105 | pi->data.s.body[len]='\0'; |
---|
106 | fclose(fp); |
---|
107 | } |
---|
108 | } |
---|
109 | unlink(filename); |
---|
110 | } |
---|
111 | |
---|
112 | static char *sdb_find_arg(char *p) |
---|
113 | { |
---|
114 | p++; |
---|
115 | while (*p==' ') p++; |
---|
116 | char *pp=p; |
---|
117 | while (*pp>' ') pp++; |
---|
118 | *pp='\0'; |
---|
119 | return p; |
---|
120 | } |
---|
121 | |
---|
122 | static char sdb_lastcmd='c'; |
---|
123 | |
---|
124 | void sdb(Voice * currentVoice, const char * currLine, int len) |
---|
125 | { |
---|
126 | int bp=0; |
---|
127 | if ((len>1) |
---|
128 | && ((currentVoice->pi->trace_flag & 1) |
---|
129 | || (bp=sdb_checkline(currentVoice->pi->trace_flag))) |
---|
130 | ) |
---|
131 | { |
---|
132 | loop |
---|
133 | { |
---|
134 | char gdb[80]; |
---|
135 | char *p=(char *)currLine+len-1; |
---|
136 | while ((*p<=' ') && (p!=currLine)) |
---|
137 | { |
---|
138 | p--; len--; |
---|
139 | } |
---|
140 | if (p==currLine) return; |
---|
141 | |
---|
142 | currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines" |
---|
143 | Print("(%s,%d) >>",currentVoice->filename,yylineno); |
---|
144 | fwrite(currLine,1,len,stdout); |
---|
145 | Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp); |
---|
146 | p=fe_fgets_stdin("!",gdb,80); |
---|
147 | while (*p==' ') p++; |
---|
148 | if (*p >' ') |
---|
149 | { |
---|
150 | sdb_lastcmd=*p; |
---|
151 | } |
---|
152 | Print("command:%c\n",sdb_lastcmd); |
---|
153 | switch(sdb_lastcmd) |
---|
154 | { |
---|
155 | case '?': |
---|
156 | case 'h': |
---|
157 | { |
---|
158 | PrintS( |
---|
159 | "b - print backtrace of calling stack\n" |
---|
160 | "c - continue\n" |
---|
161 | "d - delete current breakpoint\n" |
---|
162 | "e - edit the current procedure (current call will be aborted)\n" |
---|
163 | "h,? - display this help screen\n" |
---|
164 | "n - execute current line, break at next line\n" |
---|
165 | "p <var> - display type and value of the variable <var>\n" |
---|
166 | "q <flags> - quit debugger, set debugger flags(0,1,2)\n" |
---|
167 | "Q - quit Singular\n"); |
---|
168 | int i; |
---|
169 | for(i=0;i<7;i++) |
---|
170 | { |
---|
171 | if (sdb_lines[i] != -1) |
---|
172 | Print("breakpoint %d at line %d in %s\n", |
---|
173 | i,sdb_lines[i],sdb_files[i]); |
---|
174 | } |
---|
175 | break; |
---|
176 | } |
---|
177 | case 'd': |
---|
178 | { |
---|
179 | Print("delete break point %d\n",bp); |
---|
180 | currentVoice->pi->trace_flag &= (~Sy_bit(bp)); |
---|
181 | if (bp!=0) |
---|
182 | { |
---|
183 | sdb_lines[bp-1]=-1; |
---|
184 | } |
---|
185 | break; |
---|
186 | } |
---|
187 | case 'n': |
---|
188 | currentVoice->pi->trace_flag|= 1; |
---|
189 | return; |
---|
190 | case 'e': |
---|
191 | { |
---|
192 | sdb_edit(currentVoice->pi); |
---|
193 | sdb_flags=2; |
---|
194 | return; |
---|
195 | } |
---|
196 | case 'p': |
---|
197 | { |
---|
198 | p=sdb_find_arg(p); |
---|
199 | Print("request `%s`",p); |
---|
200 | idhdl h=ggetid(p,TRUE); |
---|
201 | if (h==NULL) |
---|
202 | PrintS("NULL\n"); |
---|
203 | else |
---|
204 | { |
---|
205 | sleftv tmp; |
---|
206 | memset(&tmp,0,sizeof(tmp)); |
---|
207 | tmp.rtyp=IDHDL; |
---|
208 | tmp.data=h; |
---|
209 | Print("(type %s):",Tok2Cmdname(tmp.Typ())); |
---|
210 | tmp.Print(); |
---|
211 | } |
---|
212 | break; |
---|
213 | } |
---|
214 | case 'b': |
---|
215 | VoiceBackTrack(); |
---|
216 | break; |
---|
217 | case 'q': |
---|
218 | { |
---|
219 | p=sdb_find_arg(p); |
---|
220 | if (*p!='\0') |
---|
221 | { |
---|
222 | sdb_flags=atoi(p); |
---|
223 | Print("new sdb_flags:%d\n",sdb_flags); |
---|
224 | } |
---|
225 | return; |
---|
226 | } |
---|
227 | case 'Q': |
---|
228 | m2_end(999); |
---|
229 | case 'c': |
---|
230 | default: |
---|
231 | return; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|