This has already been answered at: How to access print assembled matrix from variational form - #2 by dokken