Theranostic agents for concurrent cancer therapy and diagnosis have begun attracting attention as a promising modality. However, accurate imaging and identification remains a great challenge for theranostic agents. Here, we designed and synthesized a novel theranostic agent H6M based on the "double-locked" strategy by introducing an electron-withdrawing nitro group into 1-position of a pH-responsive 3-amino-β-carboline and further covalently linking the hydroxamic acid group, a zinc-binding group (ZBG), to the 3-position of β-carboline to obtain histone deacetylase (HDAC) inhibitory effect for combined HDAC-targeted therapy. We found that H6M can be specifically reduced under overexpressed nitroreductase (NTR) to produce H6AQ, which emits bright fluorescence at low pH. Notably, H6M demonstrated a selective fluorescence imaging via successive reactions with NTR (first "key") and pH (second "key"), and precisely identified tumor margins with a high S/N ratio to guide tumor resection. Finally, H6M exerted robust HDAC1/cancer cell inhibitory activities compared with a known HDAC inhibitor SAHA. Therefore, the NTR/pH-activated theranostic agent provided a novel tool for precise diagnosis and efficient tumor therapy.