Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Yao Qi <qiyaoltc@gmail.com>
To: Yao Qi <qiyaoltc@gmail.com>
Cc: Philipp Rudo <prudo@linux.vnet.ibm.com>,
	 gdb-patches@sourceware.org,  peter.griffin@linaro.org,
	 yao.qi@arm.com,  arnez@linux.vnet.ibm.com
Subject: Re: [RFC 3/7] Add basic Linux kernel support
Date: Thu, 09 Feb 2017 13:06:00 -0000	[thread overview]
Message-ID: <86wpczo6o9.fsf@gmail.com> (raw)
In-Reply-To: <20170207105403.GA1630@E107787-LIN> (Yao Qi's message of "Tue, 7	Feb 2017 10:54:03 +0000")

Yao Qi <qiyaoltc@gmail.com> writes:

> I am playing your first three patches on x86_64 with some hacks.
> I write a small program with the same linux kernel "signature", and
> want GDB treat it as a linux kernel.
>

I make some progress on writing such small test case,
see the code below.  I hacked lk_try_push_target not to do the
sanity check, and not to call lk_try_push_target in
lk_observer_inferior_created, so that I can push this target layer when
I want.

(gdb) break stop^M
Breakpoint 2 at 0x400711: file /home/yao/SourceCode/gnu/gdb/git/gdb/testsuite/gdb.base/linux-kernel.c, line 104.^M
(gdb) continue^M
Continuing.^M
^M
Breakpoint 2, stop () at /home/yao/SourceCode/gnu/gdb/git/gdb/testsuite/gdb.base/linux-kernel.c:104^M
104     {}^M
(gdb) PASS: gdb.base/linux-kernel.exp: continue to breakpoint: stop

at this point, the list of tasks are set up, switch to linux-kernel
target layer,

target linux-kernel^M
[New process 8001]^M
(gdb) PASS: gdb.base/linux-kernel.exp: target linux-kernel
maintenance print target-stack^M
The current target stack is:^M
  - linux-kernel (linux kernel support)^M
  - native (Native process)^M
  - exec (Local exec file)^M
  - None (None)^M

It works!  In this way, we can test that GDB can successfully parse the
these data structures in Linux kernel without Linux kernel image at all.

Then, we can generate a coredump,

(gdb) generate-core-file 
Saved corefile core.9614

Remove the hack in lk_observer_inferior_created, so that GDB can
automatically push linux-kernel target layer,

$ ./gdb ./testsuite/outputs/gdb.base/linux-kernel/linux-kernel ./core.9614
[New LWP 9614]
[New process 9614]
Core was generated by `/scratch/yao/gdb/build-git/x86_64/gdb/testsuite/outputs/gdb.base/linux-kernel/li'.
Program terminated with signal SIGTRAP, Trace/breakpoint trap.
#0  stop () at /home/yao/SourceCode/gnu/gdb/git/gdb/testsuite/gdb.base/linux-kernel.c:104
104	{}
[Current thread is 1 (PID:  9614*, 0x602010)]
(gdb) maintenance print target-stack 
The current target stack is:
  - linux-kernel (linux kernel support)
  - core (Local core dump file)
  - exec (Local exec file)
  - None (None)

The next step would be extending the test case to a multi-threaded
program, so that we can create task lists for these threads, and
generate coredump which is similar to the kernel coredump.

-- 
Yao (齐尧)

#include <unistd.h>
#include <stdlib.h>
#include <string.h>

static char linux_banner[10];
static int _stext = 0;
static int _etext = 0;

typedef int pid_t;

struct list_head
{
  struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) (struct list_head) { &(name), &(name) }

struct thread_struct
{};

struct task_struct
{
  struct list_head tasks;
  pid_t pid;
  pid_t tgid;
  struct list_head thread_group;
  char comm[20];

  struct thread_struct thread;
};

struct rq
{
  struct task_struct *curr;
};

#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define BITS_PER_BYTE           8
#define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof (long))

#define DECLARE_BITMAP(name,bits) \
  unsigned long name[BITS_TO_LONGS(bits)]

#define NR_CPUS 10

struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); };

struct cpumask __cpu_online_mask;

static void
cpumask_set_cpu (unsigned int cpu, struct cpumask* dstp)
{
  dstp->bits[cpu / (BITS_PER_BYTE * sizeof (long))]
    |= 1UL << (cpu % (BITS_PER_BYTE * sizeof (long)));
}

struct task_struct init_task;;
struct rq runqueues[NR_CPUS];
struct cpumask baz;

unsigned long __per_cpu_offset[NR_CPUS];

struct mm_struct
{};

struct mm_struct init_mm;


static void
setup (void)
{
  int i;
  struct task_struct *task;

  /* Set up __per_cpu_offset.  */
  for (i = 0; i < NR_CPUS; i++)
    __per_cpu_offset[i] = i * sizeof (runqueues[0]);

  /* Mark cpu 0 is online.  */
  cpumask_set_cpu (0, &__cpu_online_mask);

  init_task.tasks = LIST_HEAD_INIT(init_task.tasks);
  init_task.thread_group = LIST_HEAD_INIT(init_task.thread_group);

  task = malloc (sizeof (struct task_struct));
  memset (task, 0, sizeof (sizeof (struct task_struct)));

  task->pid = getpid ();

  runqueues[0].curr = task;

  /* Chain it tasks list.  */
  init_task.tasks.next = (struct list_head *) task;
  init_task.tasks.prev = (struct list_head *) task;
  task->tasks.next = (struct list_head *) &init_task;
  task->tasks.prev = (struct list_head *) &init_task;

  /* TASK is the group leader.  */
  task->thread_group = LIST_HEAD_INIT(task->thread_group);
}

static void
stop (void)
{}

int
main (void)
{
  setup ();

  stop ();
  return 0;
}


  parent reply	other threads:[~2017-02-09 13:06 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-12 11:32 [RFC 0/7] Support for Linux kernel debugging Philipp Rudo
2017-01-12 11:32 ` [RFC 3/7] Add basic Linux kernel support Philipp Rudo
2017-02-07 10:54   ` Yao Qi
2017-02-07 15:04     ` Philipp Rudo
2017-02-07 17:39       ` Yao Qi
2017-02-09  9:54         ` Philipp Rudo
2017-02-09 13:06     ` Yao Qi [this message]
2017-01-12 11:32 ` [RFC 1/7] Convert substitute_path_component to C++ Philipp Rudo
2017-01-12 11:32 ` [RFC 5/7] Add commands for linux-kernel target Philipp Rudo
2017-01-12 11:32 ` [RFC 2/7] Add libiberty/concat styled concat_path function Philipp Rudo
2017-01-12 12:00   ` Pedro Alves
2017-01-12 13:33     ` Philipp Rudo
2017-01-12 13:48       ` Pedro Alves
2017-01-12 15:09         ` Philipp Rudo
2017-01-12 15:42           ` Pedro Alves
2017-01-12 11:32 ` [RFC 7/7] Add S390 support for linux-kernel target Philipp Rudo
2017-01-12 17:09   ` Luis Machado
2017-01-13 11:46     ` Philipp Rudo
2017-02-06 15:52     ` Yao Qi
2017-02-06 18:48       ` Andreas Arnez
2017-01-12 11:32 ` [RFC 6/7] Add privileged registers for s390x Philipp Rudo
2017-01-12 12:56 ` [RFC 0/7] Support for Linux kernel debugging Philipp Rudo
2017-01-12 13:02 ` [RFC 4/7] Add kernel module support for linux-kernel target Philipp Rudo
2017-01-25 18:10 ` [RFC 0/7] Support for Linux kernel debugging Peter Griffin
2017-01-26 13:12   ` Philipp Rudo
2017-02-03 17:45     ` Yao Qi
2017-02-03 19:46       ` Andreas Arnez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86wpczo6o9.fsf@gmail.com \
    --to=qiyaoltc@gmail.com \
    --cc=arnez@linux.vnet.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=peter.griffin@linaro.org \
    --cc=prudo@linux.vnet.ibm.com \
    --cc=yao.qi@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox