LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2014, 02:29 PM   #1
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Rep: Reputation: Disabled
In GCC C is their any way to catch a runtime system error in C code?


I am getting a core dump when running my C program. Is there any C catch mechanism such as signal to catch that error so I can print out the error that the operating system found to a file as well as close the output file? How does that work? Thank you. Alvin...
 
Old 01-16-2014, 03:15 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Just use the debugger.

Code:
$ cc -g -O0 YourProgram.c

$ gdb ./a.out

(gdb) run

[crash occurs]

(gdb) bt

(gdb) quit

[fix your code]
 
Old 01-16-2014, 03:35 PM   #3
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
Thank you dwhitney67 That helped a lot.
 
Old 01-16-2014, 04:03 PM   #4
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
A problem:
I did
Code:
$ c99 -g -Oapsbasic apsbasic.c

$ gdb ./apsbasic

(gdb) run
but it said:
Code:
Starting program: /home/schmitta/APS_PRODUCTS/USBTOEMBED/software/apsbasic/apsbasic 
usage: run <filename>
[Inferior 1 (process 2844) exited with code 01]
(gdb)
so I typed:

Code:
run ./apsbasic ./pt01.bas
and it said:
Code:
Starting program: /home/schmitta/APS_PRODUCTS/USBTOEMBED/software/apsbasic/apsbasic ./apsbasic ./pt01.bas
usage: run <filename>
[Inferior 1 (process 2849) exited with code 01]
(gdb)
So it would not let me enter the file name ./pt01.bas

So I typed and it answered:
Code:
(gdb) run ./apsbasic
Starting program: /home/schmitta/APS_PRODUCTS/USBTOEMBED/software/apsbasic/apsbasic ./apsbasic

  APSBASIC -- PC to USB to Embedded PIC32 Control Program
     Copyright (c) 2013 APSTRON LLC all rights reserved
                  www.apsproducts.net
             type help for more information


Program received signal SIGSEGV, Segmentation fault.
get_token () at apsbasic.c:2912
2912		*temp2='\0';
(gdb)
So it does not look like it will allow me to enter command line fields after the ./apsbasic module name.
 
Old 01-17-2014, 02:33 AM   #5
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
try
Code:
gdb --args apsbasic arg1
 
1 members found this post helpful.
Old 01-17-2014, 09:44 AM   #6
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
Thank you piyush.sharma I will try that.
 
Old 01-17-2014, 10:26 AM   #7
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
I have a symbol table structure called "struct symbol" defined globally in a header file. In one of my functions I try to print out the sizeof the structure in "sizeof(symbol)". I get the following error code:
Code:
apsbasic.c:2299:64: error: ‘symbol’ undeclared (first use in this function)
Why does the above not work? Thank you. Alvin...
 
Old 01-17-2014, 12:25 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It would be helpful if you posted ALL of your code (or at least the relevant parts), rather than just the error.

However, if I had to guess... and that's all I can do right now (crystal ball is not functioning at the moment), I would say that you are using a statement like "sizeof(symbol)" versus using "sizeof(struct symbol)".
 
