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