473,836 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Debugging standard C library routines

I am working on MontaVista Linux on a PPC board.
The compiler I am using is a gnu cross compiler cross compiler
(ppc-82xx-gcc)

The application/software that is being run, is messing up the memory
due to which a subsequent malloc fails.

When the application core is dumped I do not get any backtrace in the
code (although the application is compiled with debug symbols, with -g
option). The only thing I get is the address of the function (and not
the name) where the crash is happening.

Using prints in the code I found that application crashes while calling
malloc. I am linking the standard C library as .so, and thats probably
why there is not backtrace conatining the functions of my application.

There is not enough memory on the board to run gdb. Also due to the
complexity of the scenarios it is not always possible to run gdbserver.
It there a possible way to:
1. Link a debuggable version of stdlib which would put more information
in the core, when it is dumped.
2. Can I enable some asserts or extra information in stdlib which would
tell me if memry is being messed up.
3. Is there a way by which I can map an address to a function of .so
loaded in the memory. Or even if i know that the address belongs t a
perticaular .so

Thanks in advance.

Sep 30 '06
83 4007
jacob navia said:

<snip>
Initializing every pointer does no harm, but it is unlikely to
improve the situation of the original poster.
What I gave was a list of several easy steps (initialisation being one of
those steps) which, taken together, make it easier to debug memory-related
problems. In that context, it will certainly improve the situation of the
original poster.

<snip>
In a debug setting is better to never check malloc,
Oh deary deary me. You surely cannot be serious?
so, if there is any
failure the program crashes immediately at the point of failure.
The Standard offers no such guarantee.
>>>Check that every array access is within bounds (0 to n - 1, for an array
of n elements).

This is like saying to the kids:

Be a good kid and do not do any stupid actions ok?
Yes. And if you're a good kid and don't do anything stupid, you improve the
chances that your code's going to work.
HOW could that advise be implemented?
for(i = 0; i < n; i++)
{
x[i] = foo(i);
}

It's not rocket science.
It is all the difficulty of C.
I don't see what's so difficult about it. If you're writing outside your
array, your mental model of the program is wrong. And so, whether or not
the language cushions you from the bad effects of writing out of bounds,
you still need to fix the program. Anyone who can drive safely along the
road knows the importance, and *ease*, of keeping within bounds.
Telling people "check every array access" leads to no concrete
solutions for the OP.
Here's what I actually said:

"Initialise every pointer. Check every malloc/calloc/realloc to ensure it
succeeded before you rely on its return value. Check that every array
access is within bounds (0 to n - 1, for an array of n elements). Set
indeterminate ('dangling') pointers to NULL.

Following these simple steps will get rid of 99.90072% of all known
memory-related crashes."

I reckon that *will* lead to a concrete solution for the OP.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 1 '06 #11
Frederick Gotham said:
Richard Heathfield posted:
>>>Check every malloc/calloc/realloc to ensure it succeeded before
you rely on its return value.

If malloc'ing less than a kilobyte, I wouldn't bother.

...but when he offers dangerously stupid advice such as that, it's a good
idea for people to point it out. Such stupid advice leads, when followed,
to buggy, unstable software.

Many people disagree with you.
Yes, and many people aren't very good C programmers.

>>>Set indeterminate ('dangling') pointers to NULL.

In Debug Mode, maybe. It wastes valuable nanoseconds (if not
microseconds)
>>in Release Mode.

Yet more stupid advice. Is there no beginning to this man's talent?

You're on a roll today Richard, tossing your civility out the window
Hypocrisy. In any case, you have surrendered your right to demand civility
from anyone else, by showing such appalling incivility to Keith Thompson -
as you are well aware.
so
that people will pay attention to your dim-witted attitude. You've yet to
call me a bonehead though -- that _really_ sealed the argument for you the
last time.
It seems that the point of that "bonehead" comment passed right over your
head.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 1 '06 #12
Richard Heathfield posted:
Hypocrisy. In any case, you have surrendered your right to demand
civility from anyone else, by showing such appalling incivility to Keith
Thompson - as you are well aware.
Allow me to reshape your words in an effort to show you how we express
ourselves in the non-fascist world:

