Getting Basic block's scope information

Hi,
I’m trying to get all the basic blocks’ scope{starting and ending line} information inside a function.
Seems there’s no metadata node attached to basic block. So what I did is
to get the first instruction and the terminator instruction of the BB, and then
call getDebugLoc to get the line number. But the resulting ranges are not very accurate.
Similarly I want to get the ending scope line information of a function instead of just the definition line number.

I wonder if there’s better way to implement this?
For example, is it possible to somehow get a DILexicalBlock instance from a BasicBlock?

Thanks,
Ryan