Loading...
Searching...
No Matches
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 210 of file sdb.cc.

{
int bp=0;
if ((len>1)
&& ((currentVoice->pi->trace_flag & 1)
|| (bp=sdb_checkline(currentVoice->pi->trace_flag)))
)
{
loop
{
char gdb[80];
char *p=(char *)currLine+len-1;
while ((*p<=' ') && (p!=currLine))
{
p--; len--;
}
if (p==currLine) return;
currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines"
Print("(%s,%d) >>",currentVoice->filename,yylineno);
fwrite(currLine,1,len,stdout);
Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp);
p=fe_fgets_stdin(">>",gdb,80);
while (*p==' ') p++;
if (*p >' ')
{
sdb_lastcmd=*p;
}
Print("command:%c\n",sdb_lastcmd);
switch(sdb_lastcmd)
{
case '?':
case 'h':
{
PrintS(
"b - print backtrace of calling stack\n"
"B <proc> [<line>] - define breakpoint\n"
"c - continue\n"
"d - delete current breakpoint\n"
"D - show all breakpoints\n"
"e - edit the current procedure (current call will be aborted)\n"
"h,? - display this help screen\n"
"n - execute current line, break at next line\n"
"p <var> - display type and value of the variable <var>\n"
"q <flags> - quit debugger, set debugger flags(0,1,2)\n"
" 0: stop debug, 1:continue, 2: throw an error, return to toplevel\n"
"Q - quit Singular\n");
int i;
for(i=0;i<7;i++)
{
if (sdb_lines[i] != -1)
Print("breakpoint %d at line %d in %s\n",
i,sdb_lines[i],sdb_files[i]);
}
break;
}
case 'd':
{
Print("delete break point %d\n",bp);
currentVoice->pi->trace_flag &= (~Sy_bit(bp));
if (bp!=0)
{
sdb_lines[bp-1]=-1;
}
break;
}
case 'D':
sdb_show_bp();
break;
#if 0
case 'l':
{
extern void listall(int showproc);
listall(FALSE);
break;
}
#endif
case 'n':
currentVoice->pi->trace_flag|= 1;
return;
case 'e':
{
sdb_edit(currentVoice->pi);
sdb_flags=2;
return;
}
case 'p':
{
p=sdb_find_arg(p);
EXTERN_VAR int myynest;
Print("variable `%s`at level %d",p,myynest);
idhdl h=ggetid(p);
if (h==NULL)
PrintS(" not found\n");
else
{
sleftv tmp;
memset(&tmp,0,sizeof(tmp));
tmp.rtyp=IDHDL;
tmp.data=h;
Print("(type %s):\n",Tok2Cmdname(tmp.Typ()));
tmp.Print();
}
break;
}
case 'b':
VoiceBackTrack();
break;
case 'B':
{
p=sdb_find_arg(p);
Print("procedure `%s` ",p);
sdb_set_breakpoint(p);
break;
}
case 'q':
{
p=sdb_find_arg(p);
if (*p!='\0')
{
sdb_flags=atoi(p);
Print("new sdb_flags:%d\n",sdb_flags);
}
return;
}
case 'Q':
m2_end(999);
case 'c':
default:
return;
}
}
}
}
FALSE
#define FALSE
Definition auxiliary.h:97
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
sleftv
Class used for (list of) interpreter objects.
Definition subexpr.h:83
sleftv::Typ
int Typ()
Definition subexpr.cc:1048
sleftv::rtyp
int rtyp
Definition subexpr.h:91
sleftv::data
void * data
Definition subexpr.h:88
sleftv::Print
void Print(leftv store=NULL, int spaces=0)
Called by type_cmd (e.g. "r;") or as default in jPRINT.
Definition subexpr.cc:63
Print
#define Print
Definition emacs.cc:80
yylineno
VAR int yylineno
Definition febase.cc:40
myynest
VAR int myynest
Definition febase.cc:41
fe_fgets_stdin
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition feread.cc:32
currentVoice
VAR Voice * currentVoice
Definition fevoices.cc:49
VoiceBackTrack
void VoiceBackTrack()
Definition fevoices.cc:77
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition gentable.cc:137
EXTERN_VAR
#define EXTERN_VAR
Definition globaldefs.h:6
ggetid
idhdl ggetid(const char *n)
Definition ipid.cc:581
h
STATIC_VAR Poly * h
Definition janet.cc:971
m2_end
void m2_end(int i)
Definition misc_ip.cc:1104
NULL
#define NULL
Definition omList.c:12
Sy_bit
#define Sy_bit(x)
Definition options.h:31
PrintS
void PrintS(const char *s)
Definition reporter.cc:288
idhdl
idrec * idhdl
Definition ring.h:22
sdb_set_breakpoint
BOOLEAN sdb_set_breakpoint(const char *pp, int given_lineno)
Definition sdb.cc:64
sdb_flags
VAR int sdb_flags
Definition sdb.cc:31
sdb_edit
void sdb_edit(procinfo *pi)
Definition sdb.cc:109
sdb_files
VAR char * sdb_files[6]
Definition sdb.cc:30
sdb_find_arg
static char * sdb_find_arg(char *p)
Definition sdb.cc:47
sdb_show_bp
void sdb_show_bp()
Definition sdb.cc:57
sdb_lines
VAR int sdb_lines[]
Definition sdb.cc:29
sdb_checkline
int sdb_checkline(char f)
Definition sdb.cc:33
sdb_lastcmd
STATIC_VAR char sdb_lastcmd
Definition sdb.cc:208
loop
#define loop
Definition structs.h:71
IDHDL
#define IDHDL
Definition tok.h:31