"Hypocrisy, in my opinion. In any case, I think you have surrendered your
right to demand civility from anyone else, by showing what I deem to be
appalling incivility to Keith Thompson -- as I believe you are well aware."

In any case, may we end this Babysitters' Club masquerade? Neither you nor
Keith like me, that's fine -- I haven taken quite a disliking to the both of
you. Notwithstanding that, I'll continue to post here and respond to whatever
post captures my interest with regard to C programming, regardless of the
post's author. So do me a favour and save all your little diatribes for your
next teen book. If you want to talk about C, you're in the right place. I
quite like how Keith Thompson has accepted his disliking of me and ceased
responding -- please follow suit, it will make comp.lang.c a more pleasant
place.

--

Frederick Gotham
Oct 1 '06 #13
Eric Sosman said:
jacob navia wrote:
>Frederick Gotham wrote:
>>Richard Heathfield posted:

Initialise every pointer.

Initializing every pointer does no harm, [...]

It does no harm to the running program, certainly. But
it *does* harm the process of developing the program, by
removing the compiler's ability to warn about certain kinds
of errors.
I beg to differ. I mean, yes, it has the effect that you say it has - on
some compilers, anyway - but it doesn't harm the development process at
all.
Example:

char *next_field(cha r **start)
{
char *p /* = NULL */, *q /* = NULL */;
/* Skip white space to find the start of the field: */
p = *start + strspn(*start, " \t\f\r\n");
/* Skip non-whites to find the end of the field: */
p = p + strcspn(p, " \t\f\r\n");
/* Record where the next search should start: */
*start = q + (*q != '\0');
/* Zero-terminate the field just located: */
*q = '\0';
/* Return a pointer to its beginning: */
return p;
}
Great example. Well done. If you omit the initialisation, okay, let's say
the compiler issues a warning (despite the fact that it needn't and some
don't). But you know and I know that some people will say "oh, it's only a
warning, it's fine", and they'll be scratching their heads trying to debug
it. Whereas, if you set q to NULL, then it doesn't take a genius to
discover that *start is being set to an obviously silly value (probably
NULL, possibly 1). So the debugging process is very swift after all.
The cheapest errors are those not made in the first place.
The next-cheapest are those caught by the compiler and fixed
before committing the code.
That's fine, provided people treat diagnostic messages seriously. We have
ample evidence here on comp.lang.c that this is not the case.
Wanton initialization of pointers (of any variables,
actually) discourages the compiler's assistance and therefore ought
not to be indulged in.
Giving the program deterministic behaviour by ensuring that all variables
are initialised helps the programmer to understand the program better and
debug it more quickly, and therefore ought to be encouraged. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 1 '06 #14
Frederick Gotham said:
In any case, may we end this Babysitters' Club masquerade? Neither you nor
Keith like me, that's fine
You are mistaken. I got on with you just fine until you called Keith a
fascist. I continue to hope that you will apologise to him because I
continue to believe you are a reasonable man, albeit somewhat stubborn.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 1 '06 #15
Frederick Gotham wrote:
Richard Heathfield posted:
Set indeterminate ('dangling') pointers to NULL.


In Debug Mode, maybe. It wastes valuable nanoseconds (if not microseconds)
in Release Mode. Maybe something like the following would be a compromise:

#ifdef NDEBUG
#define DMODE(statement )
#else
#define DMODE(statement ) statement
#endif
Yikes, that sounds like a great way to introduce heisenbugs. I could
_maybe_ see this in a very tight inner loop, but aside from that it's
unlikely that the time difference would even be measureable within the
context of the whole program running, let alone noticeable.

Oct 1 '06 #16
Richard Heathfield wrote:
Eric Sosman said:

>>jacob navia wrote:
>>>Frederick Gotham wrote:
Richard Heathfield posted:

>Initiali se every pointer.

Initializi ng every pointer does no harm, [...]

It does no harm to the running program, certainly. But
it *does* harm the process of developing the program, by
removing the compiler's ability to warn about certain kinds
of errors.


I beg to differ. I mean, yes, it has the effect that you say it has - on
some compilers, anyway - but it doesn't harm the development process at
all.

>>Example:

char *next_field(cha r **start)
{
char *p /* = NULL */, *q /* = NULL */;
/* Skip white space to find the start of the field: */
p = *start + strspn(*start, " \t\f\r\n");
/* Skip non-whites to find the end of the field: */
p = p + strcspn(p, " \t\f\r\n");
/* Record where the next search should start: */
*start = q + (*q != '\0');
/* Zero-terminate the field just located: */
*q = '\0';
/* Return a pointer to its beginning: */
return p;
}


Great example. Well done. If you omit the initialisation, okay, let's say
the compiler issues a warning (despite the fact that it needn't and some
don't). But you know and I know that some people will say "oh, it's only a
warning, it's fine", and they'll be scratching their heads trying to debug
it. Whereas, if you set q to NULL, then it doesn't take a genius to
discover that *start is being set to an obviously silly value (probably
NULL, possibly 1). So the debugging process is very swift after all.
This programmer you describe seems a bit of an odd fish. He's
reckless enough to ignore warning messages, yet diligent enough to
initialize all his pointers. Sounds like a person with a multiple
personality disorder ...
> The cheapest errors are those not made in the first place.
The next-cheapest are those caught by the compiler and fixed
before committing the code.

That's fine, provided people treat diagnostic messages seriously. We have
ample evidence here on comp.lang.c that this is not the case.
It's interesting to read this in light of remarks about "drool-
proof languages" on another current thread ...

I feel -- without quantitative evidence, I admit -- that there's
more to be gained from improving a programmer's skills than by trying
to compensate for his deficiencies. A person can be taught to pay
attention to warnings, or to initialize everything in sight so the
warnings go away and their Heisenbugs become reproducible. The first
course seems to me to offer more benefits, on balance, than the second.
Yes, a Heisenbug can be a royal PITA -- but it's not a certainty that
a reproducible error arising from a wrongly-initialized variable will
be reliably detected, either. Even if that variable is a pointer and
the initialization is to NULL, it's not a sure bet that there will be
a catastrophic failure, easily spotted: Maybe the pointer will be used
as the argument to fflush() or the first argument to strtok() or in
some other context where NULL is perfectly legal -- but perhaps not
what was intended.
>>Wanton initialization of pointers (of any variables,
actually) discourages the compiler's assistance and therefore ought
not to be indulged in.

Giving the program deterministic behaviour by ensuring that all variables
are initialised helps the programmer to understand the program better and
debug it more quickly, and therefore ought to be encouraged. :-)
Get rid of the STOP sign at the busy intersection, but make sure
that anyone who fails to stop *will* be run over by a cement truck.
Well, there won't be many repeat offenders ... (An old Dilbert strip
involved Dogbert trying to teach common sense to those who lacked it
rather conspicuously. "Larry the auto mechanic liked to smoke cigars
while working on gasoline engines," says Dogbert. "Can anybody think
of a problem this might cause?" The answer comes from someone swathed
in bandages and smoking a cigar: "He gets struck by lightning every
time?")

--
Eric Sosman
es*****@acm-dot-org.invalid

Oct 1 '06 #17
Richard Heathfield wrote:
Eric Sosman said:

>>jacob navia wrote:
>>>Frederick Gotham wrote:
Richard Heathfield posted:

>Initiali se every pointer.

Initializi ng every pointer does no harm, [...]

It does no harm to the running program, certainly. But
it *does* harm the process of developing the program, by
removing the compiler's ability to warn about certain kinds
of errors.


I beg to differ. I mean, yes, it has the effect that you say it has - on
some compilers, anyway - but it doesn't harm the development process at
all.
Thank goodness C99 gave us the ability to declare variables at point of
use, this entire debate becomes moot.

--
Ian Collins.
Oct 1 '06 #18
Richard Heathfield wrote:
it. Whereas, if you set q to NULL, then it doesn't take a genius to
discover that *start is being set to an obviously silly value (probably
NULL, possibly 1). So the debugging process is very swift after all.
Right. There should be an immediate crash upon (*q != '\0') .
Hopefully.
That's fine, provided people treat diagnostic messages seriously. We have
ample evidence here on comp.lang.c that this is not the case.
Yes. But is that really a good enough reason to say "always initialize every
pointer and tie your shoes before going outdoors?" Might as well never
use malloc and always use calloc while we're at it.
Giving the program deterministic behaviour by ensuring that all variables
are initialised helps the programmer to understand the program better and
debug it more quickly, and therefore ought to be encouraged. :-)
I pretty much agree - but after a certain point where it reaches
compulsiveness, then it starts to head into doubting something you already
know or have enough experience with that pre-initializing ends up being token
exercise. What I mean by this is that, given a long chain of string
wrangling, like Eric pointed out, setting a pointer to NULL, beforehand, as a
method of insta-crash due to coding mistakes is almost like saying "I am
initializing this because I might make a mistake here." as opposed to
something like:

