xcode 如何在 OS X 中调试屏幕保护程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1101926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:39:57  来源:igfitidea点击:

How to debug a Screensaver in OS X

objective-cxcodemacosdebuggingscreensaver

提问by Dave Martorana

I was wondering if there was any decent way, other than NSLog-ing just about everything - to properly debug a Screensaver app bundle in OS X?

我想知道是否有任何体面的方法,除了 NSLog-ing 几乎所有的东西 - 在 OS X 中正确调试屏幕保护程序应用程序包?

The "Screensaver" is a project type in Xcode, but there's obviously no Build and Go debugging. Further, I've found that in fact my bundle is getting loaded in to the

“屏幕保护程序”是 Xcode 中的项目类型,但显然没有 Build and Go 调试。此外,我发现实际上我的包正在加载到

/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app 

application as some sort of plugin.

应用程序作为某种插件。

So is there a decent way to debug your code? Looking at crash reports and NSLog-ing to the console helps, but it's far from perfect.

那么有没有一种像样的方法来调试你的代码?查看崩溃报告和 NSLog-ing 到控制台有帮助,但它远非完美。

采纳答案by Mark

There is an old MacTecharticle that describes the Screen Saver development cycle. There is also a Part 2to the article. Look in the "Debugging Tips" section.

有一篇旧的MacTech文章描述了屏幕保护程序的开发周期。文章还有第 2 部分。查看“调试技巧”部分。

I find this method a pain so I wrote an application, the basic application was one window and a controller that initialized a ScreenSaverView with my new screensaver bundle. Once that was working all I had to do to test a change was hit Command-R in Xcode.

我觉得这个方法很麻烦,所以我写了一个应用程序,基本应用程序是一个窗口和一个控制器,用我的新屏幕保护程序包初始化 ScreenSaverView。一旦这一切正常,我需要做的就是在 Xcode 中点击 Command-R 来测试更改。

回答by Karl Moskowski

Because of OS X 10.11 El Capitan's System Integrity Protection feature, the debugger can't attach to anything running from /System/. Also, the other info here applies to old versions of Xcode.

由于 OS X 10.11 El Capitan 的系统完整性保护功能,调试器无法附加到任何从/System/. 此外,此处的其他信息适用于旧版本的 Xcode。

Here's how I got it working on El Capitan with Xcode 7.2:

