Linux/Linux System Programming

커널 디버깅 - printk

Hans_S_92 2022. 11. 20. 01:44

  C언어의 printf와 같은 기능으로 커널에서 사용하는 출력 함수입니다. 

자료형의 서식지정은 C언어의 printf와 동일합니다.

* printk로 포인터를 출력하고 싶으면 %p를 쓰면 됩니다.

책의 예제에 따라 커널 소스 중 일부를 수정했다.

static void insert_wq_barrier(struct pool_workqueue *pwq,
                  struct wq_barrier *barr,
                  struct work_struct *target, struct worker *worker)
{
    struct list_head *head;
    unsigned int linked = 0;
    // 수정내용
    printk("[+] process: %s\n", current->comm);
    printk("[+][debug] message [F:%s, L:%d]: caller(%pS)\n",
            __func__, __LINE__, (void*)__builtin_return_address(0));

    /*
     * debugobject calls are safe here even with pool->lock locked
     * as we know for sure that this will not trigger any of the
     * checks and call back into the fixup functions where we
     * might deadlock.
     */
    INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
    __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));

    init_completion_map(&barr->done, &target->lockdep_map);

    barr->task = current;

    /*
     * If @target is currently being executed, schedule the
     * barrier to the worker; otherwise, put it after @target.
     
     ...
linux/kernel/workqueue.c

위와 같이 수정하고 커널 빌드 & 설치 한다.

라즈베리파이 reboot를 한 후 dmesg 출력하면 아래와 같이 추가된 printk가 적용된 모습을 확인할 수 있다. 

  • printk 로그를 실행한 프로세스의 이름은 sshd 이다.
  • printk를 출력하는 곳은 insert_wq_barrier 함수 2542라인이다.
  • insert_wq_barrier 함수는 flush_work 함수에서 호출했다.

라고 정리할 수 있다.

 

printk 출력 주의할 점

  • 자주 호출 되는 곳에 사용하면 안된다. -> 시스템 부하
  • 시스템 락업 될 수 있다, -> 커널 패닉

자주 호출 되는 커널 함수는 어떻게 콜 스택을 볼 수 있을까? => ftrace.

 

콜스택? 콜스택이란 현재 실행중인 서브루틴에 대한 정보들을 담아두는 스택구조의 메모리영역

'Linux > Linux System Programming' 카테고리의 다른 글

프로세스는 뭘까? - 정의  (0) 2022.12.08
ftrace란?  (0) 2022.11.24
dump_stack()함수  (0) 2022.11.20
커널 로그 확인 방법  (0) 2022.11.20
파일 입출력  (0) 2022.04.19