What I need to do: Modify the Linux kernel code (3.13.6 downloaded from kernel.org). Find the number of times a kvm_vmx_exit_handler function is called.

What I am trying to do: Declare an array in the file 'x86.h'

 extern unsigned long int my_count_s[40];

the file x86.h which is being included in the 'vmx.c' file

#include "x86.h"

Now, I am trying to initialise an element in the array to the value 0 in the file 'vmx.c'.

my_count_s[1] = 0;

I will be incrementing it each time when an kvm_exit_handler function is called. And I will get to know the number of times the kvm took an exit when something happened, and the number of times a particular exit handling function is called. I will be using a single element of the array for each of the exithandler function.

Error I am facing:

arch/x86/kvm/vmx.c:58:1: warning: data definition has no type or storage class [enabled by default]
arch/x86/kvm/vmx.c:58:1: error: type defaults to ‘int’ in declaration of ‘my_count_s’ [-Werror=implicit-int]
arch/x86/kvm/vmx.c:58:1: error: conflicting types for ‘my_count_s’
arch/x86/kvm/x86.h:8:26: note: previous declaration of ‘my_count_s’ was here
arch/x86/kvm/vmx.c:58:1: error: invalid initializer

My understanding: I have declared the datatype of my_count_s[40] to be an unsigned long int, but why is it being defaulted to 'int' ? And even if it was defaulted ti data type 'int', why was there an initialization error for the line my_count_s[1] = 0; ?

NOTE : 1. Line 58 in vmx.c is the "my_count_s[1]=0;" 2. If I compile the kernel by just declaring the array, it works fine but the errors popup while I am trying to assign a value to an element in the array.

有帮助吗?

解决方案

Statements, including assignments have to be inside functions. I bet yours is outside a function.

The compiler is confused because = can only appear in declarations and statements, and since it's outside a function your line is parsed as a declaration. That just doesn't work; where's the type?

其他提示

The problem is that, even though you have this in your header file:

extern unsigned long int my_count_s[40];

you still haven't defined the array anywhere - you've only declared that it exists somewhere.

In some source file (probably arch/x6/kvm/vmx.c), you need to define the array:

unsigned long int my_count_s[40];

Remember, extern says "Hey compiler, this variable exists in some module." That's why we put extern declarations in header files. They don't actually cause any space to be allocated for the variable, they just indicate its existence.

The line without extern is the definition, and is put in a .c file. It is in that compilation unit that space is actually reserved for the variable.


On another note. Does this variable need to be globally-scoped? If arch/x6/kvm/vmx.c is the only file that references it, then it should not be in the header file, and should be marked as static in the .c file:

static unsigned long int my_count_s[40];

Here, static limits its visibility to only the C file that it is defined in.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top