这是我如何使用 Xcode 7.2 在 El Capitan 上运行它:

  1. Copy /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app/to /tmp/. (Since the .xcscheme references the fully-qualified path, copying it to someplace common is best for collaboration, instead of to somewhere in a particular user's home directory.)
  2. Edit the project's .xcscheme:
    • Set the Executable for its Run action to the copied app, and add the arguments: -debug-background-module "<product-name>"(where <product-name>is the bundle name without the .saverextension).
    • Add a Pre-action script (source below), with its shell set to /bin/bashand its build settings to come from the scheme. It creates a symbolic link to the built .saverbundle in ~/Library/Screen Savers/
  1. 复制/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app//tmp/. (由于 .xcscheme 引用了完全限定的路径,因此将其复制到某个公共位置最适合协作,而不是复制到特定用户主目录中的某个位置。)
  2. 编辑项目的.xcscheme
    • 将其 Run 操作的 Executable 设置为复制的应用程序,并添加参数:(不带扩展名的包名称-debug-background-module "<product-name>"在哪里)。<product-name>.saver
    • 添加一个 Pre-action 脚本(来源如下),它的 shell 设置为/bin/bash并且它的构建设置来自于方案。它创建了一个指向内置.saver包的符号链接~/Library/Screen Savers/

Source:

来源:

SCREEN_SAVER_PATH="${HOME}/Library/Screen Savers/${FULL_PRODUCT_NAME}"
if [[ -d "${SCREEN_SAVER_PATH}" || -f "${SCREEN_SAVER_PATH}" || -L "${SCREEN_SAVER_PATH}" ]]; then
    rm -Rf "${SCREEN_SAVER_PATH}"
fi
ln -s "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}" "${SCREEN_SAVER_PATH}"

Now, when you hit Xcode's Run button, the screen saver will run in wallpaper mode on your Desktop, and you can use the debugger.

现在,当您点击 Xcode 的运行按钮时,屏幕保护程序将在您的桌面上以墙纸模式运行,您可以使用调试器。

回答by Peter N Lewis

You can debug plugins by executing the application that will load the plugin.

您可以通过执行将加载插件的应用程序来调试插件。

So to debug a screensaver, open your plugin project, choose New Custom Executable from the Project menu and set the application to be the screensaver engine.

因此,要调试屏幕保护程序,请打开您的插件项目,从“项目”菜单中选择“新建自定义可执行文件”并将应用程序设置为屏幕保护程序引擎。

For debugging a screensaver, you might also want to use a second Mac and use remote debuggingso your user interface actions don't interfere with the screensaver.

为了调试屏幕保护程序,您可能还想使用第二台 Mac 并使用远程调试,这样您的用户界面操作就不会干扰屏幕保护程序。

回答by geowar

There's a few Mac OS X apps that will run screen savers: SaverLab, Screenalicious, etc. Just find one of them on the web and download it and then set it as the target executable (as Peter N Lewis said).

有一些 Mac OS X 应用程序可以运行屏幕保护程序:SaverLab、Screenalicious 等。只需在网上找到其中一个并下载它,然后将其设置为目标可执行文件(如 Peter N Lewis 所说)。

To avoid copying the build product to '~/Library/Screen Savers/' after each build you can add a custom build script (note: I'm using '/bin/tcsh -x' for the shell):

为避免在每次构建后将构建产品复制到“~/Library/Screen Savers/”,您可以添加自定义构建脚本(注意:我在 shell 中使用了“/bin/tcsh -x”):

#remove the old screen saver or link
rm -Rf "${SCRIPT_OUTPUT_FILE_0}"

#if this is a debug build…
if ("${CONFIGURATION}" == "Debug" ) then

# create a symbolic link from our screen saver to this users screen saver directory
ln -sfv "${SCRIPT_INPUT_FILE_0}" "${SCRIPT_OUTPUT_FILE_0}"

#if this is a release build…
else if ("${CONFIGURATION}" == "Release" ) then

# copy our screen saver to this users CMM directory
cp -Rfv "${SCRIPT_INPUT_FILE_0}" "${SCRIPT_OUTPUT_FILE_0}"

endif

Then set its input file to "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}" and its output file to "${HOME}/Library/Screen Savers/${FULL_PRODUCT_NAME}".

然后将其输入文件设置为“${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}”,将其输出文件设置为“${HOME}/Library/Screen Savers/${FULL_PRODUCT_NAME}”。

Now when you build / run your project it will auto-magicly link to your debug build or copy your release build.

现在,当您构建/运行您的项目时,它会自动神奇地链接到您的调试版本或复制您的发布版本。

回答by geowar

You could also make the screensaver engine ('/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app') the target executable and pass it the -background flag (so it runs behind everything instead of in front of everything).

您还可以使屏幕保护程序引擎 ('/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app') 成为目标可执行文件,并将 -background 标志传递给它(因此它在所有内容之后运行,而不是在所有内容之前运行) .

回答by Gavin Maclean

As Peter says, you can debug the plugin by executing an application that will load the plugin.

正如彼得所说,您可以通过执行加载插件的应用程序来调试插件。

However, rather than using the screensaver engine you could also use system preferences. When the preferences appear navigate to your screensaver under "Desktop & Screen Saver" to load your plugin.

但是,您也可以使用系统首选项,而不是使用屏幕保护程序引擎。当首选项出现时,导航到“桌面和屏幕保护程序”下的屏幕保护程序以加载您的插件。

It's not perfect as your view won't be full size, but it can be easier than setting up remote debugging.

它并不完美,因为您的视图不会是全尺寸的,但它比设置远程调试更容易。

回答by cobbal

not necessarily the best way, but you could ssh in from another machine and launch ScreenSaverEngine from gdb (untested)

不一定是最好的方法,但您可以从另一台机器 ssh 进入并从 gdb 启动 ScreenSaverEngine(未经测试)

edit:

编辑

also, you could try adding a new application target and add your ScreenSaverView to a window in IB, you may have to manually configure stuff like settings, but it could help some and should probably work OK as ScreenSaverView is a subclass of NSView

此外,您可以尝试添加一个新的应用程序目标并将 ScreenSaverView 添加到 IB 中的窗口,您可能需要手动配置诸如设置之类的东西,但它可以帮助一些并且应该可以正常工作,因为 ScreenSaverView 是 NSView 的子类

回答by Trygve

If you make a copy of the ScreenSaverEngine app, and sign it with your Developer ID, it will fix the situation where System Integrity Protection prevents attaching the debugger. Just make sure to set the executable to your own-signed copy.

如果您复制 ScreenSaverEngine 应用程序,并使用您的开发人员 ID 对其进行签名,它将解决系统完整性保护阻止附加调试器的情况。只需确保将可执行文件设置为您自己签名的副本即可。

回答by 5AMp

I would like to note that @Karl's solution worked the best for me. However, if you are like me and you reboot your computer every night you may want to think about putting:

我想指出@Karl 的解决方案对我来说效果最好。但是,如果您像我一样并且每天晚上重新启动计算机,您可能需要考虑放置:

cp -Rn /System/Library/CoreServices/ScreenSaverEngine.app /tmp

cp -Rn /System/Library/CoreServices/ScreenSaverEngine.app /tmp

at the beginning of the pre-Build shell script mentioned in his answer. This will automatically do the copying step for you.

在他的回答中提到的预构建 shell 脚本的开头。这将自动为您执行复制步骤。

(though I believe this really belongs in a comment, my xp is not yet high enough)

(虽然我相信这确实属于评论,但我的xp还不够高)