C++ SIGINT and SIGSTP over ctrl+c and ctrl+z - c++

I am writing on a shell and want to implement the signals SIGSTP and SIGINT.
When the user starts a new process and presses CTRL+C it should send a SIGINT to the process and when CTRL+Z is pressed the process should get the SIGSTP signal.
Here is my code so far:
string inputNew = input;
vector<char*> arguments;
size_t lastChar = 0;
for(int i = 0; i < input.size(); i++)
{
char& c = input[i];
if((c == ' ' || c == '\t'||c == '\n') && lastChar != i)
{
c = '\0';
arguments.push_back(&input[0] + lastChar);
lastChar = i+1;
}
}
bool checkIfBackground(string & input)
{
size_t lastChar = input.size() - 1;
if(input[lastChar] == '&')
{
return true;
}
return false;
}
if((pid = fork()) < 0) {
exit(1);
} else if(pid == 0) {
execvp(arguments[0], &arguments[0]);
exit(1);
} else if(checkIfBackground(inputNew) == false) {
int status;
pid_t pid_r;
if(waitpid(pid, &status, 0) < 0) {
cout << "PID not known!";
}
} else {
cout << "Prozess is waiting in the background." << endl;
}
I have no idea how to implement the SIGSTP and SIGINT signals inside my code.

See the sigaction(2) manual pages. It explains how to set up an implement a signal handler.
Note that a signal handler is asynchronous. This has a number of implications. Read the manual page, and spend some time in Google.

Related

Serial port ReadFile receives repeated data