## sdb_checkline()

int sdb_checkline | ( | char | f | ) |

Definition at line 33 of file sdb.cc.

{
int i;
char ff=f>>1;
for(i=0;i<7;i++)
{
if((ff & 1) && (yylineno==sdb_lines[i]))
return i+1;
ff>>=1;
if (ff==0) return 0;
}
return 0;
}
f
FILE * f
Definition checklibs.c:9

## sdb_edit()

void sdb_edit | ( | procinfo * | pi | ) |

Definition at line 109 of file sdb.cc.

{
char * filename = omStrDup("/tmp/sdXXXXXX");
int f=mkstemp(filename);
if (f==-1)
{
Print("cannot open %s\n",filename);
omFree(filename);
return;
}
if (pi->language!= LANG_SINGULAR)
{
Print("cannot edit type %d\n",pi->language);
si_close(f);
}
else
{
const char *editor=getenv("EDITOR");
if (editor==NULL)
editor=getenv("VISUAL");
if (editor==NULL)
editor="vi";
editor=omStrDup(editor);
if (pi->data.s.body==NULL)
{
iiGetLibProcBuffer(pi);
if (pi->data.s.body==NULL)
{
PrintS("cannot get the procedure body\n");
si_close(f);
si_unlink(filename);
omFree(filename);
return;
}
}
ssize_t res=write(f,pi->data.s.body,strlen(pi->data.s.body));
si_close(f);
if (res==-1)
{
PrintS("cannot write the procedure body\n");
si_unlink(filename);
omFree(filename);
return;
}
int pid=fork();
if (pid!=0)
{
si_wait(&pid);
}
else if(pid==0)
{
if (strchr(editor,' ')==NULL)
{
execlp(editor,editor,filename,NULL);
Print("cannot exec %s\n",editor);
}
else
{
size_t len=strlen(editor)+strlen(filename)+2;
char *p=(char *)omAlloc(len);
snprintf(p,len,"%s %s",editor,filename);
int res=system(p);
if (res<0)
{
Print("cannot call `%s`\n",editor);
}
}
exit(0);
}
else
{
PrintS("cannot fork\n");
}
FILE* fp=fopen(filename,"r");
if (fp==NULL)
{
Print("cannot read from %s\n",filename);
}
else
{
fseek(fp,0L,SEEK_END);
long len=ftell(fp);
fseek(fp,0L,SEEK_SET);
omFree((ADDRESS)pi->data.s.body);
pi->data.s.body=(char *)omAlloc((int)len+1);
myfread( pi->data.s.body, len, 1, fp);
pi->data.s.body[len]='\0';
fclose(fp);
}
}
si_unlink(filename);
omFree(filename);
}
ADDRESS
void * ADDRESS
Definition auxiliary.h:120
fp
CanonicalForm fp
Definition cfModGcd.cc:4110
res
CanonicalForm res
Definition facAbsFact.cc:60
myfread
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition feFopen.cc:195
getenv
char * getenv()
iiGetLibProcBuffer
char * iiGetLibProcBuffer(procinfo *pi, int part)
Definition iplib.cc:197
pi
#define pi
Definition libparse.cc:1145
SEEK_SET
#define SEEK_SET
Definition mod2.h:115
SEEK_END
#define SEEK_END
Definition mod2.h:111
omStrDup
#define omStrDup(s)
Definition omAllocDecl.h:263
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omFree
#define omFree(addr)
Definition omAllocDecl.h:261
LANG_SINGULAR
@ LANG_SINGULAR
Definition subexpr.h:22

