I'm encountering an issue on my mid-2012 Macbook Pro (system specs below). While performing an Xcode build of an Ionic project from the command line, I see (once every ten times or so?) the process stall and almost all apps hang or beachball.

I am generally able to switch between apps and open menus, but apps will not respond to clicks inside the window or on menu items. Tooltips still appear when I hover over items in the Dock and I am able to switch apps by clicking Dock items. When I try to shut down, it tells me that every single one of my apps is not responding, but even if I am able to force-quit them, I am not able to actually restart and instead have to hold down the power button.

Strangely, Terminal keeps responding through all of this, and I can open files and run new processes, however if I want to run a process that requires net access, such as lynx, the request times out.

I don't know where to start debugging this issue. Any insight on Mac-specific command line tools that would help analyze this while it's actually happening?