This situation is seen most often on TV legal shows like Perry Mason and Matlock, but is seen from time to time in real life.
The prosecution is trying to show that Y killed X. The usual means are to demonstrate motive, means, and opportunity. All those weight against the defendant.
The "standard" defense is to dispute one or more elements of the foregoing. But in this instance, the defense attorney uses an "indirect" defense; that Z had similar means, motive and opportunity to do the deed, thereby casting "reasonable doubt" on Y's guilt.
The prosecution objects: "Z is not on trial here, only Y."
What are the canons of trial law that govern how far the defense can go to "put Z on trial" in order to defend Y? (Let's say that Z is the prosecution's star witness, but someone who might have been given immunity in exchange for testimony against Y.) Or is it a matter that is largely left to the discretion of the judge?