207 lines
3.9 KiB
C++
207 lines
3.9 KiB
C++
#include <iostream>
|
|
#include <iomanip>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <signal.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <sys/file.h>
|
|
#include <functional>
|
|
|
|
#include "async_scanner.h"
|
|
|
|
#define BUF_LEN_FOR_PID 64
|
|
|
|
|
|
static async_scanner *scanner = nullptr;
|
|
static void sigHandler(int sig)
|
|
{
|
|
// if (sig == SIGINT || sig == SIGTERM)
|
|
// remove(MY_PID_FILE);
|
|
printf("exit now for signal: %d\n", sig);
|
|
if(scanner)
|
|
{
|
|
scanner->stop();
|
|
scanner->release();
|
|
}
|
|
|
|
_exit(0);
|
|
}
|
|
|
|
namespace test_cmd
|
|
{
|
|
static void skip_space(const char*& ptr)
|
|
{
|
|
while(*ptr == ' ')
|
|
ptr++;
|
|
}
|
|
static void print_help(void)
|
|
{
|
|
console_color clr = console_color::COLOR_CHAR_RED;
|
|
|
|
printf("usage: ");
|
|
utils::printf_with_color("clear", clr);
|
|
printf(" - to clear screen\n ");
|
|
|
|
utils::printf_with_color("exit", clr);
|
|
printf(" - to quit this app\n ");
|
|
|
|
utils::printf_with_color("free", clr);
|
|
printf(" - <addr>: to free memory\n ");
|
|
|
|
utils::printf_with_color("mem", clr);
|
|
printf(" - to print memory usage\n ");
|
|
|
|
utils::printf_with_color("rdmem", clr);
|
|
printf(" - <addr> [len]: to read memory\n ");
|
|
|
|
utils::printf_with_color("sys", clr);
|
|
printf(" - to invoke system call\n");
|
|
}
|
|
|
|
static const char* free_cmd = "free ";
|
|
static void free_memory(const char* cmd)
|
|
{
|
|
uint64_t addr = 0;
|
|
if(strstr(cmd, " 0x"))
|
|
{
|
|
cmd = strstr(cmd, " 0x") + 3;
|
|
utils::from_hex_string(cmd, &addr);
|
|
}
|
|
else
|
|
{
|
|
cmd += strlen(free_cmd);
|
|
addr = atoll(cmd);
|
|
}
|
|
printf("\t free %p? ", addr);
|
|
if(getchar() == 'y')
|
|
free((void*)addr);
|
|
}
|
|
|
|
static const char* rdm_cmd = "rdmem ";
|
|
static void read_memory(const char* cmd)
|
|
{
|
|
uint64_t addr = 0;
|
|
if(strstr(cmd, " 0x"))
|
|
{
|
|
cmd = strstr(cmd, " 0x") + 3;
|
|
utils::from_hex_string(cmd, &addr);
|
|
}
|
|
else
|
|
{
|
|
cmd += strlen(free_cmd);
|
|
addr = atoll(cmd);
|
|
}
|
|
printf("\t read memory %p? ", addr);
|
|
if(getchar() == 'y')
|
|
{
|
|
int len = 128, off = 0;
|
|
skip_space(cmd);
|
|
cmd = strstr(cmd, " ");
|
|
if(cmd)
|
|
{
|
|
skip_space(cmd);
|
|
len = atoi(cmd);
|
|
}
|
|
|
|
uint8_t *ptr = (uint8_t*)addr;
|
|
std::string str("");
|
|
char buf[2] = {0};
|
|
for(; off < len; )
|
|
{
|
|
printf("%p: ", ptr + off);
|
|
for(int c = 0; off < len && c < 16; ++c, ++off)
|
|
{
|
|
printf("%02X ", ptr[off]);
|
|
if(ptr[off] >= ' ' && ptr[off] < 0x7f)
|
|
{
|
|
buf[0] = ptr[off];
|
|
str += buf;
|
|
}
|
|
else
|
|
str += ".";
|
|
|
|
if((c + 1) % 16 == 0)
|
|
{
|
|
printf(" %s\n", str.c_str());
|
|
str = "";
|
|
}
|
|
else if((c + 1) % 8 == 0)
|
|
{
|
|
printf(" ");
|
|
}
|
|
}
|
|
}
|
|
if(str.length())
|
|
printf(" %s\n", str.c_str());
|
|
}
|
|
}
|
|
|
|
static const char* sys_cmd = "sys ";
|
|
static void invoke_sys_command(const char* cmd)
|
|
{
|
|
cmd = strstr(cmd, sys_cmd);
|
|
if(cmd)
|
|
{
|
|
cmd += strlen(sys_cmd);
|
|
system(cmd);
|
|
}
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
/* Ctrl + C */
|
|
if (signal(SIGINT, sigHandler) == SIG_ERR)
|
|
{
|
|
exit(-1);
|
|
}
|
|
|
|
/* kill pid / killall name */
|
|
if (signal(SIGTERM, sigHandler) == SIG_ERR)
|
|
{
|
|
exit(-1);
|
|
}
|
|
|
|
/* kill pid / killall name */
|
|
signal(SIGKILL, sigHandler);
|
|
|
|
scanner = new async_scanner();
|
|
int err = scanner->last_error();
|
|
|
|
test_cmd::print_help();
|
|
while(err == 0)
|
|
{
|
|
std::string cmd(utils::from_console("cmd>"));
|
|
if(cmd == "exit")
|
|
break;
|
|
if(cmd == "mem")
|
|
utils::print_memory_usage(" Memory usage", false);
|
|
else if(cmd.find(test_cmd::free_cmd) == 0)
|
|
test_cmd::free_memory(cmd.c_str());
|
|
else if(cmd.find(test_cmd::rdm_cmd) == 0)
|
|
test_cmd::read_memory(cmd.c_str());
|
|
else if(cmd == "clear")
|
|
system("clear");
|
|
else if(cmd == "status")
|
|
scanner->print_usb_status();
|
|
else if(cmd.find(test_cmd::sys_cmd) == 0)
|
|
test_cmd::invoke_sys_command(cmd.c_str());
|
|
else
|
|
test_cmd::print_help();
|
|
}
|
|
|
|
async_scanner* tmp = scanner;
|
|
|
|
scanner = nullptr;
|
|
tmp->stop();
|
|
tmp->release();
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(500));
|
|
utils::print_memory_usage("Memory usage", false);
|
|
|
|
return 0;
|
|
} |