Monthly Archives: January 2016

Linux Kernel Analysis: Enable Linux Kernel Stack Dumping

As we know, if we want to know a special function and who will call it.
One of way is to use “cscope” to static analysis.

The following step is how we do it:

Enabling Stack Dumping in Linux Kernel
Enabling in Kernel Config
To enable the dump_stack() function in the kernel config the following options must be set. You can use make menuconfig or make xconfig to do this.

Kernel hacking -> Kernel debugging
Kernel hacking -> Verbose kernel error messages

1

Enabling these two options will change the dump_stack() function from a do nothing function to dumping the stack.

You need to rebuild your Linux kernel image after enabling these options.

Using dump_stack()
Using the dump_stack() function is as easy as calling dump_stack() wherever you wish to print out the stack. This will cause a stack trace to be printed at that point.

TCC source code — define_stack and tcc_alloc

define_stack is good example of lots of stack using TCC. The following is example of print Define Stack.

1
2
3
4
5
6
7
8
9
10
11
void printDefineStack(){
  Sym *p = define_stack;
  for(;;){
    if(p == NULL){
      break;
    }
 
    printf("%d\n",p -> v);
    p = p -> prev; 
  }
}

tcc_alloc is function which is allocated tcc object, It is very simple and used everywhere in tcc. Here is example on how to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void tcc_alloc_test()
{
  int i = 0;
  int len =  3;
  char *str = "int";
  TokenSym *ts;  
  table_ident = tcc_malloc((TOK_ALLOC_INCR) * sizeof(TokenSym *));
 
  ts = tcc_malloc(sizeof(TokenSym) + len);
  table_ident[i] = ts;
 
  ts->sym_define = NULL;
  ts->sym_label = NULL;
  ts->sym_struct = NULL;
  ts->sym_identifier = NULL;
  ts->len = len;
  ts->hash_next = NULL;
  memcpy(ts->str, str, len);
  ts->str[len] = '\0';
 
}

Google CodeJam – Mushroom Monster

Question URL is https://code.google.com/codejam/contest/4224486/dashboard

This question could be become two sub question.
1. less mushroom to eat.
for array 10 5 15 5, what is the sum of decreased numbers?
10 is decreased to 5, the decreased number is (10-5).
5 raises to 15, it will be ignored.
15 is decreased to 5, the decreased number is (15-5)

the sum of all decreased numbers that is (10-5)+(15-5) will be the answer.

2. eating mushroom in static speed.

find the max gap of decreased numbers. (15-5) is the max speed in 10 seconds.
there for, if mushroom number on the dish is greater than (15-5), Kaylin only eats(15-5).

if mushroom number on the dish is less that (15-5), Kaylin will finish all of them and waiting for next refill.

Here is Clang code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  int N;
  int L;
  int i,j,k;
  int array[MAXSIZE];
  int sum1 = 0;
  int sum2 = 0;
 
  scanf("%d\n",&N);
 
  for(i=0;i<N;i++)
  {
    scanf("%d",&L);
    memset(array,0,sizeof(int)*MAXSIZE);
    sum1 = 0;
    sum2 = 0;
    for(j = 0;j<L;j++){
      scanf("%d",&array[j]);
    }
 
    //start
    for(j = 0;j<L - 1;j++){
      if(array[j]>array[j + 1])
        sum1 += (array[j] - array[j + 1]); 
    }
 
    int max = 0;
 
    for(j = 0;j<L - 1;j++){
      if(array[j]>array[j + 1]){
        if(max<(array[j] - array[j + 1])){
          max = array[j] - array[j + 1];
        }
      }
    }
 
    for(j = 0;j<L - 1;j++){
      sum2 += (array[j]>max?max:array[j]); 
    }
 
    printf("Case #%d: %d %d\n",(i + 1),sum1,sum2);
  }