1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

inline関数とtrace-cmd/systemtap

Posted at

trace-cmdやsystemtapは、動作中のカーネルの分析などに大変便利なものですが、inline関数に弱いのが欠点です。Cの関数の入り口に細工をするので当然です。コンパイラはinlineキーワードがついていようといまいと、static関数をどんどんインライン化してしまいます。trace-cmdのfunctionプラグインの結果を眺めながら、なんでこの関数が呼ばれないのだろうと悩んでいたら、コンパイラが俺の知らないところでインライン化していた、というのもよくあることです。

どうしてもinline関数を調べたい場合は、インライン化を抑止するオプションをつけてカーネルを再コンパイルします。たとえば、pte_dirty()を調べたい場合は、

--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -118,7 +118,7 @@ extern pmdval_t early_pmd_flags;
  * The following only work if pte_present() is true.
  * Undefined behaviour if not..
  */
-static inline int pte_dirty(pte_t pte)
+static noinline __maybe_unused int pte_dirty(pte_t pte)
 {
        return pte_flags(pte) & _PAGE_DIRTY;
 }

という感じです。noinlineでインライン化を抑止します。__maybe_unusedは、gccでは__attribute__((__unused__))と展開され、「定義されたのに使われていない」という警告を止めます ([gccの文書] (https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute)を参照)。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?