0

This is a code in linux (5.4.21)
When I use a virtual machine and connect gdb to the linux process, I can use break points and follow code. For example, I set breakpoint on a function arm_smmu_device_probe. When I follow with 'next' command, I see some values, for example, 'smmu' or 'dev' below are shown to have been optimized out. How can I make them not optimized out so that I can see them in gdb?

static int arm_smmu_device_probe(struct platform_device *pdev)
{
    int irq, ret;
    struct resource *res;
    resource_size_t ioaddr;
    struct arm_smmu_device *smmu;
    struct device *dev = &pdev->dev;
    bool bypass;

    smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
    if (!smmu) {
        dev_err(dev, "failed to allocate arm_smmu_device\n");
        return -ENOMEM;
    }
    smmu->dev = dev;

    if (dev->of_node) {
        ret = arm_smmu_device_dt_probe(pdev, smmu);
    } else {
        ret = arm_smmu_device_acpi_probe(pdev, smmu);
        if (ret == -ENODEV)
            return ret;
    }

I tried chaning -O2 to -Og in the top Makefile but the kernel build fails then.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • Enable the `CONFIG_DEBUG_INFO` for the starter. – 0andriy Oct 05 '21 at 21:44
  • @0andriy it is already set. that's why I can track the code and can see some values. (For kernel build, CONFIG_DEBUG_INFO, CONFIG_DEBUG_KERNEL, CONFIG_MAGIC_SYSRQ, CONFIG_DEBUG_FS, CONFIG_HAVE_HW_BREAKPOINT, CONFIG_HAVE_MIXED_BREAKPOINTS_REGS were all set to ‘y’) – Chan Kim Oct 06 '21 at 00:50
  • So, are you looking into a macro or inline design function? – 0andriy Oct 06 '21 at 06:07
  • No, devm_kzalloc is a inline function and it calls a normal function. I tried changin the CFLAGS for specific files like in http://www.joelfernandes.org/linux/2018/06/10/kernel-gdb.html, but it gives me error : see http://lists.kernelnewbies.org/pipermail/kernelnewbies/2021-October/022195.html – Chan Kim Oct 06 '21 at 07:01

1 Answers1

0

Recently I found how to do this. (from Is there a way to tell GCC not to optimise a particular piece of code?, flolo's answer)
If you want a function aaa(...) not to be optimzed, you can do it like this.

#pragma GCC push_options
#pragma GCC optimize ("O0")
aaa ( ... ) 
{
  function body
}
#prgma GCC pop_options

In some cases, this putting #pragma causes some discrepancy between the #include header file and the function source. So in this case (not often) you need to add this #praga around the corresponding #include statement. If linux/bbb.h causes this kind of problem, do this.

#pragma GCC push_options
#pragma GCC optimize ("O0")
#include <linux/bbb.h>
#pragma GCC pop_options

This works sure and I'm enjoying(?) debug/analysis this way.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112