## sdb_find_arg()

char * sdb_find_arg | ( | char * | p | ) |

static

Definition at line 47 of file sdb.cc.

{
p++;
while (*p==' ') p++;
char *pp=p;
while (*pp>' ') pp++;
*pp='\0';
return p;
}
pp
CanonicalForm FACTORY_PUBLIC pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f ).
Definition cf_gcd.cc:676

## sdb_set_breakpoint()

BOOLEAN sdb_set_breakpoint | ( | const char * | pp,
|  | int | given_lineno )

Definition at line 64 of file sdb.cc.

{
idhdl h=ggetid(pp);
if ((h==NULL)||(IDTYP(h)!=PROC_CMD))
{
PrintS(" not found\n");
return TRUE;
}
else
{
procinfov p=(procinfov)IDDATA(h);
#ifdef HAVE_DYNAMIC_LOADING
if (p->language!=LANG_SINGULAR)
{
PrintS("is not a Singular procedure\n");
return TRUE;
}
#endif
int lineno;
if (given_lineno >0) lineno=given_lineno;
else lineno=p->data.s.body_lineno;
int i;
if (given_lineno== -1)
{
i=p->trace_flag;
p->trace_flag &=1;
Print("breakpoints in %s deleted(%#x)\n",p->procname,i &255);
return FALSE;
}
i=0;
while((i<7) && (sdb_lines[i]!=-1)) i++;
if (sdb_lines[i]!= -1)
{
PrintS("too many breakpoints set, max is 7\n");
return TRUE;
}
sdb_lines[i]=lineno;
sdb_files[i]=p->libname;
i++;
p->trace_flag|=(1<<i);
Print("breakpoint %d, at line %d in %s\n",i,lineno,p->procname);
return FALSE;
}
}
TRUE
#define TRUE
Definition auxiliary.h:101
PROC_CMD
@ PROC_CMD
Definition grammar.cc:281
IDDATA
#define IDDATA(a)
Definition ipid.h:126
IDTYP
#define IDTYP(a)
Definition ipid.h:119
procinfov
procinfo * procinfov
Definition structs.h:56

## sdb_show_bp()

void sdb_show_bp | ( |  | ) |

Definition at line 57 of file sdb.cc.

{
for(int i=0; i<7;i++)
if (sdb_lines[i]!= -1)
Print("Breakpoint %d: %s::%d\n",i+1,sdb_files[i],sdb_lines[i]);
}

## 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 208 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.

{-1,-1,-1,-1,-1,-1,-1,-1};