473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

trapping library calls

How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the
real puts from libc?
I tried all kinds of special linker options, but it seems that when my puts
calls the real puts, it actually calls itself and gets into an infinite
loop. Is this kind of thing possible with the standard gcc and GNU ld
tools?
Thanks, Keith
Nov 13 '05 #1
9 1714
"Keith Briggs" <Ke**********@bt.com> wrote:
How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the
real puts from libc?


Portably, you can't. You can #define puts() to be something else, or
similar dirty tricks, but AFAIK they all invoke undefined behaviour, and
thus may work or not.
It's quite possible that your implementation does provide you with the
option to do this, but it's not required to; ask in a newsgroup
dedicated to your implementation.

Richard
Nov 13 '05 #2
In <bm**********@visp.bt.co.uk> "Keith Briggs" <Ke**********@bt.com> writes:
How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the
real puts from libc?
I tried all kinds of special linker options, but it seems that when my puts
calls the real puts, it actually calls itself and gets into an infinite
loop. Is this kind of thing possible with the standard gcc and GNU ld
tools?


It's not blessed by the C standard, but it should work:

1. Write a header (say myputs.h) like this:

#ifdef USE_MYPUTS
int myputs(const char *s);
#define puts myputs
#endif

This header will be included, *after* including <stdio.h>, in each
source file that needs to have its puts calls trapped. Feel free to add
guards against multiple inclusion, if you think it's worth it.

2. Provide a definition for myputs, in one of the source files, like this:

#include <stdio.h>

#ifdef USE_MYPUTS
int myputs(const char *s)
{
/* do whatever you need to do, including calling puts */
}
#endif

#include "myputs.h"

/* the rest of the code calling puts goes here */
/* it will actually call myputs instead */

Now, you can control the trapping of the puts calls by defining the
USE_MYPUTS macro on the compiler command line.

Strictly speaking, you're not allowed to define the puts macro after
including <stdio.h>, but there should be no problems in practice: it is
highly unlikely that other standard library macros used by your program
contain puts calls.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

"Keith Briggs" <Ke**********@bt.com> wrote in message
news:bm**********@visp.bt.co.uk...
How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the real puts from libc?
I tried all kinds of special linker options, but it seems that when my puts calls the real puts, it actually calls itself and gets into an infinite
loop. Is this kind of thing possible with the standard gcc and GNU ld
tools?
Thanks, Keith


There are several ways this can be done. I'll assume that this is a
temporary change and that you will eventually want to go back to calling the
"real" puts(). You can use a preprocessor macro such as:

#defined puts(X) my_puts(X)

and then write a definition such as:

int my_puts(const char *s)
{
/* Do all of your specific actions here */

#undef puts
puts(s); /* call the real puts() here */
}

If you do NOT plan on reverting to the original puts(), then just remove the
#define and #undef macros and globally replace all the puts() calls with
my_puts() calls. I hope that helps.
Nov 13 '05 #4
Thank you for the suggestions, but I didn't make it clear in my original
post that I do not have the source code. This needs to be done with an
already compiled application.
Keith

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"Keith Briggs" <Ke**********@bt.com> wrote:
How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the real puts from libc?


Portably, you can't. You can #define puts() to be something else, or
similar dirty tricks, but AFAIK they all invoke undefined behaviour, and
thus may work or not.
It's quite possible that your implementation does provide you with the
option to do this, but it's not required to; ask in a newsgroup
dedicated to your implementation.

Richard

Nov 13 '05 #5
"Keith Briggs" <Ke**********@bt.com> wrote:

