Home > Enterprise >  Print address function
Print address function

Time:10-19

How can I print the address of the instruction I am executing? For example let's consider the following main:

int main( void )
{
    //printf("......\n");
    printf("...Main...\n");
    uint32_t nb,delay;
    uint16_t result;
    DD_SPI_STATUS  status;
    //Get SPI driver instance
    DD_DRIVER_SPI* SPIdrv = DD_SPI_GetDriver(0);
    printf("...DD_DRIVER_SPI* SPIdrv = DD_SPI_GetDriver(0)...\n");
    DD_IRQ_Init();
    printf("...DD_IRQ_Init()...\n");

    // Initialize buffers
    for (nb = 0; nb < sizeof(TxBuffer); nb  )
    {
        TxBuffer[nb] = nb & 0xFF;
    }

    // Initialize the SPI driver
    SPIdrv->Initialize(SPI_callback);
    printf("...SPIdrv->Initialize(SPI_callback)...\n");
    // force driver reset
    SPIdrv->Control( DD_SPI_MODE_INACTIVE, 0);
    printf("...SPIdrv->Control( DD_SPI_MODE_INACTIVE, 0)...\n");
return 0;
}

How do I print the address of the instruction SPIdrv-> Control (DD_SPI_MODE_INACTIVE, 0); in such a way that by going to see the disassembled code I can immediately identify where I am?

CodePudding user response:

you can print an address using %p formatting:

printf("SPIdrv->Initialize=%p\n", (void*)SPIdrv->Initialize);

There's no standard way to print the contents of the function as instructions. But if you google "C disassembler library" you'll find some functions you can download.

If you're trying to print the address in main() where you're calling the function, I don't think that's possible at all in standard C. GCC has an extension that allows you to get the address of a label, so you could do:

here: 
SPIdrv->Initialize(SPI_callback);
printf("Called SPIdrv->Initialize at %p\n", &&here);

CodePudding user response:

It's non-standard, but if you're using gcc there's an extension to get the address of a label, so if you had:

debug_point:
    SPIdrv->Control( DD_SPI_MODE_INACTIVE, 0);

then &&debug_point would be a void * to that location in the code.

  •  Tags:  
  • c
  • Related