My Project  debian-1:4.1.2-p1+ds-2
Functions | Variables
sdb.cc File Reference
#include "kernel/mod2.h"
#include "omalloc/omalloc.h"
#include "misc/options.h"
#include "reporter/si_signals.h"
#include "Singular/tok.h"
#include "Singular/ipshell.h"
#include "Singular/ipid.h"
#include "Singular/fevoices.h"
#include "kernel/oswrapper/feread.h"
#include "Singular/sdb.h"
#include <unistd.h>
#include <sys/wait.h>

Go to the source code of this file.

Functions

int sdb_checkline (char f)
 
static char * sdb_find_arg (char *p)
 
void sdb_show_bp ()
 
BOOLEAN sdb_set_breakpoint (const char *pp, int given_lineno)
 
void sdb_edit (procinfo *pi)
 
void sdb (Voice *currentVoice, const char *currLine, int len)
 

Variables

VAR int sdb_lines [] ={-1,-1,-1,-1,-1,-1,-1,-1}
 
VAR char * sdb_files [6]
 
VAR int sdb_flags =0
 
STATIC_VAR char sdb_lastcmd ='c'
 

Function Documentation

◆ sdb()

void sdb ( Voice currentVoice,
const char *  currLine,
int  len 
)

Definition at line 200 of file sdb.cc.

201 {
202  int bp=0;
203  if ((len>1)
204  && ((currentVoice->pi->trace_flag & 1)
206  )
207  {
208  loop
209  {
210  char gdb[80];
211  char *p=(char *)currLine+len-1;
212  while ((*p<=' ') && (p!=currLine))
213  {
214  p--; len--;
215  }
216  if (p==currLine) return;
217 
218  currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines"
219  Print("(%s,%d) >>",currentVoice->filename,yylineno);
220  fwrite(currLine,1,len,stdout);
221  Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp);
222  p=fe_fgets_stdin(">>",gdb,80);
223  while (*p==' ') p++;
224  if (*p >' ')
225  {
226  sdb_lastcmd=*p;
227  }
228  Print("command:%c\n",sdb_lastcmd);
229  switch(sdb_lastcmd)
230  {
231  case '?':
232  case 'h':
233  {
234  PrintS(
235  "b - print backtrace of calling stack\n"
236  "B <proc> [<line>] - define breakpoint\n"
237  "c - continue\n"
238  "d - delete current breakpoint\n"
239  "D - show all breakpoints\n"
240  "e - edit the current procedure (current call will be aborted)\n"
241  "h,? - display this help screen\n"
242  "n - execute current line, break at next line\n"
243  "p <var> - display type and value of the variable <var>\n"
244  "q <flags> - quit debugger, set debugger flags(0,1,2)\n"
245  " 0: stop debug, 1:continue, 2: throw an error, return to toplevel\n"
246  "Q - quit Singular\n");
247  int i;
248  for(i=0;i<7;i++)
249  {
250  if (sdb_lines[i] != -1)
251  Print("breakpoint %d at line %d in %s\n",
252  i,sdb_lines[i],sdb_files[i]);
253  }
254  break;
255  }
256  case 'd':
257  {
258  Print("delete break point %d\n",bp);
259  currentVoice->pi->trace_flag &= (~Sy_bit(bp));
260  if (bp!=0)
261  {
262  sdb_lines[bp-1]=-1;
263  }
264  break;
265  }
266  case 'D':
267  sdb_show_bp();
268  break;
269  #if 0
270  case 'l':
271  {
272  extern void listall(int showproc);
273  listall(FALSE);
274  break;
275  }
276  #endif
277  case 'n':
278  currentVoice->pi->trace_flag|= 1;
279  return;
280  case 'e':
281  {
283  sdb_flags=2;
284  return;
285  }
286  case 'p':
287  {
288  p=sdb_find_arg(p);
289  EXTERN_VAR int myynest;
290  Print("variable `%s`at level %d",p,myynest);
291  idhdl h=ggetid(p);
292  if (h==NULL)
293  PrintS(" not found\n");
294  else
295  {
296  sleftv tmp;
297  memset(&tmp,0,sizeof(tmp));
298  tmp.rtyp=IDHDL;
299  tmp.data=h;
300  Print("(type %s):\n",Tok2Cmdname(tmp.Typ()));
301  tmp.Print();
302  }
303  break;
304  }
305  case 'b':
306  VoiceBackTrack();
307  break;
308  case 'B':
309  {
310  p=sdb_find_arg(p);
311  Print("procedure `%s` ",p);
313  break;
314  }
315  case 'q':
316  {
317  p=sdb_find_arg(p);
318  if (*p!='\0')
319  {
320  sdb_flags=atoi(p);
321  Print("new sdb_flags:%d\n",sdb_flags);
322  }
323  return;
324  }
325  case 'Q':
326  m2_end(999);
327  case 'c':
328  default:
329  return;
330  }
331  }
332  }
333 }
#define FALSE
Definition: auxiliary.h:96
int i
Definition: cfEzgcd.cc:125
int p
Definition: cfModGcd.cc:4019
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
int Typ()
Definition: subexpr.cc:1033
int rtyp
Definition: subexpr.h:91
void * data
Definition: subexpr.h:88
void Print(leftv store=NULL, int spaces=0)
Called by type_cmd (e.g. "r;") or as default in jPRINT.
Definition: subexpr.cc:63
#define Print
Definition: emacs.cc:80
VAR int yylineno
Definition: febase.cc:40
VAR int myynest
Definition: febase.cc:41
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:34
VAR Voice * currentVoice
Definition: fevoices.cc:47
void VoiceBackTrack()
Definition: fevoices.cc:67
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:140
#define EXTERN_VAR
Definition: globaldefs.h:6
idhdl ggetid(const char *n)
Definition: ipid.cc:521
STATIC_VAR Poly * h
Definition: janet.cc:971
void m2_end(int i)
Definition: misc_ip.cc:1096
#define NULL
Definition: omList.c:12
#define Sy_bit(x)
Definition: options.h:31
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
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:198
#define loop
Definition: structs.h:80
char trace_flag
Definition: subexpr.h:62
#define IDHDL
Definition: tok.h:31

