Detecting a segmention fault in C - c

I have Win7 Pro (32 bit) and CodeBlocks IDE.
I would like to know is there any way to detect line with a segmention fault in C. My code is PRIME1.c
I find somewhere on Stack Overflow that this is possible on linux in terminal, but I would like to do that in Windows.
Could anyone tell me how to do that?
Many thanks!
In other words, I would like to know how to use debugger from cmd in windows 7 and how it can tell me which line is problematic.
I just found this link
Determine the line of C code that causes a segmentation fault?
But, as you can see, this is for Linux.
I would like to know how can I do that in Windows cmd?

You can catch seg fault. But, unfortunately, can not handle this event anyhow or get any info about that fault (in standard way, there are workarounds specific for compilers). So, maybe put printf in every line with __LINE__ macro and just wait until it fails.
#include <signal.h>
#include <conio.h>
#include <stdio.h>
void listener(int sig) {
printf("listener: access violation");
_getch();
}
void main() {
char a = 10;
char *p = &a;
signal(SIGSEGV, listener);
do {
printf("%d", *p++);
} while (1);
_getch();
}

Related

Segmentation fault on trying to execute value at environment variable

Hey so I was trying to solve a problem for beginners ctf event.
And this is the code that I am trying to run.
#include <stdio.h>
#include <stdlib.h>
int main(){
int (*func)();
func = getenv("MYENV");
func();
return 0;
}
I created a MYENV environment like this :
export MYENV=ls
but on running the code, it throws a segmentation fault (core dumped). I don't understand why.
The func function is basically calling the environment variable whose value is a simple command that I set. Why is it throwing an error.
I'm very new at linux and shell, so I'm sorry if this is too naive.
In C, if you want to run a system command, you have to use the system function (or one of the exec functions but that's more complicated):
#include <stdio.h>
#include <stdlib.h>
int main() {
char* cmd = getenv("MYENV");
system(cmd);
return 0;
}
If you're looking to run arbitrary code, you can inject shell code into it:
export MYENV=$'\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80'
You can learn more here.

How to catch error when program crash?

How to catch error when program crashes?
Example - Java when crash writes an error log to file.
I writed this code to cause an error:
int * invalidPointer = NULL;
printf("%d\n", invalidPointer[0]);
And java crashes and saving an error log (hs_error_pid(pid).log) to file, I want to handle an error in program written with C (not in Java, this is only example)
Second example - Chrome when crashes shows information and we can restart browser by clicking yes.
Though it is undefined behavior, you can use this if you're getting segmentation fault.
#include <signal.h>
#include <stdio.h>
void Segfault_Handler(int signo)
{
fprintf(stderr,"\n[!] Oops! Segmentation fault...\n");
}
int main() {
signal(SIGSEGV,Segfault_Handler);
return 0;
}

ASM code in C program causes segmentation fault

I included some sample ASM code in a small program to do a test.
My program is:
#include <stdio.h>
static inline
unsigned char inb (int port) {
unsigned char data;
asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
return data;
}
int main()
{
printf("hello world %d\n", inb(22));
return 0;
}
When I run the program, it crashes with a segmentation fault when executing the ASM code.
Could someone tell me what's wrong with this small program? Thanks a lot.
You need to use ioperm before you're allowed to use port I/O. Also, note the kernel already provides inb and outb functions.
Use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the
user space application to access the I/O ports in question. Failure
to do this will cause the application to receive a segmentation fault.
If your OS is Windows or Linux, most likely your program is terminated because the OS doesn't allow regular applications access I/O ports.
You syntax is absolutely correct. Just find and use the valid or unused port on your system.

How to use System(const char*) in TC++

Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++

UBUNTU on VMWARE Segmentation fault (core dumped)

sorry if my english is horrible: I'm realy stresed :(
I'm using Ubuntu 11.04 that work as virtual machine. Using VMWARE as virtual machine operating system is windows 7.
I'm trying to write a program on linux with C language: that using shared memory. when I try to compile there are no errors but when I execute it I see an error like this:
Segmentation fault (core dumped)
I'm not sure but as far as I learned VMWARE is causing this :S
here are the codes:
#include<stdio.h>
#include<sys/wait.h> //Process wait
#include <fcntl.h> //File
//#include <cstdlib>
//#include<fstream.h>
int main(){
printf("\n Here we go...!");
int *Numbers;
Numbers=(int*)getmem(327); // shared memory
int i,ProcID;
ProcID=fork(); //depart processor
if(ProcID==0){ // child processor
for(i=0;i<50;i++){
Numbers[i]=random()%50;
}
}else if(ProcID<0){
printf("\n Hmm... There is an error!");
}
int Waiting;
wait(&Waiting);
if(ProcID>0){ // parent processor
int fileeven,fileodd;
fileeven=open("EK_even.txt",O_RDWR|O_CREAT,0600);
fileodd=open("EK_odd.txt",O_RDWR|O_CREAT,0600);
for(i=0;i<50;i++){
if(Numbers[i]%2==0){
write(fileeven,&Numbers[i],sizeof(Numbers[i]));
}else{
write(fileodd,&Numbers[i],sizeof(Numbers[i]));
}
}
close(fileeven);
close(fileodd);
}else if(ProcID<0){
printf("\n Hmm... There is an error!");
}
return 1;
}
I'm using this to compile on terminal:gcc -o ./RUN ./EK.c -shared
to Run :./RUN
as result :Segmentation fault (core dumped)
Thanks for your time and reponds I'm realy in need...
Assuming getmem takes a number of bytes as a parameter, you allocate 327 bytes for your array of numbers:
Numbers=(int*)getmem(327);
If you are on a 64 bit system with 8-byte int, this is enough space for 40 integers.
You then proceed to put 50 numbers into that array, more than you allocated space for. This might very well cause a segmentation fault.
Generally, start your program in a debugger to see where exactly the segmentation fault occurs. This way you can more easily locate the error in your program.
The problem is VMWare as Marc B said. I tryed it on a real operating system and it worked. getmem() function is not my own function. To use it you have to add "-shared" at the end of compile line. Thanks for replies...

Resources