list_s *head = NULL;

Which has a functional purpose rather than a "I might screw up" purpose.
Oct 1 '06 #19
Ian Collins wrote:
Thank goodness C99 gave us the ability to declare variables at point of
use, this entire debate becomes moot.
Yea instead the code looks like crap to read.
Oct 1 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4942
by: Advocated | last post by:
Hi there, just wondering if anyone uses visual studio 6? Im having real problems, just with debugging, i could either do with a really good tutorial, if anyone knows of one, but the main thing is, when i run the debugger, as soon as it gets to my first printf statement, its asking me "please enter the path for PRINTF.C" Any ideas?
10
3679
by: ibic | last post by:
Just curious: is it possible to recursively list all the directorys/files inside a given directory using standard c i/o library routines only, which can be re-compiled and run on any os supportes c compiler? Or this is too os dependent, system-specific functions must be called? I think about this when i tried to do this under windows, i found in order to achieve this, some windows-specific api such as FindFirstFile, FindNextFile must be...
4
7067
by: yinglcs | last post by:
I am trying to debug a C++ program in GDB on linux. I want to dump out the content of the "this" object, Here is what I get: (gdb) print *this $2 = {path = <incomplete type>} My question is why I don't see the content of 'path'? It said '<incomplete type>'. In the code, path is:
22
2519
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
85
4884
by: fermineutron | last post by:
Some compilers support __asm{ } statement which allows integration of C and raw assembly code. A while back I asked a question about such syntax and was told that __asm is not a part of a C standard. My question now is: Is there a chance that such statement will become a part of C standard in the future? In some cases using asm language is the best way to acomplish some small task, hence integration of C and asm would greatly enhence C,...
5
7804
by: phnimx | last post by:
Hi , We have developed a number of plug-in .NET Library Components that we typically deploy with our various applications by installing them into the GAC. Each of the applications contains an app.config file referencing arbitrary versions of the plug-in components they wish to consume. Here's the problem: Assuming I have installed any one of our application software,
20
3547
by: J de Boyne Pollard | last post by:
MThe library functions which are included to allow process Mlaunch, forking, and termination, imply that it is both Mpossible and desirable for a process to fork itself. This is Ma fundamental part of the Unix thought process, but is Mnot essential for a working OS. There are no Standard C Library functions for process forking or overlaying. The fork() and execve() functions are part of the C language bindings to POSIX. They are not...
6
2265
by: Jeff | last post by:
I understand there's socket / recursive calls here, but this is a C question, really it is: Here's the code block in question: #if defined(__GLIBC__) { struct hostent hent; char hbf; memset(&hbf, '\0', 8192);
33
2892
by: fmassei | last post by:
Hello! I made a short piece of code that I find very useful for debugging, and I wanted to ask you if it is correct, somehow acceptable or if I simply reinvented the wheel. To deal with some bad bugs caused by memory leaks I ended up with this simple solution: I made one header file that, when included, replaces the malloc/calloc/realloc/free functions with some other functions that do the actual job and insert (or remove) the pointers to...
0
9826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9674
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10560
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10604
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5659
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4466
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.