I am working on a multi threading program to read and write to a serial port. If I test my application with Putty everything is fine. But when I test it with the created .exe-file, it doesn't work. (I start the program in VS2017 and then the .exe file)
E.g.: My input: "Test", the output in the other window: "Teeeeeeeeeeeessssssssssttttttt".
My code to send the data:
void SendDataToPort()
{
for (size_t i = 0; i <= strlen(line); i++) // loop through the array until
every letter has been sent
{
try
{
if (i == strlen(line)) // If ever letter has been sent
{ // end it with a new line
c = '\n'; // a new line
}
else
{
c = line[i];
}
WriteFile(serialHandle, &c, 1, &dwBytesWrite, NULL); // Send letter to serial port
}
catch (const exception&)
{
cout << "Error while writing.";
}
}
cout << endl << "Sent." << endl;
}
In the array "line" I have the input from the user.
My code to read the data:
int newLineCounter = 0;
unsigned char tmp;
while (!endCurrentRead)
{
ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter
if (tmp != '\n')
{
newLineCounter = 0;
if (tmp >= 32 && tmp <= 126)
{
output += tmp; // Add current letter to the output
}
}
else
{
newLineCounter++;
if (newLineCounter < 2) // If there are more than 2 '\n' it doesn't write it down
{
output += tmp;
}
else if (newLineCounter == 2)
{
Print(output);
output = receiverName;
endCurrentRead = true;
}
}
}
After that I write the data down with the Print(output) function:
cout << output << endl;
How I create the Handle File:
serialHandle = CreateFile(LcomPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Why is this happening and why is this only happening when I test it with the .exe-file and not with Putty?
Thanks to the help of #quetzalcoatl, I was able to solve the problem. I have to check if the bytesRead are bigger then 0.
Solution:
int newLineCounter = 0;
DWORD dwCommModemStatus;
unsigned char tmp;
while (!endCurrentRead)
{
ReadFile(hSerial, &tmp, 1, &bytesRead, NULL); // Get new letter
if (bytesRead > 0)
{
if (tmp != '\n')
{
newLineCounter = 0;
if (tmp >= 32 && tmp <= 126)
{
output += tmp; // Add current letter to the output
}
}
else
{
output += tmp;
Print(output);
output = receiverName;
endCurrentRead = true;
}
}
}

Fork: Resource temporarily unavailable when running shell with one arg

I am trying to write a microshell in C++ that will take in 1 or 2 args and run them in UNIX. My shell takes two args split by || fine, but when I run only one I get a massive fork error. My shell will look for || as a pipe instead of just |. Thank you in advance!
Some Functional commands are:
cat filename || sort
ls -l || less
Code:
#include <iomanip>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
void getParms (char[], char* [], char* []);
int main()
{
char command[160];
pid_t pid1 = 1, pid2 = 1;
cout << "myshell> ";
cin.getline(command, 160);
while (strcmp(command, "q") != 0 && strcmp(command, "quit") != 0 && pid1 > 0 && pid2 > 0)
{
char* arg1[6];
char* arg2[6];
char path1[21], path2[21];
int pipefd[2];
arg1[0]=NULL;
arg2[0]=NULL;
getParms(command, arg1, arg2);
if (pipe(pipefd) < 0)
{
perror ("Pipe");
exit (-1);
}
//cerr <<"This is arg2"<<arg2[0]<<endl;
pid1 = fork();
if (pid1 < 0)
{
perror ("Fork");
exit (-1);
}
if (pid1 == 0)
{
//cout<<"Child 1"<<endl;
//cerr<<arg1[0]<<endl;
if(arg2[0] != NULL)
{
close(pipefd[0]);
close(1);
dup(pipefd[1]);
close(pipefd[1]);
}
strcpy(path1, "/bin/");
strcat(path1, arg1[0]);
if (execvp(path1, arg1) < 0)
{
strcpy(path1, "/usr/bin/");
strncat(path1, arg1[0], strlen(arg1[0]));
if (execvp(path1, arg1) < 0)
{
cout<<"Couldn't execute "<<arg1[0]<<endl;
exit (127);
}
}
if(arg2[0]== NULL)
{ // Parent process
close (pipefd[0]); //read
close (pipefd[1]); //write
waitpid(pid1, NULL, 0); // Waits for child2
cout << "myshell> ";
cin.getline(command, 160);
}
}
else if(arg2[0] != NULL)
{
//cerr<<"Child 2"<<endl;
pid2 = fork();
if (pid2 < 0)
{
perror ("Fork");
exit (-1);
}
if (pid2 == 0)
{
close(pipefd[1]);
close(0);
dup(pipefd[0]);
close(pipefd[0]);
strcpy(path2, "/bin/");
strncat(path2, arg2[0], strlen(arg2[0]));
if (execvp(path2, arg2) < 0)
{
strcpy(path2, "/usr/bin/");
strncat(path2, arg2[0], strlen(arg2[0]));
if (execvp(path2, arg2) < 0)
{
cout<<"Couldn't execute "<<arg2[0]<<endl;
exit (127);
}
}
}
else
{ // Parent process
//cerr<<"in last 2 else"<<endl;
close (pipefd[0]); //read
close (pipefd[1]); //write
waitpid(pid2, NULL, 0); // Waits for child2
cout << "myshell> ";
cin.getline(command, 160);
}
}
}
return 0;
}
/****************************************************************
FUNCTION: void getParms (char [], char* [], char* [])
ARGUMENTS: char str[] which holds full command
char* args[] args2[] which will hold the individual commands
RETURNS: N/A
****************************************************************/
void getParms(char str[], char* args[], char* args2[])
{
char* index;
int i= 0;
int j= 0;
index = strtok(str, " ");
//cerr<<"before first while"<<endl;
// While the token isn't NULL or pipe
while (index != NULL && strstr(index,"||") == NULL)
{
args[i] = index;
index = strtok(NULL, " ");
i++;
}
args[i] = (char*) NULL; // makes last element Null
//cerr<<" getParms before ||"<<endl;
if(index != NULL && strcmp(index,"||") != 0)
{
//cerr<<"after checking for ||"<<endl;
index = strtok(NULL," ");
while (index != NULL)
{
args2[j] = index;
index = strtok(NULL," ");
j++;
}
}
//cerr<<"After second IF"<<endl;
args2[j] = (char*) NULL; // makes last element Null
}
Your problem is that the main while loop is not going to any of the if-else statements in which you have the prompt for another command - the same statement is executed over and over. When you use the double pipe it goes to else if(arg2[0] != NULL) and the parent process shows a new prompt.
Try removing both prompts for a command from the main while loop in your if-else statement and move the prompt to the beginning of the loop like this:
//Move these two below into the while loop
//cout << "myshell> ";
//cin.getline(command, 160);
while (strcmp(command, "q") != 0 && strcmp(command, "quit") != 0 && pid1 > 0 && pid2 > 0)
{
cout << "myshell> ";
cin.getline(command, 160);
//...
}
Try not to make such redundant calls of the same thing. If you have a couple of those and you need to change something it can get messy.

batch processing in a simple shell

Hey folks I am making a batch command function in a shell that reads from a txt file and pipes it to a child to exec.
I'm having an issue with exec. I suspect it is something with the null terminator. If I execl with an L and an explicit (char*)NULL the exec runs. If I execvp(argIn[0],argIn) nothing runs and returns a -1. If I execvp with an explicit (char*)NULL I get an error cannot convert char* to char* constant*. I read somewhere that it might be the g++ compiler giving me the error but the gcc compiler wouldn't give the error. Right now it won't compile with gcc though so I'm not sure if that's true. But it shouldn't need the explicit terminator anyway. I'm not sure if the '\0' I have stored is being passed to the exec right. It checks out when I pass it to other functions though so maybe that's not the solution.
Second, my for loop won't exec more than once which I think is more to do with the first solution. I can get the execl to fork with an index but I can't increment the index to point to the right token the next time through because the child should be wiping out my index right?
Anyway it's been 3 weeks of digging to figure out what's wrong. I failed the assignment. Probably going to fail the class. I don't know what else to try. So any help I would appreciate.
My question is why would the exec function not execute the program? I'm passing execvp(program name, program name, option, option, '\0') and not getting a result.
or
execl(program name, program name[index], option[index+1], option[index+1], (char*)NULL) and getting a result. They both seem to be following the parameters but only one is giving me a result.
#include<string.h>
#include<iostream>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
using namespace std;
int makearg(char s[], char**args[]);
int main(int argc, char *argv[])
{
char **argIn;
int argCount;
int pos = 0;
char str[500];
pid_t pid = fork();
for(int i = 0; i < 4; i++)
{
if(pid == 0)
{
while(fgets(str, 500, stdin) != NULL)
{
cout << "String loaded: " << str;
argCount = makearg(str, &argIn);
execvp(argIn[0],argIn); //nothing exec
// execl(argIn[0],argIn[0],argIn[1],argIn[2],(char*)NULL); //exec but requires index.
else if(pid < 0)
{
perror("fork() error");
exit(-1);
}
else if(pid > 0)
{
cout << "Parent waiting" << endl;
wait(NULL);
}
}
return 0;
}
int makearg(char s[], char**args[])
{
int counter = 1;
int tokenLen = 1;
int i = 0;
int j = 0;
int k = 0;
char arg1[50];
char * arg2;
strcpy(arg1, s);
//Count white space.
while (arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
{
counter++;
}
j++;
}
//Allocate the number of rows to be pointed to.
args[0] = (char**) malloc(counter + 1);
if(args[0] == NULL)
exit(1);
//Allocate the size of the c string arrays
j = 0;
while(arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
{
(*args)[i] = (char*)(malloc(tokenLen));
if((*args)[i] == NULL)
exit(1);
tokenLen = 0;
i++;
}
j++;
tokenLen++;
}
(*args)[i] = (char*)(malloc(tokenLen));
if ((*args)[i] == NULL)
exit(1);
//reset values
i = 0;
j = 0;
//Set arg2 to point to args row head. Transfer values from arg1 to arg2.
arg2 = ((*args)[i]);
while(arg1[j] != '\0')
{
if (arg1[j] != ' ' && arg1[j] != '\0' && arg1[j] != '\n')
{
arg2[k] = arg1[j];
k++;
}
else
{
arg2[k] = '\0';
i++;
k = 0;
arg2 = ((*args)[i]);
}
j++;
}
arg2[k] = '\0';
if (counter < 1)
{
return -1;
}
return counter;
}
I took your posted code, updated it to fix build errors and ran. I executed the simple command "ls" but I got the message
String loaded: ls
ls: cannot access '': No such file or directory
That indicated to me that makearg is not working correctly. Then, I added a function to help with diagnosing the problem.
void printArguments(char **args)
{
for ( int j = 0; args[j] != NULL; ++j )
{
printf("args[%d]: %s\n", j, args[j]);
}
}
and added a call to it from main, right after the call to makearg.
argCount = makearg(str, &argIn);
printArguments(argIn);
I got the output:
String loaded: ls
args[0]: ls
args[1]:
ls: cannot access '': No such file or directory
That indicated to me that makearg was not dealing with the end of the line correctly. It creates an empty argument.
I added couple of functions to trim whitespaces from the left and from the right. After that, the child process was able to execute "ls" correctly.
Here's the updated program.
#include<string.h>
#include<iostream>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
using namespace std;
int makearg(char s[], char**args[]);
void printArguments(char **args)
{
for ( int j = 0; args[j] != NULL; ++j )
{
printf("args[%d]: %s\n", j, args[j]);
}
}
int main(int argc, char *argv[])
{
char **argIn;
int argCount;
char str[500];
pid_t pid = fork();
for(int i = 0; i < 4; i++)
{
if(pid == 0)
{
while(fgets(str, 500, stdin) != NULL)
{
cout << "String loaded: " << str;
argCount = makearg(str, &argIn);
printArguments(argIn);
execvp(argIn[0],argIn); //nothing exec
}
}
else if(pid < 0)
{
perror("fork() error");
exit(-1);
}
else if(pid > 0)
{
cout << "Parent waiting" << endl;
wait(NULL);
}
}
return 0;
}
void trimWhiteSpacesLeft(char s[])
{
int i = 0;
for ( ; isspace(s[i]); ++i );
if ( i == 0 )
{
return;
}
int j = 0;
for (; s[i] != '\0'; ++j, ++i )
{
s[j] = s[i];
}
s[j] = '\0';
}
void trimWhiteSpacesRight(char s[])
{
int len = strlen(s);
int i = len-1;
for ( ; i >= 0; --i )
{
if ( !isspace(s[i]) )
{
break;
}
}
s[i+1] = '\0';
}
int makearg(char s[], char**args[])
{
int counter = 1;
int tokenLen = 1;
int i = 0;
int j = 0;
int k = 0;
char arg1[50];
char * arg2;
strcpy(arg1, s);
// Trim whitespaces from both ends.
trimWhiteSpacesLeft(arg1);
trimWhiteSpacesRight(arg1);
//Count white space.
while (arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' )
{
counter++;
}
j++;
}
//Allocate the number of rows to be pointed to.
args[0] = (char**) malloc(counter + 1);
if(args[0] == NULL)
exit(1);
//Allocate the size of the c string arrays
j = 0;
while(arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
{
(*args)[i] = (char*)(malloc(tokenLen));
if((*args)[i] == NULL)
exit(1);
tokenLen = 0;
i++;
}
j++;
tokenLen++;
}
(*args)[i] = (char*)(malloc(tokenLen));
if ((*args)[i] == NULL)
exit(1);
//reset values
i = 0;
j = 0;
//Set arg2 to point to args row head. Transfer values from arg1 to arg2.
arg2 = ((*args)[i]);
while(arg1[j] != '\0')
{
if (arg1[j] != ' ' && arg1[j] != '\0' && arg1[j] != '\n')
{
arg2[k] = arg1[j];
k++;
}
else
{
arg2[k] = '\0';
i++;
k = 0;
arg2 = ((*args)[i]);
}
j++;
}
arg2[k] = '\0';
if (counter < 1)
{
return -1;
}
return counter;
}

Implementing a Pipe in C++

I am having trouble implementing my pipe. It reads Unix terminal commands from a text file for commands such as "ls|wc" where it opens a pipe so that the output of ls can be used for wc.
I have implemented it to parse program names ("ls", "wc") and store them in two separate arrays (arguments and arguments2), fork a child process, then have that child fork another child process, then the second child process calls the execvp() command and passes the first program to be executed.
The output from ("ls") is then written to the pipe by changing the standard output. The former child process then execvp()'s the other process ("wc") and reads from the pipe by changing the standard input.
However, wc loops indefinitely and does not seem to count the number of words from ls. Ls executes in a directory where there are words to be counted.
Any tips? Thank you so much and sorry for the long explanation.
Here is a simpler example: It forks, creates a pipe, and implements "ls" and writes its output to the pipe, goes back to the parent and reads from the pipe the output of "ls". It still seems to be reading forever or not working right.
//
// main.cpp
// Pipe_Test
//
// Created by Dillon Sheffield on 9/28/15.
// Copyright © 2015 Dillon Sheffield. All rights reserved.
//
#include <iostream>
using namespace std;
int main()
{
char* arguments[2];
char* programArguments[1];
int fd[2];
arguments[0] = new char[2];
arguments[1] = new char[2];
programArguments[0] = new char[1];
programArguments[0][0] = '\0';
string ls = "ls";
string wc = "wc";
for (int i = 0; i < 1; i++) {
arguments[0] = &ls.at(i);
arguments[1] = &wc.at(i);
}
pid_t pid = fork();
pipe(fd);
if (pid < 0) {
perror("Failed.\n");
} else if (pid == 0) {
dup2(fd[1], STDOUT_FILENO);
execvp(arguments[0], programArguments);
}
wait(NULL);
dup2(fd[0], STDIN_FILENO);
execvp(arguments[1], programArguments);
close(0);
close(1);
return 0;
}
Here is my original code:
//
// main.cpp
// homework2
//
// Created by Dillon Sheffield on 9/19/15.
// Copyright © 2015 Dillon Sheffield. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
// Global Variable(s)
const short inputLineSize = 10; // Size of programName, arguments, and argument name.
char *arguments[inputLineSize];
char *arguments2[inputLineSize];
ifstream inputFile;
char* input;
void readLine()
{
// Create new char array(s)
input = new char[inputLineSize];
// Initialize the char array(s)
for (int i = 0; i < inputLineSize; i++)
{
input[i] = '\0';
}
// Read a line and skip tabs, spaces, and new line characters
for (int i = 0; !inputFile.eof() && inputFile.peek() != '\n'; i++)
{
while (inputFile.peek() == '\n' || inputFile.peek() == '\t' || inputFile.peek() == ' ') inputFile.get();
inputFile.get(input[i]);
}
// If the file is multi-spaced, keep reading new line char(s) to clear them
while (inputFile.peek() == '\n') inputFile.get();
}
void parseTokens()
{
//----------Parse the read line into tokens--------------------------------------------//
// Get the program name
for (int i = 0; i < inputLineSize; i++)
{
arguments[i] = new char[inputLineSize];
for (int j = 0; j < inputLineSize; j++)
arguments[i][j] = '\0';
}
int i = 0;
int j = 0;
while (input[i] != '\0' && input[i] != '-' && input[i] != '|')
{
arguments[j][i] = input[i];
i++;
}
// Tokenize arguments if supplied
j++;
int k;
while (input[i] == '-')
{
k = 0;
arguments[j][k] = input[i];
i++;
k++;
while (input[i] != '-' && input[i] != '\0')
{
arguments[j][k] = input[i];
i++;
k++;
}
j++;
}
// Delete unused arguments
while (j < inputLineSize)
{
delete arguments[j];
arguments[j] = NULL;
j++;
}
// Check if the pipe command '|' is supplied
if (input[i] == '|')
{
i++;
// Get the other program name
for (int x = 0; x < inputLineSize; x++)
{
arguments2[x] = new char[inputLineSize];
for (int y = 0; y < inputLineSize; y++)
arguments2[x][y] = '\0';
}
int x = 0;
int j = 0;
while (input[i] != '\0' && input[i] != '-' && input[i] != '|')
{
arguments2[j][x] = input[i];
i++;
x++;
}
// Tokenize arguments if supplied
j++;
int k;
while (input[i] == '-')
{
k = 0;
arguments2[j][k] = input[i];
i++;
k++;
while (input[i] != '-' && input[i] != '\0')
{
arguments2[j][k] = input[i];
i++;
k++;
}
j++;
}
// Delete unused arguments
while (j < inputLineSize)
{
delete arguments2[j];
arguments2[j] = NULL;
j++;
}
}
}
int main()
{
// Variable(s)
pid_t pid;
pid_t pid2;
int fd[2];
//--Open the file named "input"-------------------------------------------------------//
inputFile.open("input", ios::in);
// Check if opening the file was successful
if (inputFile.is_open())
{
// Read until the file has reached the end
while (!inputFile.eof())
{
// Read a line and parse tokens
readLine();
parseTokens();
//----------Now create a new process with parsed Program Name and Arguments-----------//
// Create a pipe
pipe(fd);
// Fork
pid = fork();
if (pid < 0)
{
perror("Fork failed.\n");
return -2;
}
else if (pid == 0)
{
// Fork again
pid2 = fork();
if (pid2 < 0)
{
perror("Fork failed.\n");
return -2;
}
else if (pid2 == 0)
{
// Change standard output
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) perror("dup2 error to stdout.\n");
// Execute the given program
execvp(arguments[0], arguments);
}
// Change the standard input to the pipe
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) perror("dup2 error to stdin.\n");
int returnValue = execvp(arguments2[0], arguments2);
if (returnValue == -1) perror("Error has occurred.\n");
// Close the pipe and exit
close(fd[0]);
close(fd[1]);
exit(0);
}
// Wait for the child so it doesn't become a Zombie
wait(NULL);
//----------Clean up-----------------------------------------------------------------//
delete input;
input = NULL;
int i = 0;
while (arguments[i] != NULL)
{
delete arguments[i];
arguments[i] = NULL;
i++;
}
i = 0;
}
}
else perror("Cannot open file.\n");
inputFile.close();
return 0;
}
You first execute a pipe(), then you execute a fork(), and the child process, with some additional work, executes your commands.
The problem here is that the pipe()d file descriptors, both of them, remain open in the original parent process. Both the read-side of the pipe, and, more importantly, the write-side of the pipe. These file descriptors do, from all appearances, get correctly set up for your child processes, but because the file descriptors also remain open in the parent process, the pipe never closes, even after the process that writes to the write side of the pipe terminates.
Since the write side of the pipe remains open, in the parent process, the child process that's reading the read side of the pipe will continue reading. Forever.
What you need to do is that after the fork, in the parent process, close both the read and the write side of the pipe. Once the initial process gets forked, the parent process does not need the pipe, and its open file descriptors get in the way.

Escaping trapflag/single step

I'm writing a program that traces the execution of other programs. I'm using dynamic instruction instrumentation to track the behavior of x86's CMP instruction.
I'm using the windows debugging api to control the behavior of the debugged program. I start the program with the 'debug only this process' flag, and then set the trap flag on the main thread.
I then enter the main debugging loop:
bool cDebugger::ProcessNextDebugEvent(bool Verbose)
{
bool Result = true;
DEBUG_EVENT Event = { 0 };
DWORD Status = DBG_CONTINUE;
if (!WaitForDebugEvent(&Event, INFINITE))
{
_Reporter("Error: WaitForDebugEvent: " + to_string(GetLastError()));
return Result;
}
else
{
if (Event.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Created process: " + GetFilenameFromHandle(Event.u.CreateProcessInfo.hFile));
}
else if (Event.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Dll: " + GetFilenameFromHandle(Event.u.LoadDll.hFile) + " loaded at: " + to_string((unsigned int)Event.u.LoadDll.lpBaseOfDll));
_Dlls.insert(make_pair((unsigned int)Event.u.LoadDll.lpBaseOfDll, GetFilenameFromHandle(Event.u.LoadDll.hFile)));
}
else if (Event.dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Thread[" + to_string(Event.dwThreadId) + "] created at: " + to_string((unsigned int)Event.u.CreateThread.lpStartAddress));
_Threads.push_back(Event.dwThreadId);
}
else if (Event.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Thread[" + to_string(Event.dwThreadId) + "] exited with: " + to_string(Event.u.ExitThread.dwExitCode));
auto It = std::find(_Threads.begin(), _Threads.end(), Event.dwThreadId);
if (It != _Threads.end())
_Threads.erase(It);
}
else if (Event.dwDebugEventCode == UNLOAD_DLL_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Dll " + _Dlls[(unsigned int)Event.u.UnloadDll.lpBaseOfDll] + " unloaded at : " + to_string((unsigned int)Event.u.UnloadDll.lpBaseOfDll));
}
else if (Event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
{
if (Verbose)
_Reporter("Process exited with: " + to_string(Event.u.ExitProcess.dwExitCode));
Result = false;
_Threads.clear();
}
else if (Event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
{
if (Event.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP)
{
Status = DBG_EXCEPTION_HANDLED;
}
else
{
Status = DBG_EXCEPTION_NOT_HANDLED;
}
}
for (size_t i = 0; i < _Threads.size(); i++)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, _Threads[i]);
if (hThread == NULL)
{
_Reporter("Error: Failed to open thread: " + to_string(GetLastError()));
}
else
{
CONTEXT ThreadContext = GetThreadContext(hThread);
ProcessStep(ThreadContext, hThread);
ThreadContext.EFlags |= 0x100; // Set trap flag.
SetThreadContext(hThread, ThreadContext);
CloseHandle(hThread);
}
}
if (!ContinueDebugEvent(Event.dwProcessId, Event.dwThreadId, Status))
{
_Reporter("Error: ContinueDebugEvent: " + to_string(GetLastError()));
}
}
return Result;
}
As you can see I loop through all threads at the end of the function to make sure that the single-step exception will trigger on every next instruction in every thread.
However sometimes execution seems to 'escape' this trap, often executing millions of instructions before being caught again by the next debugging event.
I wrote another small application to test the behavior of my program:
int main(int argc, char* argv[])
{
//__asm int 3h
if (argc == 41234123)
{
printf("Got one\n");
}
return 0;
}
The expected output of the tracer should be:
0xDEADBEEF CMP 1 41234123
However somehow the tracer does not record this instruction (indicating that no debug event was raised, and that the trap flag was not set).
Can anybody see if I'm doing something wrong in my debug loop? Or what kind of behavior of the test program (loading of a dll) could be responsible for this?
The problem had something to do with code entering kernel space when calling windows apis. My solution was to set the page protection of the executable section of the test program to PAGE_GUARD:
SYSTEM_INFO Info;
GetSystemInfo(&Info);
DWORD StartAddress = (DWORD)Info.lpMinimumApplicationAddress;
DWORD StopAddress = (DWORD)Info.lpMaximumApplicationAddress;
DWORD PageSize = 0;
PageSize = Info.dwPageSize;
_Sections.clear();
for (DWORD AddressPointer = StartAddress; AddressPointer < StopAddress; AddressPointer += PageSize)
{
MEMORY_BASIC_INFORMATION Buffer;
VirtualQueryEx(_Process.GetHandle(), (LPCVOID)AddressPointer, &Buffer, sizeof(Buffer));
if (CheckBit(Buffer.Protect, 4) || CheckBit(Buffer.Protect, 5) || CheckBit(Buffer.Protect, 6) || CheckBit(Buffer.Protect, 7))
{
if (Buffer.State == MEM_COMMIT)
{
_Sections.push_back(make_pair((unsigned int)Buffer.BaseAddress, (unsigned int)Buffer.RegionSize));
AddressPointer = (unsigned int)Buffer.BaseAddress + (unsigned int)Buffer.RegionSize;
}
}
}
void cDebugger::SetPageGuard()
{
for (size_t i = 0; i < _Sections.size(); i++)
{
DWORD Dummy;
VirtualProtectEx(_Process.GetHandle(), (LPVOID)_Sections[i].first, _Sections[i].second, PAGE_GUARD | PAGE_EXECUTE_READWRITE, &Dummy);
}
}
This way I regain control because the system will fire a EXCEPTION_GUARD_PAGE when execution returns to a guarded page.
if (Event.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP)
{
Status = DBG_CONTINUE;
if (!_Tracing)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, Event.dwThreadId);
CONTEXT ThreadContext = GetThreadContext(hThread);
if (ThreadContext.Eip == _EntryAddress)
{
ClearHardwareBreakpoint(0, hThread);
_Tracing = true;
}
CloseHandle(hThread);
}
SetPageGuard();
_Guarded = true;
}
else if (Event.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
{
Status = DBG_CONTINUE;
}
else if (Event.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_GUARD_PAGE)
{
Status = DBG_CONTINUE; // fires when processor lands on guarded pages
}
else
{
Status = DBG_EXCEPTION_NOT_HANDLED;
}
This solution is not perfect. There are possibly still some situations under which execution can still escape the 'trap'. But it solved my most immediate problem (being able to see the comparisons in my test program).