473,890 Members | 1,345 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 4020
Frederick Gotham wrote:
Keith Thompson posted:
If malloc'ing less than a kilobyte, I wouldn't bother.
That is extraordinarily bad advice.

Of course, it depends on the requirements of the project and so forth, but
there are many circumstances in which I'd just assume that malloc succeeds.
Do you do this on code that others use and count on, or just personal
toy programs?

If you do this on code that others use and count on, then I think
poorly of your judgement and moreover of the abilities of those that
review and approve of your code. This is bad practive, and bad advice
to give. small *alloc calls can and do fail, and dealing with the
failure (even if by just an error message and exit) at the point of
failure is really the thing to do in basically all serious projects.
If you think the opinions of the posters of the board are without
weight, how about this from Kernighan and Pike's "The Practice of
Programming", page 14, "...in a real program the return value of
malloc, reallloc, strdup, or any other allocation routine should always
be checked". I suggest reading that book, it has lots of helpful ideas
about developing quality software.

You are currently arguing for this assertion.

Doing B depends on A succeeding. Don't check that A succeeded before
doing B.

Sounds good, no?

-David

Oct 3 '06 #51
Richard Heathfield posted:
>Of course, it depends on the requirements of the project and so forth, but
there are many circumstances in which I'd just assume that malloc
succeeds.

It's still a stupid assumption, no matter how many times you make it.

(1) Let's say we're running our program.
(2) Let's say we want to allocate 8 bytes.
(3) We call malloc.
(4) It fails.
(5) We must save our user's data and exit the program.
(6) But we must allocate 74 kilobytes in order to save the user's data.
The point I'm making is that it depends on the amount of memory you're trying
to allocate. If you're trying to allocated 6.7 MB, then by all means, take
precautions and preserve the user's data. If you're trying to allocate 13
bytes... well... abandon ship.

>Here's an example of where I'd be wreckless with malloc:

We have no shortage of examples of stupid code.

You cannot assert that the code is stupid unless you know the details of the
project. If the failure of the invocation of "TruncateAndToU ppercase" leads
to an irreperable situation, then it might be quite pertinent to exit.

Why add to the mess? And your code is far from wreckless. Spelling
flames for the sake of it are lame, but the difference in meaning
between "reckless" and "wreckless" is significant.

Forgive my ignorance, but allow me a minute's recess to visit
dictionary.com. ..

I should have written "reckless" rather than "wreckless" .

--

Frederick Gotham
Oct 3 '06 #52
Frederick Gotham said:
Richard Heathfield posted:
>>Of course, it depends on the requirements of the project and so forth,
but there are many circumstances in which I'd just assume that malloc
succeeds.

It's still a stupid assumption, no matter how many times you make it.


