As pointed out in the other answer you'll find the fine manual useful for all the debug tools it has. For you specific problem I'd consider:
(setq debug-on-message "comint-completion-at-point failed to return valid completion data")
As the help text states:
If non-nil, debug if a message matching this regexp is displayed.
From this you should get a backtrace when the failure hits. You can then instrument the functions in question with C-u C-M-x and step through the failure next time it occurs for more information.