The film is Kiss Kiss Bang Bang (2005), written/directed by Shane Black.
Neo-noir comedy thriller: Harry stumbles into a screen test, shadows Perry for "research," and they're entangled in a real murder mystery. Sharp dialogue, fourth-wall breaks, and great chemistry. Revitalized RDJ's career pre-Iron Man.