Exemplo n.º 1
0
int shell_rmbp(char **args)
{
    if (args[1]) {
        rm_bp(get_value_from_string(args[1]));
    } else {
        free_bp();
    }
    return OK;
}
Exemplo n.º 2
0
Error free_byid(int no){
    Error res = {NULL, SUCCESS};
    if(no < 0 || no >= 32){
        res.id = OUT_RANGE;
        res.str = f_mes[OUT_RANGE];
        return res;
    }
    if (bp_head() == NULL){
        res.id = NULL_POINTER;
        res.str = f_mes[NULL_POINTER];
        return res;
    }

    BP* h = bp_head();
    // only one bp should not invoke free_bp
    if (h->NO == no) {
        recover_content_in_mem(h);
        //modify the breakpoint pool
        BP* temp = h->next;
        add(&free_, h);
        set_bp_head(temp);
        return res;
    }
    //more than one bp: find the bp before the one with number
    while (h->next != NULL) {
        if(h->next->NO == no){
            recover_content_in_mem(h->next);
            break;
        }
        h = h->next;
    }
    if(h->next == NULL){
        res.id = INVALID;
        res.str = f_mes[INVALID];
        return res;
    }
    free_bp(h);
    return res;
}
Exemplo n.º 3
0
/*把某个编号的断点释放*/
void free_b(int num)
{
	BP* current = head,*prev;
    while(current -> NO != num-1 && current != NULL)
   	{		
		prev = current;
		current = current -> next;
	}
	if(current && current -> b_or_w)
		free_bp(current -> addr);
	else if(current)
	{
		if(current == head)
			head = current -> next;
		else
			prev -> next = current -> next;
		current -> next = free_;
		free_ = current;
		current -> isUsed = false;
	}
	else
		printf("There doesn't exist the breakpoint!\n");
}
Exemplo n.º 4
0
void breakpoint_del(int n){
	free_bp(n-1);
}
Exemplo n.º 5
0
int
main(void)
{
  // INITIALIZING
  struct queue cmd_queue;
  q_init (&cmd_queue);

  uint8_t *mem = calloc (__MEMORY_SIZE, sizeof(uint8_t));
  char *input = malloc (sizeof(char)*__INPUT_SIZE);
  char *cmd = malloc (sizeof(char)*__CMD_SIZE);
  char *filename = malloc (sizeof(char)*__FILENAME_SIZE);
  if (mem == NULL || input == NULL || filename == NULL
      || cmd == NULL)
    {
      puts("MEMORY INSUFFICIENT");
      goto memory_clear;
    }

  if (!init_oplist (__OPCODE_FILENAME))
    {
      puts("OPCODE LIST INITIALIZATION FAILED.");
      goto memory_clear;
    }

  // COMMAND PROCESSING
  while (true)
    {
      int i;
      struct q_elem *qe;
      uint8_t value;
      uint32_t start, end;
      DIR *dirp = NULL;
      struct dirent *dir = NULL;
      char check[2];
      bool is_valid_cmd = false;
      char *tok = NULL;

      printf("%s", __SHELL_FORM);
      if (!get_chars(input, __INPUT_SIZE))
        goto memory_clear;

      // Processing input string
      snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE,
                   "%%%ds", __CMD_SIZE - 1);
      if (sscanf(input, (const char *) __CMD_FORMAT, cmd)!=1)
        cmd[0] = '\0';
      
      // Switching with commands
      switch(get_cmd_index(cmd))
        {
        case CMD_HELP:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          puts(__HELP_FORM);
          is_valid_cmd = true;
          break;
        
        case CMD_DIR:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          // open directory and read through all elem.
          i = 1;
          dirp = opendir(".");
          dir = readdir(dirp);
          for(; dir!=NULL; dir = readdir(dirp))
            {
              struct stat st;
              if(stat((const char*) dir->d_name, &st)!=0)
                {
                  puts("FILE NOT FOUND");
                  goto memory_clear;
                }
              // FIX: ignore . and ..
              if(_SAME_STR(dir->d_name, ".")
                 || _SAME_STR(dir->d_name, ".."))
                continue;
              printf("%20s", dir->d_name);
              if(S_ISDIR(st.st_mode)) // is Directory?
                putchar('/');
              else if( (st.st_mode & S_IXUSR) // is exe?
                 || (st.st_mode & S_IXGRP)
                 || (st.st_mode & S_IXOTH) )
                putchar('*');
              putchar('\t');
             
              // print newline after 3 elements
              if((i++)%3==0)
                putchar('\n');
            }
          if((i-1)%3!=0)
            putchar('\n');
          
          is_valid_cmd = true;
          break;
        
        case CMD_QUIT:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          
          is_valid_cmd = true;
          goto memory_clear;
        
        case CMD_HISTORY:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          qe = q_begin (&cmd_queue);
          i = 1;
          // print every formatted history
          for (; qe!=q_end(&cmd_queue); qe=q_next(qe))
            printf("%-4d %s\n", i++,
                   q_entry(qe, struct cmd_elem, elem)->cmd);
          printf("%-4d %s\n", i, input);
          
          is_valid_cmd = true;
          break;
        
        case CMD_DUMP:
          switch(sscanf(input, "%s %x , %x", cmd, &start, &end))
            {
            case 1:
              if(sscanf(input, "%*s %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              start = get_location (0, false);
              end = start + 0x10 * 10 - 1;
              // if end is too large, point to end and go 0
              if ( end >= __MEMORY_SIZE )
                end = __MEMORY_SIZE - 1;
              hexdump (mem, start, end);
              if ( end == __MEMORY_SIZE - 1)
                get_location (0, true);
              else
                get_location (end + 1, true);
              
              is_valid_cmd = true;
              break;
            
            case 2:
              if(sscanf(input, "%*s %*x %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              if (start >= __MEMORY_SIZE)
                {
                  puts("OUT OF MEMORY BOUNDS.");
                  break;
                }
              end = start + 0x10 * 10 - 1;
              // if end is too large, point to end and go 0
              if ( end >= __MEMORY_SIZE )
                end = __MEMORY_SIZE - 1;
              hexdump (mem, start, end);
              if ( end == __MEMORY_SIZE - 1)
                get_location (0, true);
              else
                get_location (end + 1, true);
              
              is_valid_cmd = true;
              break;
            
            case 3:
              if(sscanf(input, "%*s %*x , %*x %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              if (!(start<=end && end<__MEMORY_SIZE))
                {
                  puts("OUT OF MEMORY BOUNDS.");
                  break;
                }
              hexdump (mem, start, end);
              // if end is too large, point to end and go 0
              if ( end == __MEMORY_SIZE - 1)
                get_location (0, true);
              else
                get_location (end + 1, true);
              
              is_valid_cmd = true;
              break;

            default:
              puts("WRONG INSTRUCTION");
              break;
            }
          break;
        
        case CMD_EDIT:
          switch(sscanf(input, "%s %x , %hhx",
                        cmd, &start, &value))
            {
            case 3:
              if(sscanf(input, "%*s %*x , %*x %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              hexfill (mem, __MEMORY_SIZE, start, start, value);
              
              is_valid_cmd = true;
              break;
            
            default:
              puts("WRONG INSTRUCTION");
              break;
            }
          break;
        
        case CMD_FILL:
          switch(sscanf(input, "%s %x , %x , %hhx",
                        cmd, &start, &end, &value))
            {
            case 4:
              if(sscanf(input,
                        "%*s %*x , %*x , %*x %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              hexfill (mem, __MEMORY_SIZE, start, end, value);
              
              is_valid_cmd = true;
              break;
            
            default:
              puts("WRONG INSTRUCTION");
              break;
            }
          break;

        case CMD_RESET:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          // equivalent to fill 0, __MEMORY_SIZE-1
          hexfill (mem, __MEMORY_SIZE, 0, __MEMORY_SIZE - 1, 0);
              
          is_valid_cmd = true;
          break;

        case CMD_OPCODE:
          switch(sscanf(input, "%*s %s", cmd))
            {
            case 1:
              if(sscanf(input, "%*s %*s %1s", check) == 1)
                {
                  puts("WRONG INSTRUCTION");
                  break;
                }
              i = find_oplist (cmd);
              if (i != -1)
                printf("opcode is %02X\n", i);
              else
                {
                  printf("%s: NO SUCH OPCODE\n", cmd);
                  is_valid_cmd = false;
                }
              break;

            default:
              puts("WRONG INSTRUCTION");
              break;
            }
          break;

        case CMD_OPCODELIST:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          print_oplist ();
          is_valid_cmd = true;
          break;

        case CMD_ASSEMBLE:
          // Processing input string
          snprintf((char *) __CMD_FORMAT,
                   __CMD_FORMAT_SIZE,
                   "%%%ds %%%ds %%1s",
                   __CMD_SIZE - 1,
                   __FILENAME_SIZE - 1);
          if (sscanf(input,
                     (const char *) __CMD_FORMAT,
                     cmd, filename, check)!=2)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (!is_file((const char*)filename))
            {
              puts("FILE NOT FOUND");
              break;
            }

          is_valid_cmd = assemble_file (filename);

          break;

        case CMD_TYPE:
          // Processing input string
          snprintf((char *) __CMD_FORMAT,
                   __CMD_FORMAT_SIZE,
                   "%%%ds %%%ds %%1s",
                   __CMD_SIZE - 1,
                   __FILENAME_SIZE - 1);
          if (sscanf(input,
                     (const char *) __CMD_FORMAT,
                     cmd, filename, check)!=2)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (!is_file((const char*)filename))
            {
              puts("FILE NOT FOUND");
              break;
            }
          else
            {
              print_file((const char*)filename);
              is_valid_cmd = true;
            }

          break;

        case CMD_SYMBOL:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }

          print_symbol_table ();
          is_valid_cmd = true;

          break;

        case CMD_PROGADDR:
          if(sscanf(input, "%*s %*x %1s", check) == 1
             || sscanf(input, "%*s %x", &i) != 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (i < 0 || i >= __MEMORY_SIZE - 1)
            {
              puts("INVALID PROGRAM ADDRESS");
              break;
            }

          set_progaddr ((uint32_t) i);
          is_valid_cmd = true;

          break;

        case CMD_LOADER:
          init_loader ();
          tok = strtok (input, " ");
          while ( (tok = strtok (NULL, " ")) != NULL)
            {
              if (!is_file (tok))
                {
                  printf ("[%s]: INVALID FILE\n", tok);
                  free_loader ();
                  break;
                }
              if (!add_obj_loader (tok))
                {
                  printf ("[%s]: LOADER FAILED\n", tok);
                  free_loader ();
                  break;
                }
            }

          // if normally added
          if (tok == NULL)
            {
              // address __MEMORY_SIZE is reserved for boot
              if (get_proglen()+get_progaddr()>=__MEMORY_SIZE-1)
                {
                  puts ("PROGRAM IS TOO BIG: LOADER FAILED");
                  free_loader ();
                  break;
                }
              if (!run_loader (mem))
                {
                  puts ("LOADER FAILED");
                  free_loader ();
                  break;
                }
              print_load_map ();
            }
          free_loader ();
          is_valid_cmd = true;

          break;

        case CMD_RUN:
          if(sscanf(input, "%*s %1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (!init_run ())
            {
              puts ("RUN FAILED");
              free_run ();
              break;
            }
          run (mem);
          free_run ();
          is_valid_cmd = true;

          break;

        case CMD_BP:
          if(sscanf(input, "%*s %1s", check) != 1)
            {
              print_bp ();
              is_valid_cmd = true;
              break;
            }
          if(sscanf(input, "%*s %6s %1s", cmd, check) == 2
             || sscanf(input, "%*s %6s", cmd) != 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (_SAME_STR(cmd, "clear"))
            {
              puts ("\t[ok] clear all breakpoints");
              free_bp ();
              is_valid_cmd = true;
              break;
            }

          if(sscanf(input, "%*s %*x %1s", check) == 1
             || sscanf(input, "%*s %x", &start) != 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
          if (start >= __MEMORY_SIZE - 1)
            {
              puts ("INVALID BREAKPOINT ADDRESS");
              break;
            }
          if (add_bp (start))
            printf ("\t[ok] create breakpoint %x\n", start);
          is_valid_cmd = true;

          break;

        default:
          if(sscanf(input, "%1s", check) == 1)
            {
              puts("WRONG INSTRUCTION");
              break;
            }
        }

      if (is_valid_cmd)
        {
          // Saving commands
          struct cmd_elem *e = malloc(sizeof(struct cmd_elem));
          if (e == NULL)
            {
              puts("MEMORY INSUFFICIENT.");
              goto memory_clear;
            }
          e->cmd = malloc(sizeof(char)*(strlen(input)+1));
          if (e->cmd == NULL)
            {
              puts("MEMORY INSUFFICIENT.");
              goto memory_clear;
            }
          strcpy(e->cmd, input);
          q_insert (&cmd_queue, &(e->elem));
        } 
    }


memory_clear:
  if (mem != NULL)
    free (mem);
  if (input != NULL)
    free (input);
  if (cmd != NULL)
    free (cmd);
  while (!q_empty(&cmd_queue))
    {
      struct q_elem *e = q_delete(&cmd_queue);
      struct cmd_elem *ce = q_entry(e, struct cmd_elem, elem);
      if (ce->cmd != NULL)
        free(ce->cmd);
      free(ce);
    }
  free_oplist ();
  free_loader ();
  free_bp ();
  free_run ();

  return 0;
}