5 Replies
      Latest reply on Oct 14, 2019 2:08 AM by eskimo
      liang.zhou Level 1 Level 1 (0 points)

        I use printf() in my kernel extension to print some debug logs. When the message is a bit long(beyond 300 characters, I'm not sure the exact length), it will not be shown in Console.app. To shrink the message a little bit, it can be shown successfully. Is there any limit on the message length in the kernel? I'm using macOS 10.14.6. Thanks!

        • Re: printf in kernel extension
          eskimo Apple Staff Apple Staff (12,095 points)

          Is there any limit on the message length in the kernel?

          Yes.  The kernel’s printf implementation has numerous limitations.

          If you’re serious about logging from the kernel, you should use the <os/log.h> API / KPI.  Be aware, however, that this also has limitations when called from the kernel.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: printf in kernel extension
              liang.zhou Level 1 Level 1 (0 points)

              I try to use os_log instead of printf in my code, but no log can be shown in the Console.app.

              os_log(OS_LOG_DEFAULT, "some message";

               

              When I use the %s specifier, another problem occurs.

              os_log(OS_LOG_DEFAULT, "some message %s", cstringVar);

               

              I got this warning from Xcode:

              Invalid conversion specifier 's'

               

              Changing %s to %{public}s also doesn't work.

               

              So what is the correct way to use unified logging in a pure c environment? Thanks!

                • Re: printf in kernel extension
                  eskimo Apple Staff Apple Staff (12,095 points)

                  So what is the correct way to use unified logging in a pure c environment?

                  To be clear, we’re talking about a pure C environment within a KEXT, right?

                  I got this warning from Xcode:

                  Hmm, that’s strange.  I did something similar here and it worked as expected.  Specifically:

                  1. Using Xcode 11.1 on macOS 10.14.6, I created a new project using the Generic Kernel Extension template.

                  2. In xxx.c (where xxx is the name of the KEXT), I added an include to the top, just under the existing include:

                    #include <os/log.h>

                    .

                  3. I then added the following to xxx_start:

                    const char * cStringVar = "Hello Cruel World!";
                    os_log(OS_LOG_DEFAULT, "%s", cStringVar);

                    .

                  It compiles just fine.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: printf in kernel extension
                      liang.zhou Level 1 Level 1 (0 points)

                      I'm using Xcode 10.3 with macOS 10.12 SDK on 10.14.6. The os_log* functions is available in the SDK, but my kext cannot print any log on my own computer, even if I just print a literal C string. I've not yet tried it on 10.12 & 10.13.

                        • Re: printf in kernel extension
                          eskimo Apple Staff Apple Staff (12,095 points)

                          I'm using Xcode 10.3 … on 10.14.6.

                          FYI, I repeated the test above with Xcode 10.3 and it works as expected.

                          with macOS 10.12 SDK

                          Huh?  Xcode 10.3 ships with the 10.14 SDK.  Using it with any other SDK is unsupported and will result in weird bugs.

                          Note that os_log is one area where the SDK and toolchain must be well aligned because it uses compiler smarts to optimise the way that the format string gets included in the log.

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"