Old 01-19-2014, 02:37 PM   #9
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
I was using sizeof(struct symbol); but someone told me that was wrong. Thanks. I eventually instantiated (in C not c++) struct symbol a; and then did sizeof(a); multiplied it by the offset index of the array of structures -- struct symbol{ ... }symbtab[1000]; and then found the address of of the first structure in the array (&symbtab[0]) - added it all together as pointers and came up with a pointer (struct symbol *x and assigned the added up mess to the pointer (x=&symbtab[0]+index*sizeof(a). Then i used the pointer formed to address the the item in the structure I wanted (char *nameptr=(*x).name; where name is a pointer to a string that is the symbol in the symbol table I was looking for). If you have any suggestions it would be greatly appreciated.
 
Old 01-19-2014, 05:41 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
First, pick a programming language: C and C++ are not the same, although they share, for the most part, the same syntax.

In C you need to specify the keyword 'struct' when referring to a structure, unless you type-cast it away using a 'typedef'. Using the latter can sometimes lead to confusion when code is analyzed/maintained, thus avoid it if you can.
Code:
// My preferred style; the structure below must be referred to as "struct Symbol".
//
struct Symbol
{
   char* name;
};


// Alternate style, which I personally do not like; here, the structure can be referred to as "Symbol".
//
typedef struct
{
    char* name;
} Symbol;
As for what you are attempting to accomplish, I could not discern such from what you posted. It would seem that your attempts to access specific locations within your structure array may be incorrect. When performing pointer arithmetic on structures, the offset used is assumed to a multiple of the size of the structure.

Thus when computing the offset within your structure, perhaps you meant to do something like:
Code:
struct Symbol* x = &symtab[0] + index;

Last edited by dwhitney67; 01-19-2014 at 05:43 PM.
 
1 members found this post helpful.
Old 01-20-2014, 10:27 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To add to the original recommendation for using gdb. What you could also do is enable core file creation.

In the shell:
Code:
ulimit -c unlimited
Also can be done in your program:

Code:
#include <sys/time.h>
#include <sys/resource.h>

struct rlimit rlim = { RLIM_INFINITY, RLIM_INFINITY };

setrlimit(RLIMIT_CORE, &rlim);
Check the result of the setrlimit() call to ensure that it did happen.

When your program fails, it will generate a core file. You then can enter into gdb like the following:

Code:
gdb <binary-name> core
Same result is you can perform backtrace on that. As said before, make sure you use -ggdb in your compilation statement and consider disabling optimization -O0.

As far as the question about the structs, if you have an array of structures and you know the index, then index into the array first and get your structure member before you start performing pointer operations. A possible issue is longword alignment, for instance if you have a structure which is something like:
Code:
struct Eg {
    char array[5];
    char *pArray;
    short smallVar;
    double longVar;
};
If the machine natural word is 32 bits, the pointer will be a 32-bit value; however the short may not be that, but instead by 16-bits, and another problem there is the array of 5 characters may be aligned at a 16-bit word boundary versus a 32-bit word boundary. One thing a person can do there is to use directives for packing; however the behaviors I don't trust and if I know it's important, I then force alignment by declaring that array to be 32 characters and putting in say a filler "short unused" variable between smallVar and longVar. In the end, never rely on normal pointer progression through a struct so that you can declare like a char * and then step through that entire structure; you didn't say you were doing that either, just noting this potential problem. Instead it sounds as if you have an array of structures, say 1000 of the example shown above and say you wish to look at each character in the sub-element named "array". Then you do something like:
Code:
struct Eg myList[1000];
char *p_temp;
int index;

for(index = 0; index < sizeof(myList); index++) {
    p_temp = Eg.array;
    // Then use p_temp to look into the array, but remember that the size of that array is "5" or sizeof(Eg.array)
}
 
1 members found this post helpful.
Old 01-20-2014, 11:50 AM   #12
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by rtmistler View Post
Code:
struct Eg myList[1000];
char *p_temp;
int index;

for(index = 0; index < sizeof(myList); index++) {
    p_temp = Eg.array;
    // Then use p_temp to look into the array, but remember that the size of that array is "5" or sizeof(Eg.array)
}
The code above looks wrong. If you want to index into the array of structures, to get the number of elements (which we know to be 1000), you would use sizeof(myList) / sizeof(struct Eg). Also, you want to access the array, not the structure definition.

Code:
struct Eg myList[1000];

for (size_t index = 0; index < sizeof(myList) / sizeof(struct Eg); index++) {
    char* p_temp = myList[index].array;
    // ...
}
P.S. If you are going to use std-99 style comments with C code, then you might as well extend the std-99 practice to declaring variables closest to where they are used.

Last edited by dwhitney67; 01-20-2014 at 11:51 AM.
 
1 members found this post helpful.
Old 01-20-2014, 12:03 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Thanks for the clarifications. I'm a hybrid of K&R, C99, and just adapt as time, styles, and examples grow. I probably also wouldn't use the sizeof a struct anyways and would've just used 1000 directly in my code. Also a big proponent of testing it out if I'm not sure. Code standards can be whatever they want, but what it really does for your case is the most important thing.
 
1 members found this post helpful.
Old 01-20-2014, 07:01 PM   #14
schmitta
Member
 
Registered: May 2011
Location: Blacksburg VA
Distribution: UBUNTU, LXLE
Posts: 352

Original Poster
Rep: Reputation: Disabled
Thank you both for your extended comments and information. I will study it all.

I use to program mini computers where we had a linkage editor with names of variables and programs and their addresses in storage. We also had debuggers to manipulate items in memory. When I get a storage dump or just when I want to see what happened I would like to have the address of where in storage the program was loaded as well as a storage viewing routine or something to dump out the hex of storage as well as the ascii translation of the code. I am using Ubuntu. Is there a way to get the function and variable names and their offsets in storage as well as a program to dump out the storage in a formatted way? Thank you. Alvin....
 
Old 01-20-2014, 07:15 PM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Just use 'gdb' (or the GUI front-end called 'ddd') to debug and analyze your program.
 
  


Reply

Tags
error, gcc, system, ubuntu



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error in compiling SDL code using gcc richman1234 Programming 3 02-01-2013 06:56 PM
gcc throws Segmentation fault runtime error mrajdeep Programming 9 08-22-2011 02:30 PM
Trying to see assembly code on Runtime Azrai Programming 1 07-18-2006 11:44 PM
Strange GCC compile error with C code exvor Programming 16 08-23-2005 06:05 PM
error in catch and try . code is attached ashwinipahuja Programming 6 07-05-2004 10:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration