Thread: What's the best way to provide a hookable interface?

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    What's the best way to provide a hookable interface?

    If have a "panic" function in my kernel, during VERY early boot it just prints to video memory. I want to have a way to hook it at run time. What's the best way to do that?

    Currently, I have a little abstraction. So that when I make the switch from compile time hookable to run time hookable, it will have no effect on code that uses panic.

    Here is panic.h:
    Code:
    #ifndef PANIC_H_INCLUDED
    #define PANIC_H_INCLUDED
    
    extern void panic(char *function, char *message);
    
    #endif /* PANIC_H_INCLUDED */
    panic.c
    Code:
    void print(char *message)
    {
        int i;
        static int j;
        char* video_mem = (char*)0xb8000;
        for(i = 0;message[i] != 0;i++)
        {
            if(j >= 0x1000)
                j = 0;
            video_mem[j*2] = message[i];
            video_mem[(j*2) + 1] = 7;
            j++;
        }
    }
    
    void panic(char *function, char *message)
    {
        print("panic: ");
        print(function);
        print(" : ");
        print(message);
    }
    Is there a way to do this w/o resorting to making a pointer to hold the location of the hook and calling the pointer(with panic as a wrapper to call the real function that hooked it)?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm not sure what you've got against function pointers.

    Personally, I'd do something like this
    Code:
    static void print(char *message)
    {
        int i;
        static int j;
        char* video_mem = (char*)0xb8000;
        for(i = 0;message[i] != 0;i++)
        {
            if(j >= 0x1000)
                j = 0;
            video_mem[j*2] = message[i];
            video_mem[(j*2) + 1] = 7;
            j++;
        }
    }
    
    // start by dumping to VGA memory
    static void (*pf)(char *message) = print;
    
    void panic(char *function, char *message)
    {
        pf("panic: ");
        pf(function);
        pf(" : ");
        pf(message);
    }
    
    // This is called when the proper code has
    // advanced enough to do something useful
    void newPanic ( void (*newfunc)(char *message) )
    {
        pf = newfunc;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well you've asked for the best way, and although this may seem to be a subjective opinion, a function pointer would be the best and most common way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I don't have anything against function pointers. I just thought it looked like a hack, something not good to include in what will hopefully be lasting code. The way I implement function pointers was probably the root of my misconception. Before seeing the code Salem posted, I'd have done it in a messier way.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Calling IRichEditOle interface methods
    By Niara in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 01:23 PM
  3. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 02:29 AM
  4. marshall interface needed?
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2008, 07:15 PM
  5. OOP in C
    By lyx in forum C Programming
    Replies: 4
    Last Post: 11-23-2003, 01:12 PM