#include <errno.h>
#include <stdio.h>
#include <asm/sysinfo.h>
#include <asm/unistd.h>
#define TMP_PATH_MAX 1024
static int
setsysinfo(unsigned long op, void *buffer, unsigned long size,
int *start, void *arg, unsigned long flag)
{
syscall(__NR_osf_setsysinfo, op, buffer, size, start, arg, flag);
}
void
trap_unaligned(void)
{
unsigned int buf[2];
buf[0] = SSIN_UACPROC;
buf[1] = UAC_SIGBUS | UAC_NOPRINT;
setsysinfo(SSI_NVPAIRS, buf, 1, 0, 0, 0);
}
static void
usage(void)
{
fprintf(stderr,
"usage: unaligned [-b] <command-path> [command-args...]\n\n"
" This program is designed to assist debugging of\n"
" unaligned traps by running the program in gdb\n"
" and causing it to get SIGBUS when it encounters\n"
" an unaligned trap.\n\n"
" It is free software written by Sean Hunter <sean@uncarved.co.uk>\n"
" based on code by Richard Henderson and Andrew Morgan.\n\n"
);
exit(1);
}
int
main(int argc, char **argv)
{
const char my_debugger[] = "/usr/bin/gdb";
char *temp_str;
char *curr;
int size = 0;
int curr_arg;
int isBatchMode = 0;
if (argc < 2) {
usage();
}
if( strcmp("-b" , argv[1]) == 0 ){
isBatchMode = 1;
curr_arg = 2;
}else{
curr_arg = 1;
}
trap_unaligned();
if (argc > 2) {
size = 2048;
for(; curr_arg < argc; curr_arg++) {
size += strlen(argv[curr_arg]);
}
temp_str = (char *) malloc(sizeof(char) * size);
if (!temp_str) {
fprintf(stderr, "Unable to malloc memory for string use: %s\n", strerror(errno));
exit(255);
}
if(isBatchMode==1){
sprintf(temp_str, "%s -batch %s -x <( echo file %s; echo set args", my_debugger, argv[2], argv[2]);
}else{
sprintf(temp_str, "%s %s -x <( echo file %s; echo set args", my_debugger, argv[1], argv[1]);
}
curr = temp_str + strlen(temp_str);
for(curr_arg = 2; curr_arg < argc; curr_arg++) {
sprintf(curr, " %s", argv[curr_arg]);
curr = temp_str + strlen(temp_str);
}
#ifndef NOAUTORUN
curr = temp_str + strlen(temp_str);
sprintf(curr, "; echo run");
#endif
curr = temp_str + strlen(temp_str);
sprintf(curr, ")");
execlp("/bin/bash", "/bin/bash", "-c", temp_str, NULL);
}
else {
execlp(my_debugger, my_debugger, argv[1], NULL);
}
fprintf(stderr, "Unable to execute command: %s\n", strerror(errno));
usage();
}