(1) Let's say we're running our program.
(2) Let's say we want to allocate 8 bytes.
(3) We call malloc.
(4) It fails.
You'll never know this unless you check, of course. And that's why you have
to check. So That You Know.
(5) We must save our user's data and exit the program.
(6) But we must allocate 74 kilobytes in order to save the user's data.
Fine, so you allocate an emergency reserve at the beginning of the program,
to cover your needs (and you try to find a smarter way to preserve the
data, if possible - one that doesn't need so much data). I've been
explaining this for years, to anyone who'll listen. Few do. The cliffs look
so inviting, and the sea down there is so pretty...
The point I'm making is that it depends on the amount of memory you're
trying to allocate.
No, it doesn't. That's ludicrous. What it depends on is whether you want
your program to work robustly. The kind of attitude you are espousing here
is typical amongst a great many "commercial " programmers, and it's one
reason why a great many "commercial " programs are a load of rubbish.
If you're trying to allocated 6.7 MB, then by all
means, take precautions and preserve the user's data. If you're trying to
allocate 13 bytes... well... abandon ship.
So you are claiming that the user's data is less important if you're only
trying to allocate a small amount of memory? Where's the logic in that?
That's just ridiculous! What if your word processor behaved that way? You'd
be furious, and rightly so.

>>Here's an example of where I'd be wreckless with malloc:

We have no shortage of examples of stupid code.


You cannot assert that the code is stupid unless you know the details of
the project.
Yes, I can. It was evident from looking at the code that it was stupid. Even
the author wasn't sure it was correctly written. And of course there is my
rule of thumb: "never trust the code of anyone too stupid to check malloc,
too boorish to apologise for their libels, or both".

--
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 3 '06 #53
Richard Heathfield posted:
>You cannot assert that the code is stupid unless you know the details
of the project.

Yes, I can. It was evident from looking at the code that it was stupid.
Even the author wasn't sure it was correctly written.
You have convinced me. My main peeve against checking malloc's of small
quantities of memory is that it's tedious, and so I think "ExitiveMal loc"
would relieve a bit of stress, not to mention reduce the signal-to-noise
ratio of the code (who wants to read countless lines of error-checking?).
Taking your advice on board though with regard to pre-allocating some reserve
memory in case of emergency, I've cooked up the following. It's a quick
sample of a program which holds important user data in memory -- data which
must be preserved even when the program fails. I think the ExitiveMalloc
works quite well.

---------- FILE BEGIN: exitmal.h ----------

#include <stddef.h>
void *ExitiveMalloc( size_t);

---------- FILE END: exitmal.h ----------

---------- FILE BEGIN: exitmal.c ----------

#include <stddef.h>
#include <stdlib.h>

void *ExitiveMalloc( size_t const size)
{
void *const p = malloc(size);

if(!p) exit(EXIT_FAILU RE);

return p;
}

---------- FILE END: exitmal.c ----------

---------- FILE BEGIN: core.c ----------

#include <stdlib.h>

#include "exitmal.h"

void *mem_needed_for _saving;
int backup_required = 0;

void SetupBackupSyst em(void)
{
void InitiateEmergen cyBackupSequenc e(void);

mem_needed_for_ saving = ExitiveMalloc(9 8304U);
atexit(Initiate EmergencyBackup Sequence);
}

void InitiateEmergen cyBackupSequenc e(void)
{
if(backup_requi red)
{
/* Use pre-allocated memory to facilitate in saving */
}
}

int main(void)
{
SetupBackupSyst em();

/* Let's pretend that this is a circuit schematic package,
and that the user is working on a document. */

/* Now let's say that TruncateAndToUp percase is called, and
that it fails to allocate 23 bytes. TruncateAndToUp percase
contains a call to ExitiveMalloc, and so "exit" is called
upon the allocation failure. */

return 0;
}

---------- FILE END: core.c ----------
If any function in the program invokes ExitiveMalloc, we still preserve our
user's precious data. What do you think?

--

Frederick Gotham
Oct 3 '06 #54
jacob navia <ja***@jacob.re mcomp.frwrites:
He is running linux, then I assume memory protection!
Who cares about each possible CPU in the planet?
He is in a certain situation, and give specific advice to THAT
situation!
<OTAnd even in that specific situation, it's bad advice. There's no
guarantee that a pointer with a random value will point to memory that
that process does not have access to. Memory protection only works
when you attempt to scribble on memory belonging to another process,
not when you attempt to scribble on memory belonging to your own
process. </OT>

This is why you're consistently admonished to keep your posts on the
topic of Standard C.

Charlton


Oct 3 '06 #55
Frederick Gotham wrote:
Richard Heathfield posted:
>>You cannot assert that the code is stupid unless you know the details
of the project.
Yes, I can. It was evident from looking at the code that it was stupid.
Even the author wasn't sure it was correctly written.

You have convinced me. My main peeve against checking malloc's of small
quantities of memory is that it's tedious, and so I think "ExitiveMal loc"
would relieve a bit of stress, not to mention reduce the signal-to-noise
ratio of the code (who wants to read countless lines of error-checking?).
I would like to read the countless lines of error checking
because I had bad experience with other people's code when they
don't do it and I have to write it myself to find out the problem
with buggy code that has too many failure points that are unchecked.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Oct 3 '06 #56
Frederick Gotham said:
What do you think?
I think you owe Keith Thompson an apology for behaving abusively towards
him.

--
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 3 '06 #57
In article <87************ @mithril.chroma tico.net>,
Charlton Wilbur <cw*****@mithri l.chromatico.ne twrote:
><OTAnd even in that specific situation, it's bad advice. There's no
guarantee that a pointer with a random value will point to memory that
that process does not have access to. Memory protection only works
when you attempt to scribble on memory belonging to another process,
not when you attempt to scribble on memory belonging to your own
process. </OT>
Depends on the system. It isn't uncommon at all for systems to be
able to offer protection against writing to particular pages.
That would protect a process from itself.

For an obvious example of this, think of compilers that put
string literals into non-writable memory.

Per-page memory protection bits that I've heard of on various systems
include: Read, Write, Execute, and Copy-on-Write

On most of the systems, Execute implies Read as well, but on
some apparently it doesn't: those systems can be configured so that
no -explicit- read access to the pages are made.

>This is why you're consistently admonished to keep your posts on the
topic of Standard C.
Unfortunately, your technical correction was itself flawed. And
probably -my- technical correction to your correction has some flaw
as well. It would have been better if Jacob hadn't made the
non-portable assumption, which probably wasn't even completely
portable within the domain of systems ("Linux") he was intending
his advice to apply to.
--
Prototypes are supertypes of their clones. -- maplesoft
Oct 3 '06 #58
Frederick Gotham <fg*******@SPAM .comwrites:
Keith Thompson posted:
>>If malloc'ing less than a kilobyte, I wouldn't bother.

That is extraordinarily bad advice.

Of course, it depends on the requirements of the project and so forth, but
there are many circumstances in which I'd just assume that malloc succeeds.
[snip]

People should of course decide for themselves, but I personally
recommend avoiding any advice offered by Frederick Gotham. Anyone who
(a) advocates failing to check the result of malloc() and (b)
stubbornly refuses to apologize for repeated libel is not to be
trusted.

Frederick: I again call on you to apologize for your earlier insults.
(You know perfectly well what I'm talking about.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 3 '06 #59
Charlton Wilbur wrote:
jacob navia <ja***@jacob.re mcomp.frwrites:

>>He is running linux, then I assume memory protection!
Who cares about each possible CPU in the planet?
He is in a certain situation, and give specific advice to THAT
situation!


<OTAnd even in that specific situation, it's bad advice. There's no
guarantee that a pointer with a random value will point to memory that
that process does not have access to. Memory protection only works
when you attempt to scribble on memory belonging to another process,
not when you attempt to scribble on memory belonging to your own
process. </OT>

This is why you're consistently admonished to keep your posts on the
topic of Standard C.

Charlton

According to the standard, malloc can return either NULL or
a VALID pointer so your argumentation makes no sense.

Malloc can never return an invalid pointer (at least according
to the specs).

You are confusing. I am speaking about not testing the result
of malloc, not using uninitialized variables or whatever!!!

The idea is that in a debug setting, a failed
malloc means that a bad size was passed in. In that case it
is better to leave the program crash right there and see
with the debugger what is happening.

This is one way of debugging software. It is maybe not
the best, and relies on a specific OS characteristics .

Oct 3 '06 #60

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

Similar topics

7
4945
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
3683
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
7071
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
2524
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
4897
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
7806
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
3551
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
2267
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
2893
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
9980
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
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9641
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
8018
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
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5855
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...
0
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.