[ Don't top-post, please. ]
Thank you for the suggestions, but I didn't make it clear in my original
post that I do not have the source code.
In that case, you're up a creek.
This needs to be done with an already compiled application.


And if this application is statically linked, you haven't got a paddle,
either. Forget it. If it's dynamically linked, there _may_ be hope, but
it would be highly risky and completely system-dependent and off-topic.

Richard
Nov 13 '05 #6
Keith Briggs wrote:
How can I trap library calls, say to puts, and get them to call a puts
function I write, which then performs some special action before calling the
real puts from libc?
I tried all kinds of special linker options, but it seems that when my puts
calls the real puts, it actually calls itself and gets into an infinite
loop. Is this kind of thing possible with the standard gcc and GNU ld
tools?
Thanks, Keith

It is likely possible -- but not in standard C (making your question
off topic here).

Ask in a newsgroup devoted to your platform
(news:comp.unix.programmer or news:gnu.gcc.help -- I'm guessing
here) and all shall be revealed!

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #7
*** evil topposting fixed ***

Keith Briggs wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
"Keith Briggs" <Ke**********@bt.com> wrote:
How can I trap library calls, say to puts, and get them to
call a puts function I write, which then performs some special
action before calling the real puts from libc?


Portably, you can't. You can #define puts() to be something
else, or similar dirty tricks, but AFAIK they all invoke
undefined behaviour, and thus may work or not.
It's quite possible that your implementation does provide
you with the option to do this, but it's not required to;
ask in a newsgroup dedicated to your implementation.


Thank you for the suggestions, but I didn't make it clear in
my original post that I do not have the source code. This
needs to be done with an already compiled application.


Then this is not a C language question, but something specific to
a system and implementation. Use of a debugger may help. At any
rate, look for a newsgroup that deals with your system.

When you find such, or if you come back here, do not top-post. It
is generally considered rude. Your answers belong after the
quoted material, or intermixed. Do snip out quoted portions that
are not germane.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #8

"Keith Briggs" <Ke**********@bt.com> wrote in message
news:bm**********@visp.bt.co.uk...
Thank you for the suggestions, but I didn't make it clear in my original
post that I do not have the source code. This needs to be done with an
already compiled application.


As others have said, at that point it isn't a C question anymore.

Assuming, though, that you consider an object file as a C binary file, which
it may or may not be, depending on the implementation, then some things can
be done in C.

I have before written programs that will recognize a given series of bytes
in a file and replace them with another set of bytes. (Assuming that the
system is byte oriented.) If you do that you can, for example, change
"puts" to "puty", or any other string of the same length. Most likely this
will change the name of the function called. You can then write a C
function with the same name to do something and then call the original. A
small number of object program formats contain CRC or checksums which must
be adjusted. There is some chance that the string may occur in other
places. For example, changes of printf would also change fprintf and
sprintf, unless those were specifically tested for.

The program described can be standard C, assuming that it is possible to
read and write object files as C binary files. I believe that is true in
both Unix and windows files.

-- glen

-- glen


Nov 13 '05 #9
In <Cu********************@rwcrnsc51.ops.asp.att.ne t> "Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> writes:

"Keith Briggs" <Ke**********@bt.com> wrote in message
news:bm**********@visp.bt.co.uk...
Thank you for the suggestions, but I didn't make it clear in my original
post that I do not have the source code. This needs to be done with an
already compiled application.


As others have said, at that point it isn't a C question anymore.

Assuming, though, that you consider an object file as a C binary file, which
it may or may not be, depending on the implementation, then some things can
be done in C.

I have before written programs that will recognize a given series of bytes
in a file and replace them with another set of bytes. (Assuming that the
system is byte oriented.) If you do that you can, for example, change
"puts" to "puty", or any other string of the same length. Most likely this
will change the name of the function called. You can then write a C
function with the same name to do something and then call the original. A
small number of object program formats contain CRC or checksums which must
be adjusted. There is some chance that the string may occur in other
places. For example, changes of printf would also change fprintf and
sprintf, unless those were specifically tested for.


Without an intimate knowledge of the object file format, you can't
perform such tests in a *reliable* fashion, i.e. a genuine printf may be
preceded by a byte that happens to have the value of 'f'.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

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

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
21
by: Neil | last post by:
Is there a way to trap an error generated in another app that is controlled via automation? I have an Access 2000 app that opens Word 2000 and proceeds to open a series of documents and, in each...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
19
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions...
4
by: Bill | last post by:
Despite our best efforts occasionally in an aspx file, something like <%=x%> where x is not defined sqeaks by and I get the ugly asp error message. I want to be able to identify this particular...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
1
by: Elmo Watson | last post by:
Up until now, I've done all the work (gui/front end programming, classes, etc) I've done all my error trapping (try/catch) in the methods in the code behind. Now I've got an opportunity to do...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.