Arm Linux Kernel Hacks

rousalome.egloos.com

포토로그 Kernel Crash


통계 위젯 (화이트)

1148
469
422441


[리눅스커널] TRACE32: 'v.type' 명령어로 구조체와 enum 필드를 바로 확인하기! [Debugging] Tips

소스 코드를 분석 하다보면 구조체와 enum의 정체를 알고 싶을 때가 있습니다.
이 때 TRACE32를 활용하면 바로 이 정보를 확인할 수 있습니다.

구조체 확인하기

먼저 구조체의 세부 필드는 다음과 같은 명령어를 입력하면 확인할 수 있습니다.

$ v.type % %m %l %hi %sp  struct '구조체 이름'

자, 그럼 리눅스 커널에서 프로세스 정보를 나타내는 struct task_struct 구조체의 정체를 확인해봅시다.

$ v.type % %m %l %hi %sp  struct task_struct
(struct task_struct) struct task_struct struct (4096 bytes,
                [0] struct thread_info thread_info,
                [24] long int state,
                [32] void * stack,
                [40] atomic_t usage,
                [44] unsigned int flags,
                [48] unsigned int ptrace,
                [56] struct llist_node wake_entry,
                [64] int on_cpu,
 
위와 같이 task_struct 구조체의 세부 필드 정보를 확인할 수 있습니다.

그럼, 실제 리눅스 커널에서 struct task_struct 구조체의 선언부는 어느 코드에서 확인이 가능할까요?
struct task_struct 구조체의 정의문은 다음과 같습니다.

https://elixir.bootlin.com/linux/v5.4.30/source/include/linux/sched.h
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
* For reasons of header soup (see current_thread_info()), this
* must be the first element of task_struct.
*/
struct thread_info thread_info;
#endif
/* -1 unrunnable, 0 runnable, >0 stopped: */
volatile long state;

/*
* This begins the randomizable portion of task_struct. Only
* scheduling-critical items should be added above here.
*/
randomized_struct_fields_start

void *stack;
refcount_t usage;
/* Per task flags (PF_*), defined further below: */
unsigned int flags;
unsigned int ptrace;

#ifdef CONFIG_SMP
struct llist_node wake_entry;
int on_cpu;

enum 타입 확인하기

이번에는 enum의 선언부를 확인해봅시다.
먼저 enum 세부 필드는 다음과 같은 명령어를 입력하면 확인할 수 있습니다.

$ v.type % %m %l %hi %sp  enum 'enum 선언부'

자, 그럼 TRACE32를 활용해 리눅스 커널에서 파워 Supply 정보를 enum power_supply_property 정체를 확인해봅시다.

$ v.type % %m %l %hi %sp  enum power_supply_property

(enum power_supply_property) enum power_supply_property enum (32 bits, unsigned,
                POWER_SUPPLY_PROP_STATUS = 0,
                POWER_SUPPLY_PROP_CHARGE_TYPE = 1,
                POWER_SUPPLY_PROP_HEALTH = 2,
                POWER_SUPPLY_PROP_PRESENT = 3,
                POWER_SUPPLY_PROP_ONLINE = 4,
                POWER_SUPPLY_PROP_AUTHENTIC = 5,
                POWER_SUPPLY_PROP_TECHNOLOGY = 6,
                POWER_SUPPLY_PROP_CYCLE_COUNT = 7,
                POWER_SUPPLY_PROP_VOLTAGE_MAX = 8,
                POWER_SUPPLY_PROP_VOLTAGE_MIN = 9,
                POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN = 10,
                POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN = 11,
                POWER_SUPPLY_PROP_VOLTAGE_NOW = 12,
                POWER_SUPPLY_PROP_VOLTAGE_AVG = 13,
                POWER_SUPPLY_PROP_VOLTAGE_OCV = 14,
                POWER_SUPPLY_PROP_VOLTAGE_BOOT = 15,
                POWER_SUPPLY_PROP_CURRENT_MAX = 16,
                POWER_SUPPLY_PROP_CURRENT_NOW = 17,
                POWER_SUPPLY_PROP_CURRENT_AVG = 18,
                POWER_SUPPLY_PROP_CURRENT_BOOT = 19,
...

그럼, 실제 리눅스 커널에서 power_supply_property enum의 선언부는 어느 코드에서 확인이 가능할까요?

power_supply_property enum의 정의문은 다음과 같습니다.

https://elixir.bootlin.com/linux/v5.4.30/source/include/linux/power_supply.h
enum power_supply_property {
/* Properties of type `int' */
POWER_SUPPLY_PROP_STATUS = 0,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_AUTHENTIC,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_CYCLE_COUNT,
POWER_SUPPLY_PROP_VOLTAGE_MAX,
POWER_SUPPLY_PROP_VOLTAGE_MIN,
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_AVG,
POWER_SUPPLY_PROP_VOLTAGE_OCV,
POWER_SUPPLY_PROP_VOLTAGE_BOOT,

정리하면 TRACE32를 활용하면 struct 구조체와 enum 타입의 정체를 바로 확인할 수 있습니다.

---
"이 포스팅이 유익하다고 생각되시면 공감 혹은 댓글로 응원해주시면 감사하겠습니다. 
"혹시 궁금한 점이 있으면 댓글로 질문 남겨주세요. 아는 한 성실히 답변 올려드리겠습니다!"

​Thanks,
Guillermo Austin Kim(austindh.kim@gmail.com)
---

덧글

댓글 입력 영역