◆ sdb_checkline()

int sdb_checkline ( char  f)

Definition at line 33 of file sdb.cc.

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 }
FILE * f
Definition: checklibs.c:9

◆ sdb_edit()

void sdb_edit ( procinfo pi)

Definition at line 109 of file sdb.cc.

110 {
111  char * filename = omStrDup("/tmp/sd000000");
112  sprintf(filename+7,"%d",getpid());
113  FILE *fp=fopen(filename,"w");
114  if (fp==NULL)
115  {
116  Print("cannot open %s\n",filename);
117  omFree(filename);
118  return;
119  }
120  if (pi->language!= LANG_SINGULAR)
121  {
122  Print("cannot edit type %d\n",pi->language);
123  fclose(fp);
124  fp=NULL;
125  }
126  else
127  {
128  const char *editor=getenv("EDITOR");
129  if (editor==NULL)
130  editor=getenv("VISUAL");
131  if (editor==NULL)
132  editor="vi";
133  editor=omStrDup(editor);
134 
135  if (pi->data.s.body==NULL)
136  {
138  if (pi->data.s.body==NULL)
139  {
140  PrintS("cannot get the procedure body\n");
141  fclose(fp);
142  si_unlink(filename);
143  omFree(filename);
144  return;
145  }
146  }
147 
148  fwrite(pi->data.s.body,1,strlen(pi->data.s.body),fp);
149  fclose(fp);
150 
151  int pid=fork();
152  if (pid!=0)
153  {
154  si_wait(&pid);
155  }
156  else if(pid==0)
157  {
158  if (strchr(editor,' ')==NULL)
159  {
160  execlp(editor,editor,filename,NULL);
161  Print("cannot exec %s\n",editor);
162  }
163  else
164  {
165  char *p=(char *)omAlloc(strlen(editor)+strlen(filename)+2);
166  sprintf(p,"%s %s",editor,filename);
167  system(p);
168  }
169  exit(0);
170  }
171  else
172  {
173  PrintS("cannot fork\n");
174  }
175 
176  fp=fopen(filename,"r");
177  if (fp==NULL)
178  {
179  Print("cannot read from %s\n",filename);
180  }
181  else
182  {
183  fseek(fp,0L,SEEK_END);
184  long len=ftell(fp);
185  fseek(fp,0L,SEEK_SET);
186 
187  omFree((ADDRESS)pi->data.s.body);
188  pi->data.s.body=(char *)omAlloc((int)len+1);
189  myfread( pi->data.s.body, len, 1, fp);
190  pi->data.s.body[len]='\0';
191  fclose(fp);
192  }
193  }
194  si_unlink(filename);
195  omFree(filename);
196 }
void * ADDRESS
Definition: auxiliary.h:135
CanonicalForm fp
Definition: cfModGcd.cc:4043
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: feFopen.cc:195
char * getenv()
void system(sys)
char * iiGetLibProcBuffer(procinfo *pi, int part)
Definition: iplib.cc:193
#define pi
Definition: libparse.cc:1145
#define SEEK_SET
Definition: mod2.h:116
#define SEEK_END
Definition: mod2.h:112
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omFree(addr)
Definition: omAllocDecl.h:261
@ LANG_SINGULAR
Definition: subexpr.h:22

◆ sdb_find_arg()

static char* sdb_find_arg ( char *  p)
static

Definition at line 47 of file sdb.cc.

48 {
49  p++;
50  while (*p==' ') p++;
51  char *pp=p;
52  while (*pp>' ') pp++;
53  *pp='\0';
54  return p;
55 }
CanonicalForm pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f )
Definition: cf_gcd.cc:248

◆ sdb_set_breakpoint()

BOOLEAN sdb_set_breakpoint ( const char *  pp,
int  given_lineno 
)

Definition at line 64 of file sdb.cc.

65 {
66  idhdl h=ggetid(pp);
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;
83  if (given_lineno >0) lineno=given_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  }
100  sdb_lines[i]=lineno;
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 }
#define TRUE
Definition: auxiliary.h:100
@ PROC_CMD
Definition: grammar.cc:280
#define IDDATA(a)
Definition: ipid.h:121
#define IDTYP(a)
Definition: ipid.h:114
procinfo * procinfov
Definition: structs.h:65

◆ sdb_show_bp()

void sdb_show_bp ( )

Definition at line 57 of file sdb.cc.

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 }

Variable Documentation

◆ sdb_files

VAR char* sdb_files[6]

Definition at line 30 of file sdb.cc.

◆ sdb_flags

VAR int sdb_flags =0

Definition at line 31 of file sdb.cc.

◆ sdb_lastcmd

STATIC_VAR char sdb_lastcmd ='c'

Definition at line 198 of file sdb.cc.

◆ sdb_lines

VAR int sdb_lines[] ={-1,-1,-1,-1,-1,-1,-1,-1}

Definition at line 29 of file sdb.cc.