9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit bc35d72f75ab87a9fb96cd88d8c37c602ec04d8c
parent 27a83338a8e8dbcd8271b4baba09fa85356b7c5f
Author: Dimitris Papastamos <dimitris.papastamos@arm.com>
Date:   Tue,  6 Nov 2018 12:57:35 +0000

Merge "[debuglang] Avoid problems tokenizing the input string"
Diffstat:
Msrc/debuglang/debuglang.c | 170+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 79 insertions(+), 91 deletions(-)

diff --git a/src/debuglang/debuglang.c b/src/debuglang/debuglang.c @@ -1,5 +1,7 @@ +#include <ctype.h> #include <errno.h> #include <setjmp.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -8,19 +10,41 @@ #include "debuglang.h" +#define PREFIX "> " + +extern jmp_buf recover; + +static void +error(char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + fputs(PREFIX "ERR ", stdout); + vprintf(fmt, va); + va_end(va); + longjmp(recover, 1); +} + +static void +ready(void) +{ + puts(PREFIX "READY"); +} + void print_tf(struct trapframe *tfp) { - printf("x0=0x%llx\tx1=0x%llx\tx2=0x%llx\tx3=0x%llx\n" - "x4=0x%llx\tx5=0x%llx\tx6=0x%llx\tx7=0x%llx\n" - "x8=0x%llx\tx9=0x%llx\tx10=0x%llx\tx11=0x%llx\n" - "x12=0x%llx\tx13=0x%llx\tx14=0x%llx\tx15=0x%llx\n" - "x16=0x%llx\tx17=0x%llx\tx18=0x%llx\tx19=0x%llx\n" - "x20=0x%llx\tx21=0x%llx\tx22=0x%llx\tx23=0x%llx\n" - "x24=0x%llx\tx25=0x%llx\tx26=0x%llx\tx27=0x%llx\n" - "x28=0x%llx\tx29=0x%llx\tx30=0x%llx\telr=%p\n" - "spsr=0x%llx\tesr=0x%llx\tfar=0x%llx\n" - "sp=0x%p\n", + printf(PREFIX "x0=0x%llx\tx1=0x%llx\tx2=0x%llx\tx3=0x%llx\n" + PREFIX "x4=0x%llx\tx5=0x%llx\tx6=0x%llx\tx7=0x%llx\n" + PREFIX "x8=0x%llx\tx9=0x%llx\tx10=0x%llx\tx11=0x%llx\n" + PREFIX "x12=0x%llx\tx13=0x%llx\tx14=0x%llx\tx15=0x%llx\n" + PREFIX "x16=0x%llx\tx17=0x%llx\tx18=0x%llx\tx19=0x%llx\n" + PREFIX "x20=0x%llx\tx21=0x%llx\tx22=0x%llx\tx23=0x%llx\n" + PREFIX "x24=0x%llx\tx25=0x%llx\tx26=0x%llx\tx27=0x%llx\n" + PREFIX "x28=0x%llx\tx29=0x%llx\tx30=0x%llx\telr=%p\n" + PREFIX "spsr=0x%llx\tesr=0x%llx\tfar=0x%llx\n" + PREFIX "sp=0x%p\n", tfp->x0, tfp->x1, tfp->x2, tfp->x3, tfp->x4, tfp->x5, tfp->x6, tfp->x7, tfp->x8, tfp->x9, tfp->x10, tfp->x11, @@ -70,7 +94,7 @@ get_named_reg(char* name, struct trapframe* tfp) } /* Get rmc_command struct for named rmc command */ -const struct rmc_command* +static const struct rmc_command * get_rmc_cmd(char* name) { size_t i; @@ -84,7 +108,7 @@ get_rmc_cmd(char* name) } /* Get command struct for named command */ -const struct command* +static const struct command * get_command(char* name) { size_t i; @@ -232,105 +256,69 @@ do_rmc(char** argv,int argc, struct trapframe* tfp) return 0; } -/* Separate buffer whitespace */ -void -tokenise(char* buff, char** argv, unsigned int* argc, char* sep) -{ - char* token; - - *argc=0; - token=strtok(buff,sep); - while(token!=NULL){ - argv[*argc]=token; - (*argc)++; - token=strtok(NULL,sep); - } - return; -} - -char* -trim(char* buff) -{ - int bufflen,i; - char* newbuff; - - newbuff = buff; - bufflen=strlen(buff); - - /* Trailing whitespace */ - for(i = bufflen-1; i>0; i--){ - if(buff[i]==' ' || buff[i]=='\t' || buff[i] == '\n'){ - buff[i]='\0';; - } - else{ - break; - } - } - /* Leading whitespace */ - for(i = 0; i<bufflen; i++){ - if(buff[i]==' ' || buff[i]=='\t' || buff[i] == '\n'){ - newbuff+=1; - } - else{ - break; - } - } - return newbuff; -} - -int +static void parse_cmd(char * cmd, struct trapframe* tfp) { - char* argv[MAX_PARTS]; - unsigned int argc; - const struct command* com; - - cmd = trim(cmd); - - /* Get array of strings by separating on whitespace */ - tokenise(cmd,argv,&argc," "); - - if(argc != 0){ - com = get_command(argv[0]); + char *argv[MAX_PARTS], *p; + unsigned argc; + struct command *com; + + while (isspace(*cmd)) + ++cmd; + + argc = 0; + for (p = strtok(cmd, " \t\r"); p; p = strtok(NULL, " \t\r")) { + if (argc == MAX_PARTS) + error("too much parameters"); + argv[argc++] = p; } - if(com == NULL){ - ERROR("Command '%s' not found\n",argv[0]); - return 1; - } + if (argc == 0) + return; + + if ((com = get_command(argv[0])) == NULL) + error("command '%s' not found", argv[0]); /* Call eval function for specific command */ com->eval(argv,argc, tfp); - return 0; } -static void +static int run(struct trapframe *tf) { char buffer[BUFFER_SIZE]; - char* end; - - end=0; - do{ - printf("$ "); - fflush(stdout); - end = fgets(buffer,BUFFER_SIZE,stdin); - if(strlen(buffer)>1){ - parse_cmd(buffer, tf); - } - fflush(stdout); + size_t len; + + if (fgets(buffer, sizeof(buffer), stdin) == NULL) + return 0; + + if ((len = strlen(buffer)) != 0) { + if (buffer[len-1] != '\n') + error("line too long"); + buffer[len-1] = '\0'; + parse_cmd(buffer, tf); } - while(end != NULL); + return 1; } void debug(void) { struct trapframe tf; - extern jmp_buf recover; - memset(&tf, 0, sizeof(struct trapframe)); + setvbuf(stdin, NULL, _IOLBF, 0); + setvbuf(stdout, NULL, _IOLBF, 0); + printf("begin debug language interface\n"); + memset(&tf, 0, sizeof(struct trapframe)); setjmp(recover); - run(&tf); + + for (ready(); run(&tf); ready()) + ; + + if (ferror(stdout)) + puts("debug language interface: error writing in stdout"); + if (ferror(stdin)) + puts("debug language interface: error reading from stdin"); + printf("end debug language interface\n"); }