My Project
Loading...
Searching...
No Matches
sdb.cc
Go to the documentation of this file.
1/****************************************
2* Computer Algebra System SINGULAR *
3****************************************/
4/*
5* ABSTRACT: Singular debugger
6*/
7
8#include "kernel/mod2.h"
9#include "omalloc/omalloc.h"
10#include "misc/options.h"
11#include "reporter/si_signals.h"
12#include "Singular/tok.h"
13#include "Singular/ipshell.h"
14#include "Singular/ipid.h"
15#include "Singular/fevoices.h"
17#include "Singular/sdb.h"
18
19#include <unistd.h> // for unlink,fork,execlp,getpid
20#include <sys/wait.h> // for wait
21
22
23#ifdef HAVE_SDB
24// We use 8 breakpoints - corresponding to a bit in a char variable in procinfo
25// bit 1..7 force a breakpoint, if lineno==sdb_lines[i-1],
26// (for displaying only: file sdb_files[i-1])
27// bit 0 force a breakpoint in every line (used for 'n')
28
29VAR int sdb_lines[]={-1,-1,-1,-1,-1,-1,-1,-1};
30VAR char * sdb_files[6];
32
34{
35 int i;
36 char ff=f>>1;
37 for(i=0;i<7;i++)
38 {
39 if((ff & 1) && (yylineno==sdb_lines[i]))
40 return i+1;
41 ff>>=1;
42 if (ff==0) return 0;
43 }
44 return 0;
45}
46
47static char *sdb_find_arg(char *p)
48{
49 p++;
50 while (*p==' ') p++;
51 char *pp=p;
52 while (*pp>' ') pp++;
53 *pp='\0';
54 return p;
55}
56
58{
59 for(int i=0; i<7;i++)
60 if (sdb_lines[i]!= -1)
61 Print("Breakpoint %d: %s::%d\n",i+1,sdb_files[i],sdb_lines[i]);
62}
63
65{
67 if ((h==NULL)||(IDTYP(h)!=PROC_CMD))
68 {
69 PrintS(" not found\n");
70 return TRUE;
71 }
72 else
73 {
75 #ifdef HAVE_DYNAMIC_LOADING
76 if (p->language!=LANG_SINGULAR)
77 {
78 PrintS("is not a Singular procedure\n");
79 return TRUE;
80 }
81 #endif
82 int lineno;
84 else lineno=p->data.s.body_lineno;
85 int i;
86 if (given_lineno== -1)
87 {
88 i=p->trace_flag;
89 p->trace_flag &=1;
90 Print("breakpoints in %s deleted(%#x)\n",p->procname,i &255);
91 return FALSE;
92 }
93 i=0;
94 while((i<7) && (sdb_lines[i]!=-1)) i++;
95 if (sdb_lines[i]!= -1)
96 {
97 PrintS("too many breakpoints set, max is 7\n");
98 return TRUE;
99 }
101 sdb_files[i]=p->libname;
102 i++;
103 p->trace_flag|=(1<<i);
104 Print("breakpoint %d, at line %d in %s\n",i,lineno,p->procname);
105 return FALSE;
106 }
107}
108
110{
111 char * filename = omStrDup("/tmp/sdXXXXXX");
112 int f=mkstemp(filename);
113 if (f==-1)
114 {
115 Print("cannot open %s\n",filename);
116 omFree(filename);
117 return;
118 }
119 if (pi->language!= LANG_SINGULAR)
120 {
121 Print("cannot edit type %d\n",pi->language);
122 close(f);
123 }
124 else
125 {
126 const char *editor=getenv("EDITOR");
127 if (editor==NULL)
128 editor=getenv("VISUAL");
129 if (editor==NULL)
130 editor="vi";
132
133 if (pi->data.s.body==NULL)
134 {
136 if (pi->data.s.body==NULL)
137 {
138 PrintS("cannot get the procedure body\n");
139 close(f);
140 si_unlink(filename);
141 omFree(filename);
142 return;
143 }
144 }
145
146 write(f,pi->data.s.body,strlen(pi->data.s.body));
147 close(f);
148
149 int pid=fork();
150 if (pid!=0)
151 {
152 si_wait(&pid);
153 }
154 else if(pid==0)
155 {
156 if (strchr(editor,' ')==NULL)
157 {
158 execlp(editor,editor,filename,NULL);
159 Print("cannot exec %s\n",editor);
160 }
161 else
162 {
163 size_t len=strlen(editor)+strlen(filename)+2;
164 char *p=(char *)omAlloc(len);
165 snprintf(p,len,"%s %s",editor,filename);
166 system(p);
167 }
168 exit(0);
169 }
170 else
171 {
172 PrintS("cannot fork\n");
173 }
174
175 FILE* fp=fopen(filename,"r");
176 if (fp==NULL)
177 {
178 Print("cannot read from %s\n",filename);
179 }
180 else
181 {
182 fseek(fp,0L,SEEK_END);
183 long len=ftell(fp);
184 fseek(fp,0L,SEEK_SET);
185
186 omFree((ADDRESS)pi->data.s.body);
187 pi->data.s.body=(char *)omAlloc((int)len+1);
188 myfread( pi->data.s.body, len, 1, fp);
189 pi->data.s.body[len]='\0';
190 fclose(fp);
191 }
192 }
193 si_unlink(filename);
194 omFree(filename);
195}
196
198
199void sdb(Voice * currentVoice, const char * currLine, int len)
200{
201 int bp=0;
202 if ((len>1)
203 && ((currentVoice->pi->trace_flag & 1)
205 )
206 {
207 loop
208 {
209 char gdb[80];
210 char *p=(char *)currLine+len-1;
211 while ((*p<=' ') && (p!=currLine))
212 {
213 p--; len--;
214 }
215 if (p==currLine) return;
216
217 currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines"
218 Print("(%s,%d) >>",currentVoice->filename,yylineno);
219 fwrite(currLine,1,len,stdout);
220 Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp);
221 p=fe_fgets_stdin(">>",gdb,80);
222 while (*p==' ') p++;
223 if (*p >' ')
224 {
225 sdb_lastcmd=*p;
226 }
227 Print("command:%c\n",sdb_lastcmd);
228 switch(sdb_lastcmd)
229 {
230 case '?':
231 case 'h':
232 {
233 PrintS(
234 "b - print backtrace of calling stack\n"
235 "B <proc> [<line>] - define breakpoint\n"
236 "c - continue\n"
237 "d - delete current breakpoint\n"
238 "D - show all breakpoints\n"
239 "e - edit the current procedure (current call will be aborted)\n"
240 "h,? - display this help screen\n"
241 "n - execute current line, break at next line\n"
242 "p <var> - display type and value of the variable <var>\n"
243 "q <flags> - quit debugger, set debugger flags(0,1,2)\n"
244 " 0: stop debug, 1:continue, 2: throw an error, return to toplevel\n"
245 "Q - quit Singular\n");
246 int i;
247 for(i=0;i<7;i++)
248 {
249 if (sdb_lines[i] != -1)
250 Print("breakpoint %d at line %d in %s\n",
252 }
253 break;
254 }
255 case 'd':
256 {
257 Print("delete break point %d\n",bp);
258 currentVoice->pi->trace_flag &= (~Sy_bit(bp));
259 if (bp!=0)
260 {
261 sdb_lines[bp-1]=-1;
262 }
263 break;
264 }
265 case 'D':
266 sdb_show_bp();
267 break;
268 #if 0
269 case 'l':
270 {
271 extern void listall(int showproc);
272 listall(FALSE);
273 break;
274 }
275 #endif
276 case 'n':
278 return;
279 case 'e':
280 {
282 sdb_flags=2;
283 return;
284 }
285 case 'p':
286 {
289 Print("variable `%s`at level %d",p,myynest);
290 idhdl h=ggetid(p);
291 if (h==NULL)
292 PrintS(" not found\n");
293 else
294 {
295 sleftv tmp;
296 memset(&tmp,0,sizeof(tmp));
297 tmp.rtyp=IDHDL;
298 tmp.data=h;
299 Print("(type %s):\n",Tok2Cmdname(tmp.Typ()));
300 tmp.Print();
301 }
302 break;
303 }
304 case 'b':
306 break;
307 case 'B':
308 {
310 Print("procedure `%s` ",p);
312 break;
313 }
314 case 'q':
315 {
317 if (*p!='\0')
318 {
320 Print("new sdb_flags:%d\n",sdb_flags);
321 }
322 return;
323 }
324 case 'Q':
325 m2_end(999);
326 case 'c':
327 default:
328 return;
329 }
330 }
331 }
332}
333#endif
int BOOLEAN
Definition auxiliary.h:87
#define TRUE
Definition auxiliary.h:100
#define FALSE
Definition auxiliary.h:96
CanonicalForm FACTORY_PUBLIC pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f )
Definition cf_gcd.cc:676
int i
Definition cfEzgcd.cc:132
int p
Definition cfModGcd.cc:4079
CanonicalForm fp
Definition cfModGcd.cc:4103
FILE * f
Definition checklibs.c:9
procinfo * pi
Definition fevoices.h:64
char * filename
Definition fevoices.h:63
Definition idrec.h:35
Class used for (list of) interpreter objects.
Definition subexpr.h:83
#define Print
Definition emacs.cc:80
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition feFopen.cc:195
VAR int yylineno
Definition febase.cc:40
VAR int myynest
Definition febase.cc:41
char * getenv()
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition feread.cc:32
VAR Voice * currentVoice
Definition fevoices.cc:49
void VoiceBackTrack()
Definition fevoices.cc:77
const char * Tok2Cmdname(int tok)
Definition gentable.cc:140
#define STATIC_VAR
Definition globaldefs.h:7
#define EXTERN_VAR
Definition globaldefs.h:6
#define VAR
Definition globaldefs.h:5
@ PROC_CMD
Definition grammar.cc:280
idhdl ggetid(const char *n)
Definition ipid.cc:581
#define IDDATA(a)
Definition ipid.h:126
#define IDTYP(a)
Definition ipid.h:119
char * iiGetLibProcBuffer(procinfo *pi, int part)
Definition iplib.cc:197
STATIC_VAR Poly * h
Definition janet.cc:971
#define pi
Definition libparse.cc:1145
#define SEEK_SET
Definition mod2.h:115
#define SEEK_END
Definition mod2.h:111
void m2_end(int i)
Definition misc_ip.cc:1100
#define omStrDup(s)
#define omAlloc(size)
#define omFree(addr)
#define NULL
Definition omList.c:12
void PrintS(const char *s)
Definition reporter.cc:284
BOOLEAN sdb_set_breakpoint(const char *pp, int given_lineno)
Definition sdb.cc:64
VAR int sdb_flags
Definition sdb.cc:31
void sdb_edit(procinfo *pi)
Definition sdb.cc:109
VAR char * sdb_files[6]
Definition sdb.cc:30
void sdb(Voice *currentVoice, const char *currLine, int len)
Definition sdb.cc:199
static char * sdb_find_arg(char *p)
Definition sdb.cc:47
void sdb_show_bp()
Definition sdb.cc:57
VAR int sdb_lines[]
Definition sdb.cc:29
int sdb_checkline(char f)
Definition sdb.cc:33
STATIC_VAR char sdb_lastcmd
Definition sdb.cc:197
int status int void size_t count write
Definition si_signals.h:67
#define loop
Definition structs.h:75
procinfo * procinfov
Definition structs.h:60
char trace_flag
Definition subexpr.h:62
@ LANG_SINGULAR
Definition subexpr.h:22
#define IDHDL
Definition tok.h:31